Can AUTO_INCREMENT be safely used in a BEFORE TRIGGER in MySQL

前端 未结 2 2131
被撕碎了的回忆
被撕碎了的回忆 2021-02-20 15:19

Instagram\'s Postgres method of implementing custom Ids for Sharding is great, but I need the implementation in MySQL.

So, I converted the method found at the bottom of

2条回答
  •  执念已碎
    2021-02-20 15:51

    An alternative is to grab blocks of auto increment numbers. If you set MySQLs auto increment increment to something like 1000, a process can do an insert in the "sequence" table and get the auto increment value. The process then knows that it has 1000 sequential numbers it can use, starting at that number, that will be free of conflicts. There is no need to record every increment in a central table if all you are recording is a number.

    This is most commonly used in multiple master setups in addition to the auto increment offset. You could go the multiple master route too, and insert on the different masters. The auto increment increment and offset would assure no conflicts. This would require solid knowledge of MySQL replication.

提交回复
热议问题