Interviewer: What happens if the database self-increase ID runs out?

Seeing this problem, I remembered that when I was playing World of Warcraft, the blood volume of the 25H Difficulty Brain Roar had exceeded 2.1 billion, so at that time the BOSS of the dungeon was designed to be a transition and blood recovery mode, because of the blood volume of World of Warcraft. It is of int type and cannot exceed 2^32 size.

It is estimated that the designers of Blizzard did not expect that the HP of several expansions will exceed the upper limit of int, so that everyone guesses that there will be later attribute compression.

These are all digressions, just to tell you that the amount of data may reach the upper limit. Going back to the problem of Mysql self-increasing ID upper limit, it can be divided into two aspects.

1. Has a primary key

If the primary key is set, and the primary key is generally set to auto-increment.
We know that the int type in Mysql is 4 bytes. If there is a sign bit, it is [-2 31,2 31-1] . If there is an unsigned bit, the maximum value is 2^32-1, which is 4294967295.
Try to create a table:

CREATE TABLE `test1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(32) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2147483647 DEFAULT CHARSET=utf8mb4;

Then perform the insert

insert into test1(name) values('qq');

In this way, there is a piece of data in the table that reaches the upper limit of the maximum signed bit.
Insert picture description here
If you execute the insert statement again:

insert into test1(name) values('ww');

You will see the error message: 1062-Duplicate entry '2147483647' for key'PRIMARY', Time: 0.000000s.
In other words, if the primary key is set and auto-incremented, a duplicate primary key key will be reported when the upper limit of the auto-increment primary key is reached.
The solution is to change the mysql primary key to bigint, which is 8 bytes.
When designing, it is necessary to consider clearly what the upper limit of the value is. If the business is frequently inserted, the figure of 2.1 billion is actually still possible.

2. No primary key

If the primary key is not set, InnoDB will automatically create a 6-byte row_id for you. Since the row_id is unsigned, the maximum length is 2^48-1.
Also create a table as a test:

CREATE TABLE `test2` (
 `name` varchar(32) NOT NULL DEFAULT ''
) ENGINE

Get the process ID of mysql through ps -ef|grep mysql, then execute the command, first modify the row_id to 1 through gdb

sudo gdb -p 2584 -ex 'p dict_sys->row_id=1' -batch

Then insert a few pieces of data:

insert into test2(name) values('1');
insert into test2(name) values('2');
insert into test2(name) values('3');

Modify row_id to 2^48 again, which is 281474976710656

sudo gdb -p 2584 -ex 'p dict_sys->row_id=281474976710656' -batch

Insert data again

insert into test2(name) values('4');
insert into test2(name) values('5');
insert into test2(name) values('6');

Then query the data will find 4 data, respectively 4, 5, 6, 3.
Because we first set row_id=1 to start, the row_id of 1, 2, 3 is also 1, 2, 3.
After modifying the row_id to the upper limit, the row_id will be calculated from 0 again, so the row_id of 4, 5, 6 is 0, 1, 2.
Since 1, 2 data already exists, the data will be overwritten.

to sum up

After the auto-increment ID reaches the upper limit and is used up, it is divided into two situations:
if the primary key is set, then an error will be reported for the primary key conflict.
If the primary key is not set, the database will automatically generate a global row_id for us, and the new data will overwrite the old data.
Solution:

The table should set the primary key as much as possible, and the primary key should use the bigint type as much as possible. The upper limit of 2.1 billion is still possible. For example, the upper limit of row_id is as high as 281 trillion, but the data coverage is obviously unacceptable.

Reader benefits

Thank you for seeing here!
I have compiled a lot of 2021 latest Java interview questions (including answers) and Java study notes here, as shown below
Insert picture description here

The answers to the above interview questions are organized into document notes. As well as interviews also compiled some information on some of the manufacturers & interview Zhenti latest 2021 collection (both documenting a small portion of the screenshot) free for everyone to share, in need can click to enter signal: CSDN! Free to share~

If you like this article, please forward it and like it.

Remember to follow me!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49527334/article/details/112630029