When to fix auto-increment gaps in MYSQL

后端 未结 2 1675
再見小時候
再見小時候 2021-01-23 04:19

The database I am working on right now has records being added and removed by the 10s of thousands and because of this there are gaps in the auto-incremented key hundreds of tho

相关标签:
2条回答
  • 2021-01-23 04:43

    I don't know what the growth rate of your autoincrement field is, but it should be simple math for you to estimate when you will hit the 4294967295 limit.

    If you still feel that you need to do something, you have the following options:

    1. reset the current count to 1. Do this ideally by dropping the column and recreating it. Since you are not using this field for referential integrity, should be a quick and simple fix until the next time...
    2. Change the datatype to an unsigned BIGINT. Now you can go up to 18446744073709551615. But you need more space in the heap to store this increased amount of data, and you have only postponed your problem.
    3. Change from an autoincrement (INT / BIGINT) to a UUID. Then you can stop worrying about numbers and the nature of infinity, but you most likely will have to change all of your client code.

    On a separate note, I sense a poor decision or two somewhere earlier up the line here.

    0 讨论(0)
  • 2021-01-23 04:53

    The only reason I'd worry about it is if you find yourself close to that 2^32 limit. If you're not using the column as a row id, then don't even worry about it.

    EDIT If you are using this column for any kind of identifying information, then I'd switch the column over to a GUID or something, because you're gonna get overflow, and then you'll get duplicate values. And that's no bueno.

    0 讨论(0)
提交回复
热议问题