Talking about Mysql primary key from the developer's point of view

Reprinted from the developer's point of view on Mysql primary key

say it up front

Zero MySQL has always been relatively weak. As the saying goes, programmers who don’t know MySQL are not good programmers. I just met the mysql Daniel Liu Luliu, and just happened to have these articles, mainly written by Liu Lu and Liu Daniu. After modifying the text, Zero thinks that this series of articles is very good. You can take a look at mysql from the perspective of developers.

theme

  • The benefits of using auto-incrementing primary keys

  • Disadvantages of using non-auto-incrementing primary keys

  • Summarize

The benefits of using auto-incrementing primary keys

Then 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 opened automatically.

Disadvantages of using non-auto-incrementing primary keys

Since the value of the primary key inserted each time is approximately random, each new record must be inserted somewhere in the middle of the existing index page. At this time, MySQL has to move the data, or even the target, in order to insert the new record into the proper position. The page may have been written back to the disk and cleared from the cache, and then read back from the disk, which adds a lot of overhead. Index structure, and then have to rebuild the table through OPTIMIZE TABLE and optimize the fill page.

Summarize

If the data writing order of the InnoDB table can be consistent with the order of the leaf nodes of the B+ tree index, then the access efficiency is the highest. That is, the following situations have the highest access efficiency:

  • Use an auto-incrementing column (INT/BIGINT type) as the primary key. At this time, the writing order is auto-incrementing, which is consistent with the splitting order of B+ number of leaf nodes;

  • The table does not specify an auto-incrementing column as the primary key, nor does it have a unique index that can be selected as the primary key (the above conditions). At this time, InnoDB will select the built-in ROWID as the primary key, and the writing order is consistent with the ROWID growth order;

  • If an InnoDB table does not display a primary key, and there is a unique index that can be selected as the primary key, but the unique index may not be an incremental relationship (such as string, UUID, multi-field joint unique index), the table access Efficiency will be poor.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325580605&siteId=291194637