What should I do if the MySQL auto-increment ID is used up? 4 solutions!

The principle of MySQL auto-increment ID

MySQL's auto-increment ID is generated through the auto-increment mechanism. When a new table is created and an auto-increment column is defined, MySQL creates a counter called AUTO_INCREMENT in the table. Whenever a new row of data is inserted, MySQL will automatically increase the value of this counter and insert this new value into the auto-increment column. In this way, each row of data will have a unique auto-increment ID.

By default, the initial value of the auto-increment ID is 1, and it increments by 1 each time. This starting value can be changed with the ALTER TABLE statement. If you need to use a custom starting value in the table, you can use the following command:

ALTER TABLE my_table AUTO_INCREMENT = 1000;

If you need to view the current value of the auto-increment ID, you can use the following command:

SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';

What happens when auto-increment IDs are exhausted?

There are two cases to discuss, one is that the primary key is specified, and the other is that the primary key is not specified. Let's look at the first case first :

When you insert a large amount of data into the table, the value of the auto-incrementing ID counter may increase to a very large value until it reaches the maximum value of the INT or BIGINT data type. If you continue to insert data, MySQL will try to increment the value of the auto-increment ID by 1, but due to data type limitations, it will not be able to increment and will throw an error.

For example, if your table uses the INT data type, the maximum value is 2147483647, if the value of the auto-increment ID has reached the maximum value, then MySQL will no longer be able to generate a new auto-increment ID, and you will not be able to insert new records.

In the second case, if no primary key is specified, InnoDB will create an invisible row_id with a length of 6 bytes for you. InnoDB maintains a global dict_sys.row_id value. For all InnoDB tables without a primary key, each time a row of data is inserted, the current dict_sys.row_id value is used as the row_id of the data to be inserted, and then the value of dict_sys.row_id is increased by 1.

In fact, row_id is an unsigned long integer (bigint unsigned) with a length of 8 bytes when the code is implemented. However, when InnoDB is designed, only 6 bytes of length are reserved for row_id, so that only the last 6 bytes are written into the data table, so there are two row_id values ​​that can be written into the data table feature:

  • The value range of row_id written in the table is from 0 to 248-1;
  • When dict_sys.row_id=2^48, if there is another behavior of inserting data to apply for row_id, the last 6 bytes will be 0 after getting it.

Although the number 2^48 is already very large, everyone must know that a system can run for a long time, so it may still reach the upper limit, and applying again at this time will overwrite the original record. Therefore, try not to choose this way!

Solution

Solution 1: Use BIGINT data type

One workaround is to use the BIGINT data type. The maximum value of the BIGINT data type is 9223372036854775807, which is much larger than the INT data type. If you use the BIGINT data type to store auto-increment IDs, your table can insert more data without running out of auto-increment IDs.

However, there are also some disadvantages to using the BIGINT data type. First, it requires more storage space because the BIGINT data type requires 8 bytes while the INT data type requires only 4 bytes. Second, using the BIGINT data type may affect query performance because MySQL needs to process larger data blocks.

Solution 2: Reset the initial value of the auto-increment ID

Another solution is to reset the initial value of the auto-increment ID. You can reset the starting value of the auto-increment ID to a higher number by using the ALTER TABLE statement. For example, if your auto-increment ID has reached 2147483647, you can use the following command to reset the initial value of auto-increment ID to 3000000000:

ALTER TABLE my_table AUTO_INCREMENT = 3000000000;

This way, you can insert new data records into the table again.

However, this approach has some limitations. First, you need to make sure that the starting value of the auto-increment ID is large enough to insert enough records into the table. If your table can only hold 2147483647 records, even if you reset the auto-increment ID start value to 3000000000, you still cannot insert more records.

Second, resetting the initial value of the auto-increment ID may cause some problems. For example, if you delete some records before inserting new records, the new records may have an auto-incremented ID that has already been used. This can cause uniqueness constraint violations.

Solution 3: Use a distributed ID generator

Another solution is to use a distributed ID generator. A distributed ID generator can generate globally unique IDs without being limited by a single database or table. For example, Twitter's Snowflake algorithm is a distributed ID generator.

The ID generated by the Snowflake algorithm is a 64-bit integer, which includes a 41-bit timestamp, 10-bit working machine ID, and 12-bit serial number. The Snowflake algorithm can guarantee that IDs generated on different machines are unique, and at the same time ensure that the generated IDs are incremental, which makes it very suitable as a globally unique ID.

The advantage of using a distributed ID generator is that you can generate a new ID at any time without worrying about running out of auto-increment IDs. However, there are also some disadvantages to using a distributed ID generator.

First, generating a globally unique ID requires some computing and storage resources. This means that your application needs to do extra calculations when generating IDs, and use more storage space when storing IDs.

Second, distributed ID generators also have the potential to cause some performance issues. Since ID generators are distributed, different nodes may need to coordinate to ensure that generated IDs are unique. This may cause some latency and additional network overhead.

Solution 4: Use UUIDs

A final solution is to use a UUID (Universally Unique Identifier). UUID is a 128-bit identifier that is guaranteed to be globally unique. You can use UUID as primary key instead of auto-incrementing ID.

The advantage of using UUID is that you don't have to worry about running out of IDs, because the number of UUIDs is very large, far exceeding the number of self-incrementing IDs. Also, UUIDs are globally unique, so you can use them with multiple nodes in a distributed environment.

However, using UUIDs also has some disadvantages. First of all, the length of UUID is much longer than that of self-incrementing ID, which means that more storage and computing resources are required when storing and indexing UUID.

Second, using UUIDs as primary keys can cause performance issues. Since UUIDs are randomly generated, rather than incremented, this can lead to inefficient indexing. If you have a large number of records in your table, using a UUID as a primary key may cause poor query performance.

おすすめ

転載: blog.csdn.net/Dark_orange/article/details/130244621