Why is it that InnoDB must have a primary key and it is recommended to use an auto-incrementing primary key?

1. The data structure of the InnoDB storage engine must require a primary key to be organized. If the user uses the InnoDB storage engine to create a table without specifying a primary key, Mysql will automatically help you find a suitable unique index as the primary key. If you find When there are no fields that meet the conditions of the unique index, a virtual column similar to ROW_ID will be generated as the primary key of the InnoDB table;

2. Integer storage is smaller than the field type, and it should be the InnoDB storage engine that uses the B+Tree data structure. When querying data, each element needs to be compared, and the comparison efficiency of integer is higher Other data structures, strings, etc.


Is there a problem with using an auto-incrementing primary key as the primary key of an InnoDB table?

Innodb_page_size = 16kb in Mysql, select BIGINT as the primary key to occupy 8b, and the address also occupies 8b. 16kb/(8+8)b = 1000 elements, which is a 1001-level B+Tree tree structure, then a new record is added each time New data is inserted in the rightmost data. When 1001 elements are inserted, a column change occurs to generate a new root node, and the number of elements contained in the left child node at this time is (1001-1)/2 =500, there will be no change after that, because the data inserted each time is on the far right side of the entire tree, and so on, you will find that nearly half of the node space is wasted. The following figure is an example of a 7th-order B+Tree:

Guess you like

Origin blog.csdn.net/a1_HelloWord/article/details/104341349