"MySQL Series - InnoDB Engine 33" Index and Algorithm - B+ Tree

B+ tree

B+ tree, like binary tree and balanced binary tree, are classic data structures. B+ tree consists of B tree and index sequential access method (ISAM, which is also the data structure that the MyISAM engine initially refers to), but in actual use, there is almost no use of B tree.

A B+ tree is a balanced search tree designed for disks or other direct access secondary devices. In the B+ tree, all record nodes are leaf nodes stored in the same layer in the order of the key value, and are connected by pointers to each leaf node. First look at a B+ tree with a height of 2, 4 records per page, and a fan-out of 5.

As shown in the figure below, all records are on the leaf nodes and stored sequentially. If the user traverses from the leftmost leaf node, the key values ​​obtained are: 5, 10, 15, 20, 25, 30, 50, 55 , 60, 65, 75, 80, 85, 90.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-tW48kXa0-1679807421730)(017.png)]

1 Insert operation of B+ tree

The insertion of the B+ tree must ensure that the records of the leaf nodes are still in order after insertion. At the same time, three cases of insertion into the B+ tree must be considered. Each case may lead to a different insertion algorithm:

Leaf Page full Index Page is full operate
No No Insert records directly into leaf nodes
Yes No 1. Split the leafPage 2. Put the middle node on the index page 3. Put the records smaller than the middle node on the left 4. Put the records greater than or equal to the middle node on the right
Yes Yes 1. Split the leafPage 2. Put the records smaller than the middle node on the left 3. Put the records greater than or equal to the middle node on the right 4. Split the indexPage 5. Put the records smaller than the middle node on the left 6. Put the records larger than the middle node on the right 7. Put a layer of indexpage on the middle node

2 Deletion operation of B+ tree

The B+ tree uses the filling factor to control the deletion of the tree, and 50% is the minimum value that can be set for the filling factor. The deletion operation of the B+ tree must also ensure that the records in the leaf nodes are still sorted after deletion. Like the insertion, the deletion operation of the B+ tree also needs to consider the following three situations. Unlike the insertion, the deletion is measured according to the change of the filling factor:

The leaf node is smaller than the fill factor Intermediate nodes are smaller than fillfactor operate
No No Delete the record directly from the leaf node, if the node is still an index page node, replace it with the right node of the node
Yes No Merge the leaf node and its sibling nodes and update the index page at the same time
Yes Yes 1. Merge the leaf node and its sibling nodes 2. Update the index page 3. Merge the index page and its sibling nodes

Guess you like

Origin blog.csdn.net/m0_51197424/article/details/129778339