Answer the question
In order to leave comments, you need to log in
What is the best way to store and add an ordinal number in a table?
Good afternoon!
Faced with the problem that with a large number of threads in the database, duplicates for the serial number of the document begin to appear.
It works so that before writing to the table, the last sequence number is incremented:
SELECT ui_id+1 FROM deals WHERE user_id = 1 ORDER BY ui_id DESC LIMIT 1;
INSERT INTO `deals` (`id`, `ui_id`, `user_id`) VALUES (NULL, "5", "1")
Answer the question
In order to leave comments, you need to log in
And why use ui_id at all if id should already be a unique auto-incrementing ID.
Completely lock the table until the end of the write.
LOCK TABLES `deals` FOR WRITE;
...
UNLOCK TABLES;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question