How to deal with running out of MySQL auto-increment IDs

  1. Check the current maximum auto-increment ID value: You can use the following SQL query statement to obtain the current maximum auto-increment ID value:

    SELECT MAX(id) FROM your_table;
    

    Suppose your table name is your_tableand auto-increment ID column name id.

  2. Determine the type of auto-increment ID used: Determine the type of auto-increment ID you use based on the current maximum value. If the current maximum value is not reaching the type limit, you may want to consider upgrading to an auto-incrementing ID type that has not yet reached the limit.

    • If you're using INTthe type, the maximum value is 2147483647.
    • If you're using BIGINTthe type, the maximum value is 9223372036854775807.
  3. Upgrade auto-increment ID type: If your auto-increment ID type has reached the upper limit, if there is a backup,

    3.1 You can upgrade the auto-increment ID type through the following steps:

    • Create a new auto-increment ID column with a larger range, such as BIGINTtype.
    • Copy all data from old table to new table.
    • Update all associated foreign keys and indexes.
    • Modify the application code to accommodate the new table structure.
    • Deactivate the old table and drop it.
    • Modify the name of the new table to match the name of the old table.

    3.2 Continue to start from the maximum ID value, circle back to the minimum ID value for recycling

    Set the auto-increment primary key as an unsigned integer, and adjust the maximum value to prevent overflow:

    alter table tableName modify id int unsigned;
    
    alter table tableName change id id int unsigned AUTO_INCREMENT=1;
    

    3.3 Reset the auto-increment column, return to the initial value and start over

    alter table tableName auto_increment=1;
    

    3.4 Use non-incrementing primary keys such as GUID/UUID instead

    Add a GUID type primary key to the table, and automatically generate a GUID value when inserting data.

    3.5 Divide tables and databases, split data into different tables or libraries

    Split a single large-capacity table into multiple tables to make the auto-increment ID of each table available in a larger range.

    3.6 Use combined primary keys to reduce single primary key dependencies

    Combining multiple columns as the primary key can avoid the problem of running out of auto-increment IDs for a single primary key.

    3.7 Regularly clean up unused IDs We can regularly clean up those IDs that have been used but are no longer used, and release them for new data. This requires us to judge carefully at the business layer that those IDs will no longer be used.

    3.8 Using a distributed ID generator, a distributed ID generator can generate a globally unique ID without being limited by a single database or table.

    Choose an appropriate solution to prevent and deal with the problem of exhaustion of self-incrementing IDs, and ensure the stable operation of the system.

Note that before execution, it is recommended to back up the database to prevent data loss or errors. Also, these may involve some complex operations, so proceed with caution and adapt them to your specific situation. It's a good idea to try these steps first in a test environment to make sure it applies to your situation.

write at the end

If you are interested in related articles, you can pay attention to the official account "Architecture Hall", and will continue to update AIGC, java basic interview questions, netty, spring boot, spring cloud and other series of articles, and a series of dry goods will be delivered at any time!

Guess you like

Origin blog.csdn.net/jinxinxin1314/article/details/132466722