-1

so i have same problem with this question Auto Increment after delete in MySQL

and according to that answer, and considering for each condition that provided on that answer, i've to say that i must do the auto increment after delete on mysql.

i have table lets say

table : product
fields : id, name

so if i have ids of 16,17,18 and then i deleted id 16, and i make the new one, i want to make the new id was on "16" not on 19. according to that answer

Take a step back and ask "why you need to recycle key values?" Do unsigned INT (or BIGINT) not provide a large enough key space?

i must say that yes, it's possible the fields cant provide large enough key space

what should i do

note: the id on that table its unique and didnt connected to any other table, i just want to avoid if the ids cant provide large enough key space again

18Man
  • 572
  • 5
  • 17

1 Answers1

1

How to achieve it?

To be honest, this is achieveable.Insert record's corresponding id value into an ordered queue every time it is deleted, and take the smallest value from the queue as the id to be insered(if the queue is empty, use id generated by database).

Is it worth doing?

I will assume that your primary key is the id, whcih is a clustered index in mysql. So this way may cause more page splits, which will slow insertion performance.
And you need to ensure that the id in the ordered queue can only be consumed by one inserting thread at the same time.If you inserted fail, you need to re-enqueue the id.If the size of the ordered queue is too large, the sorting operation will also slow performance.
At last, you can't rely on id for analysis. eg. The record with max id is the latest record.

spike 王建
  • 1,556
  • 5
  • 14