What should I do if I run out of auto-increment primary keys?

In this article, let's talk about the primary key problem of mysql table.

First raise two questions:

Question 1: Is it to use uuid or auto-increment the primary key?

Question 2: If the auto-increment primary key is used up, how to solve it?

So how to answer this kind of question?

We will solve the above problems in this article.

Question 1: Is it to use uuid or auto-increment the primary key?

It is best to use an auto-incrementing primary key, mainly for the following two reasons:

1. If the table uses an auto-increasing primary key, every time a new record is inserted, the record will be sequentially added to the subsequent position of the current index node. When a page is full, a new page will be automatically opened.
  2. If a non-incremental primary key (such as uuid) is used, since the value of the primary key inserted each time is approximately random, each new record must be inserted into a random position on the index page. At this time, MySQL is to insert the new record. Move data to a suitable location, and even the target page may have been written back to the disk and cleared from the cache. At this time, it must be read back from the disk. This adds a lot of overhead. At the same time, frequent moving and paging operations cause indexes. Fragmentation resulted in an index structure that was not compact enough. Later, OPTIMIZE TABLE had to be used to rebuild the table and optimize the page filling.

However, it is not necessary to use auto-incrementing primary keys in all scenarios. In possible scenarios, the primary key must be generated by itself, regardless of the performance overhead. That's no problem.

Question 2: If the auto-increment primary key is used up, how to solve it?

First of all, we have to understand that in mysql, the range of int integer type is as follows:

img

Let's take the unsigned integer type as an example. The storage range is 0~4294967295, which is about 4.3 billion! Let's talk about it first, once the auto-increment id reaches the maximum value, if the data continues to be inserted at this time, a primary key conflict exception will be reported as shown below

//Duplicate entry '4294967295' for key 'PRIMARY'

The solution is also very simple. Change the Int type to BigInt type. The scope of BigInt is as follows:

img

Even if you have 10,000 data per second and run for 100 years, the data in a single table is only 10000*24*3600*365*100=31536000000000

This number is far from the upper limit of BigInt, so if you set the auto-increment ID to BigInt type, you don't need to consider the issue of auto-increment ID reaching the maximum value!

At this time, if you answer: change the type of the auto-incrementing primary key to the BigInt type.

Then there will be more questions (pits) in the follow-up: For example, how do you modify the data type of a column online?

So what is the professional answer to the question "How to solve if the auto-increment primary key runs out"?
You answer: I have never encountered this problem, because the auto-increment primary key usually uses the int type, and usually does not reach the maximum value, and the database and table will be divided, so I have never encountered this kind of problem.

The picture comes from Smoke with Lonely.

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/112837432