-2

I use AUTO_INCREMENT for my PK and I want to reuse ids when I remove a record from the table.

For example, if I delete the record with id=5, I want the next insert of a new record to use the empty id=5.

So how can I fill the unused IDs with AUTO_INCREMENT?

Hayi
  • 6,972
  • 26
  • 80
  • 139

1 Answers1

2

The notion of reusing auto_increments is a horrible idea. Biggest reason for this is concurrency, among others. You can (and will) make your entire database and data model unstable.

Using a proper data type for the id will rectify the issue of you running out of numbers, proper data type being bigint in this case.

The bigint allows you to store up to the size of 18446744073709551615.

If your inserts are 1000 a second, that means during 1 day you will perform 1000 * 3600 * 24 records, or 86 400 000 records a day. During the course of a year, that will be 31,536,000,000 records. Using simple maths, you can calculate that you'll exceed the bigint from above in some 58 000 years if you are able to produce 1000 inserts a second during that time, which should convince you to use proper data types for handling records rather than trying to be smart about that will only result in failure.

N.B.
  • 13,688
  • 3
  • 45
  • 55