Database -> table index why use primary key + integer + auto-increment

Why do table indexes use primary key + integer + auto-increment

  • Needless to say, the primary key is a question of query efficiency

integer + auto increment

  • The current MySQL is the data engine of InnoDB, and the bottom layer is the B+ tree
  • InnoDB must have a primary key, because 整个B+树都是靠这索引来维护the entire table cannot be built without an index
  • Of course, we don’t take the initiative to add the primary key index, and InnoDB will add it by default
    • Letting InnoDB add its own primary key will affect efficiency even more, either it chooses a field for indexing, or adds an additional field for indexing
    • It needs one, so why don't we set it ourselves, and there will be other efficiency problems mentioned above
  • The bottom layer of the B+ tree is also 二分法来对树进行一层一层的查找used
  • Then the index uses an integer method, which is definitely better than using uuid , because the dichotomy compares the size int肯定比String比较要效率搞
    • And the memory required by uuid will be larger than that of int
  • The bottom layer of the B+ tree will maintain the order of the index . We will increase the index in the order of 123456, so the index will only be added at the end.
  • If we don't follow the self-increment method , the index will suddenly change from the index 中间插入, and the mobile index structure will 改变已经完成的索引顺序have to be readjusted

So when creating a table, please use primary key + integer + self-increment

Guess you like

Origin blog.csdn.net/rod0320/article/details/123488975