The auto-increment feature, which generates unique values for primary key columns, is an integral part of the database’s design. With the release of MySQL 8, a notable enhancement was introduced to the auto-increment counter. Compared to MySQL 5.7, this enhancement ensures that the maximum auto-increment counter value persists between server restarts, providing enhanced consistency and reliability in data management. In this article, we will look into this enhancement, compare MySQL 5.7 with MySQL 8, and provide practical examples to demonstrate the differences.

Auto-increment in MySQL 5.7

In MySQL 5.7, the auto-increment counter works as follows: when a new row is inserted into a table with an auto-increment column, the counter increments by 1, and the generated value is used for the primary key of the inserted row. This counter value is stored in memory and is not persistent across server restarts. As a result, if the server crashes or is restarted, the counter might reset to a lower value.

Persistent auto-increment counter in MySQL 8

With the release of MySQL 8, a significant improvement was made to the auto-increment counter mechanism. In MySQL 8, the maximum auto-increment counter value is now persisted across server restarts. This means that even if the server is restarted, the auto-increment counter will resume from where it left off, ensuring that Auto-Increment primary key values remain sequential.

Key differences illustrated with an example

Let’s consider a simple example to illustrate the differences between MySQL 5.7 and MySQL 8 regarding the persistent auto-increment counter. We will create a table called “users” to store user information:

Table Creation in MySQL 5.7:

Now, let’s insert three records into the “users” table within MySQL 5.7:

Following this, we can query three entries in the “users” table:

We proceed to delete one record and insert a new one:

After the removal of the record with ID 3 and the insertion of a new record, as expected, we observe the ID 4 for the new entry:

Now, let’s delete the last record (ID 4) from the “users” table, perform a restart, and inspect the table’s contents:

Only two records remain in the table. To explore, let’s insert the fifth record and assess whether it adopts ID 5 or reverts to ID 3:

Consequently, in MySQL 5.7, a restart causes the auto-increment counter to reset to a lower value, leading to the insertion of the new record with ID 3.

MySQL 8’s solution

MySQL 8 addresses the auto-increment counter loss issue in the InnoDB storage engine upon server restart. This enhancement ensures that the auto-increment counter’s value persists across server restarts, guaranteeing consistency in generating primary keys.

Creation of the Table in MySQL 8:

Inserting three records in MySQL 8 proceeds as follows:

Upon inspection, the records in the “users” table are as follows:

Subsequently, we delete a record and insert a new one:

Following the deletion of record ID 3 and the insertion of a new entry, the ID 4 is utilized:

Upon deletion of the last record (ID 4), restarting, and reviewing the table.

After the restart, the “users” table maintains just two records. Upon inserting a new entry, as anticipated in MySQL 8, the record is added with the ID 5.

Conclusion

The auto-increment counter issue reported in MySQL versions prior to MySQL 8’s InnoDB storage engine could cause confusion and possibly data inconsistency, especially during server restarts. The value of the counter could be lost, resulting in a mismatch in auto-generated primary key values. MySQL 8 has fixed this problem by guaranteeing that the auto-increment counter persists between server restarts. 

By upgrading to MySQL 8, developers can use this functionality to create more robust applications that manage diverse failure scenarios without affecting data integrity. 

Please read the following blogs to learn more about ways to make the major version upgrade easier:

Percona Distribution for MySQL is the most complete, stable, scalable, and secure open source MySQL solution available, delivering enterprise-grade database environments for your most critical business applications… and it’s free to use!

 

Try Percona Distribution for MySQL today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments