Home  >  Article  >  Database  >  Why does Mysql InnoDB add an auto-incrementing primary key that has nothing to do with the business?

Why does Mysql InnoDB add an auto-incrementing primary key that has nothing to do with the business?

高洛峰
高洛峰Original
2016-12-02 13:26:131409browse

In Mysql table design, an auto-increment column that has nothing to do with the business is usually used as the primary key.
This is because Mysql uses B-Tree index by default, which you can simply understand as "sorted fast search structure".
The following is the structure diagram of a B-Tree, a 2-layer B+ tree, the fan-out of each page is 4; and there are five records from 1 to 6; the upper record saves the minimum value of each page; each page is linked through a doubly linked list Up;

Why does Mysql InnoDB add an auto-incrementing primary key that has nothing to do with the business?When you insert record 7, page splitting will occur:

Why does Mysql InnoDB add an auto-incrementing primary key that has nothing to do with the business?

As can be seen above, splitting produces record movement, but the optimized split operation does not require record movement:

Why does Mysql InnoDB add an auto-incrementing primary key that has nothing to do with the business?

In InnoDB In the implementation, a position of the last insertion is maintained for each index page, as well as an increment/decrement identifier of the last insertion. Based on this information, InnoDB can determine whether the newly inserted record into the page still satisfies the increment/decrement constraints. If the constraints are met, the optimized splitting strategy will be adopted;
So it is recommended to use a column of sequentially increasing IDs as the primary key. But it doesn’t have to be the autoincrement field of the database, as long as it satisfies the sequential increase. Many large applications will have sequentially increasing ID generators.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn