Why InnoDB tables must have primary keys, and it is recommended to use integer auto-increment primary keys?

InnoDB index file and data file are together
frm: table structure; ibd: index file and data file
Insert picture description here
Insert picture description here
Q: Why InnoDB table must have a primary key. And it is recommended to use an integer auto-incrementing primary key:

1. If the primary key is set, InnoDB will select the primary key as the clustered index. If the primary key is not explicitly defined, InnoDB will select the first unique index that does not contain NULL values ​​as the primary key index. If there is no such unique index, Then InnoDB will choose the built-in 6-byte long ROWID as the implicit clustered index (ROWID increases with the writing of the row record and the primary key).

2. If the table uses an auto-incrementing primary key,
then every time a new record is inserted, the record will be sequentially added to the subsequent position of the current index node, and the order of the primary key will be arranged in the order of insertion of the data record, automatically ordered. When a page is full, a new page will be opened automatically

3. If you use a non-incremental primary key (such as an ID card number or student number, etc.),
since the value of the primary key inserted each time is approximately random, each new record must be inserted into a certain position in the middle of the existing index page. Sometimes MySQL has to move the data in order to insert the new record into the appropriate position, 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, which adds a lot of overhead. At the same time, frequent movement and paging operations caused a lot of fragmentation, resulting in a less compact index structure, and then had to use OPTIMIZE TABLE to rebuild the table and optimize the page filling.

Question: Why does the leaf node of the non-primary key index structure store the primary key value:

Reduce the maintenance work of the secondary index when the row moves or the data page is split (when the data needs to be updated, the secondary index does not need to be modified, only the clustered index needs to be modified, a table can only have one clustered index, and other All are secondary indexes, so only the clustered index needs to be modified, and the secondary index does not need to be rebuilt)

A clustered index is also called a primary key index. The leaf nodes of the index tree store the entire row of data. The physical order of the rows in the table is the same as the logical (index) order of the key values. A table can only contain one clustered index. Because the index (directory) can only be sorted in one way.

The content of the leaf node of a non-clustered index (ordinary index) is the value of the primary key. In InnoDB, non-primary key indexes are also called secondary indexes.


More:

BTree is also called multi-way balanced search treeInsert picture description here
Insert picture description here

The
frm separated from myisam index file and data file : table structure; MYD: data file; MYI: index file
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_41699562/article/details/104139458