The difference between MySQL 8 and MySQL 5.7 in auto-increment counting

The difference between MySQL 8 and MySQL 5.7 in auto-increment counting

作宇:Arunjith Aravindan

Source of this article: Percona blog, translated by the Axon open source community.

This article is about 900 words and is expected to take 2 minutes to read.

Auto-Increment

The Auto-Increment counting function can generate unique values ​​for the primary key column, which is a design of the database. Compared with MySQL 5.7, MySQL 8 has made an important upgrade for the auto-increment function. This upgrade ensures that the maximum value of the self-increment counter remains unchanged after the server is restarted, thus providing better guarantee for data consistency and reliability. In this article, we will compare the differences between MySQL 5.7 and MySQL 8 and provide practical examples to demonstrate the differences.

MySQL 5.7 auto-increment

In MySQL 5.7, the working mechanism of the auto-increment counter is as follows: when a new row of data is inserted into a table containing an auto-increment column, the counter will automatically increase by 1, and the generated value will be used as the primary key of the inserted row. This counter value is only saved in memory and cannot be persisted across server restarts. Therefore, if the server crashes or is restarted, the counter may be reset to a lower value.

Auto-increment persistence in MySQL 8

With the release of MySQL 8, the auto-increment counter mechanism has been significantly improved. In MySQL 8, the maximum value of the auto-increment counter can now be persisted across server restarts. This means that even if the server is restarted, the auto-increment counter will resume from where it last ended, ensuring that the value of the auto-increment primary key remains continuous.

Example comparison

Let's use a simple example to illustrate the difference between MySQL 5.7 and MySQL 8 in terms of persistent self-incrementing counters. We will create a userstable named to store user information.

Create the table in MySQL 5.7.

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.42-46 |
+-----------+

mysql> CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL
);
Query OK, 0 rows affected (0.02 sec)

Insert three pieces of data into the table and you can view them.

mysql> INSERT INTO users (username) VALUES ('user1');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO users (username) VALUES ('user2');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO users (username) VALUES ('user3');
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
|  3 | user3    |
+----+----------+
3 rows in set (0.00 sec)

We proceed by deleting a record and inserting a new one.

mysql> delete from users where id=3;
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
+----+----------+
2 rows in set (0.00 sec)

mysql> INSERT INTO users (username) VALUES ('user4');
Query OK, 1 row affected (0.01 sec)

After deleting the record with ID 3 and inserting the new record, we observe that the new record has an ID of 4, as expected.

mysql> select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
|  4 | user4    |
+----+----------+
3 rows in set (0.00 sec)

Now, we usersdelete the last record (ID=4) from the table, restart the server, and check the table contents.

mysql> delete from users where id=4;
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
+----+----------+
2 rows in set (0.00 sec)

service mysql restart

mysql> select * from users;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    2
Current database: db1

+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
+----+----------+
2 rows in set (0.01 sec)

There are only two records left in the table. We insert the fifth record and determine whether it uses ID 5 or falls back to ID 3.

mysql> INSERT INTO users (username) VALUES ('user5');
Query OK, 1 row affected (0.00 sec)

mysql> select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
|  3 | user5    |
+----+----------+
3 rows in set (0.00 sec)

Therefore, in MySQL 5.7, a restart causes the autoincrement counter to reset to a lower value, causing new records to be inserted with ID 3.

MySQL 8 solution

MySQL 8 solves the problem of the InnoDB storage engine losing its auto-increment counter when the server is restarted. This enhancement ensures that the value of the auto-increment counter is persisted after server restart, thereby ensuring the consistency of primary key generation.

Create a table in MySQL 8.

mysql> select version();
+-------------------------+
| version()               |
+-------------------------+
| 8.0.33-0ubuntu0.22.04.2 |
+-------------------------+
1 row in set (0.00 sec)

mysql> CREATE TABLE users (
    ->     id INT AUTO_INCREMENT PRIMARY KEY,
    ->     username VARCHAR(50) NOT NULL
    -> );
Query OK, 0 rows affected (0.04 sec)

Insert three pieces of data into the table and you can view them.

mysql> INSERT INTO users (username) VALUES ('user1');
Query OK, 1 row affected (0.07 sec)

mysql> INSERT INTO users (username) VALUES ('user2');
Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO users (username) VALUES ('user3');
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
|  3 | user3    |
+----+----------+
3 rows in set (0.00 sec)

Next, delete one and insert one.

mysql> delete from users where id=3;
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
+----+----------+
2 rows in set (0.00 sec)

mysql> INSERT INTO users (username) VALUES ('user4');
Query OK, 1 row affected (0.01 sec)

Delete the record with ID 3 and insert a new record with ID 4.

mysql>  select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
|  4 | user4    |
+----+----------+
3 rows in set (0.00 sec)

After deleting the last record (ID=4), restart the server and view the table.

mysql> delete from users where id=4;
Query OK, 1 row affected (0.01 sec)

mysql>  select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
+----+----------+
2 rows in set (0.00 sec)

service mysql restart

mysql> select * from users;
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
Connection id:    8
Current database: db1
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
+----+----------+
2 rows in set (0.02 sec)

After restarting, usersonly two records remain in the table. In MySQL 8, when inserting a new record, it takes ID=5 as expected.

mysql> INSERT INTO users (username) VALUES ('user5');
Query OK, 1 row affected (0.01 sec)

mysql>  select * from users;
+----+----------+
| id | username |
+----+----------+
|  1 | user1    |
|  2 | user2    |
|  5 | user5    |
+----+----------+
3 rows in set (0.00 sec)

Summarize

An issue with auto-increment counters reported by the InnoDB storage engine in versions prior to MySQL 8 could cause confusion and data inconsistencies, especially during server restarts. The value of the counter may be missing, causing the automatically generated primary key value to mismatch. MySQL 8 solves this problem by ensuring that the incrementing counter persists across server restarts.

By upgrading to MySQL 8, developers can take advantage of this feature to create more robust applications that can manage different failure scenarios without compromising data integrity.

https://www.percona.com/blog/auto-increment-counter-persistence-in-mysql-8-comparing-the-evolution-from-mysql-5-7/

For more technical articles, please visit: https://opensource.actionsky.com/

About SQLE

SQLE from the Axon open source community is a SQL audit tool for database users and managers that supports multi-scenario audits, standardized online processes, native support for MySQL audits and scalable database types.

SQLE get

type address
Repository https://github.com/actiontech/sqle
document https://actiontech.github.io/sqle-docs/
release news https://github.com/actiontech/sqle/releases
Data audit plug-in development documentation https://actiontech.github.io/sqle-docs/docs/dev-manual/plugins/howtouse

Guess you like

Origin blog.csdn.net/ActionTech/article/details/133278270