Persistence of auto-incrementing variables [MySQL][database]

Persistence of self-incrementing variables

Before MySQL 8.0, if the value of the self-incrementing primary key AUTO_INCREMENT is greater than max(primary key) + 1, then after MySQL restarts, it will reset AUTO_INCREMENT = max(primary key) + 1. This phenomenon may cause problems in some cases. Causes business primary key conflicts or other hard-to-find problems

  • The reason for this problem is that in MySQL 5.7, the allocation rules for self-incrementing primary keys are determined by a counter inside the InnoDB data dictionary, which is maintained in memory and will not persist to disk. (that is, in the hard disk), when the database is restarted, the counter will be initialized, and the result of the initialization at this time is: auto_increment = max(primary key)+1

In MySQL 8.0, the counter of the self-incrementing primary key is persisted to the redo log, and each time the counter changes, it will be written to the redo log. If the database is restarted at this time, the InnoDB data dictionary The memory value of the counter is initialized according to the information in the redo log

  • Then use this method to persist the counter to the redo log, and then save the redo log to disk. At this time, even if we lose power (or after the database is restarted), our redo log will still remember us. The last self-increment value, this time there will be no problems in MySQL 5.7
Here we use an example to demonstrate the difference between the self-increasing variables before they are not persistent (that is, before MySQL 8.0) and after the self-increasing variables are persistent (that is, after MySQL 8.0 starts):
  1. First, let's analyze the situation in MySQL 5.7
    • That is, the persistence of self-incrementing variables has not been performed yet.
CREATE TABLE test9(
id INT PRIMARY KEY AUTO_INCREMENT
);
  • Here we first create a data table, and set an auto-increment column in this data table
INSERT INTO test9
VALUES (0),(0),(0),(0);
  • At this time, we added 4 records to the test9 table. When we add 0 at this time, it is actually the default assignment, because we cannot add 0 or null to our auto-increment column, then the four added records in the table at this time. The id field values ​​are: 1, 2, 3, 4
DELETE FROM test9
WHERE id = 4;
  • At this time, we delete the field with id 4 in the table
INSERT INTO test9
VALUES(0);
  • At this time, we continue to add a record to the test9 table. What should the id of the record we add at this time be? — At this time, we can find that the result is 5, because we have deleted the record with id 4 in the table before. , at this time, even if there is no field with an id of 4 in the table in the next auto-increment, this time we will not add 4, but add 5 - in fact, at this time, the value of the auto-increment primary key auto_increment is greater than max(primary key)+1
DELETE FROM test9
WHERE id = 5;
  • At this time, we will delete the record with id 5 in the table.
This step is important:

At this time we have to restart the database

  • So how do we restart the database?
    1. We need to use the "net stop database" command in cmd (command prompt) to close the database
      • Note: The database here is not a specific database name, but the name of the database we started in the background, we can go to the opened background to query this name (through my computer, then right-click to enter the management, and then Find the service and application in the management, then click on the service, and then find the name of our database in the list (Note: The premise is that our database is already started)
    2. Then use the "net start database" command to start our database
INSERT INTO test9
VALUES(0);
  • Then after restarting the database, we will add a record to the test9 table, and also add an id of 0, which is actually the default way of adding -- then will the id added at this time be 6? (Press our The previous thought should be 6, because 4 and 5 have been deleted)

    • But at this time, after we execute it, we can find that the id value of the record added in the result of the execution here is: 4
    • This is because the data in the database memory disappears after our database is restarted. This time is in MySQL 5.7. At this time, the counter for the self-incrementing primary key is maintained in the memory. At this time, the data in the memory disappears, then That is to say, the counter needs to be re-initialized. At this time, our counter initialization is to set the auto-increment primary key auto_increment to max(primary key)+1, that is, to restore our auto-increment value to the maximum id+1 in the table

But if all the above operations are performed in MySQL 8.0, the result will be different at this time:

If the same operation is done in MySQL 8.0, the execution results of all the previous steps are the same, but for the last operation of adding data (that is, the operation after restarting the database), the execution results are different. ---- If it is in MySQL 8.0, then after the last operation of adding data, we query the data in the table to find that the id of the data we added is 6, why is it 6 at this time?

  • Because in MySQL 8.0 we have the function of persistence for auto-increment columns, in MySQL 8.0 we added a redo log to record the changes of each auto-increment column counter in the redo log, and we The redo log is persisted to disk, so when we restart the database, the data in our redo log will not disappear, and after we restart the database, the self-incrementing primary key counter will be initialized through the redo log, then this The id of the record added here should be 6, because the records with id 4 and 5 have been deleted.

Guess you like

Origin blog.csdn.net/m0_57001006/article/details/123625810