"MySQL Series - InnoDB Engine 32" Indexes and Algorithms - Data Structures and Algorithms

Data Structures and Algorithms

The B+ tree index is the most common and the most frequently used index in the database.

1 Binary search method

The binary search method (binary search), also known as the binary search method, is used to search for a certain record in a group of ordered record arrays. First take the midpoint position of the ordered sequence as the comparison object. If the value of the element to be searched is smaller than the midpoint element, the sequence to be searched is reduced to the left half, otherwise it is the right half. Through a comparison, the search interval is reduced by half.

If there are 10 numbers of 5, 10, 19, 21, 31, 37, 42, 48, 50, and 52, and now you want to find the record 48 from these 10 numbers, the search method is as follows:

insert image description here

As shown in the figure above, the number 48 can be found in 3 times. If it is a sequential search, it needs 8 times. Therefore, the efficiency of the binary search method is relatively better than the sequential search method. For example, for the above ten numbers, if the sequential search method is used to search separately, they are respectively 1+2+3+4+5+6+7+8+9+10=55次, that is, the average needs (1+2+3+4+5+6+7+8+9+10)\10=5.5次. If it is a binary search method, respectively 4+3+2+4+3+1+4+3+2+3=29次, that is, the average need (4+3+2+4+3+1+4+3+2+3)\10=2.9次. So in terms of the average number of searches, the binary search method is better. And in the worst case, the sequential search method needs 10 times, and the binary search method only needs 4 times.

The application of binary search method is extremely wide, and its idea is easy to understand. The first binary search method appeared in 1946, but the first fully correct binary search method did not appear until 1962. In the InnoDB engine of the database, the slots in the Page Directory of each page are stored in the order of the primary key, and the query for a specific record is obtained by performing a binary search on the Page Directory.

2 Binary search tree and balanced binary tree

Before introducing the B+ tree, you need to understand the binary search tree first. The B+ tree is evolved from the binary search tree, and then from the balanced binary tree and the B tree. A binary tree is a classic data structure.

In a binary search tree, the key value of the left subtree is always less than the key value of the root, and the key value of the right subtree is always greater than the key value of the root.

insert image description here

As shown in the figure below, it is also a binary tree, but this binary tree is a less efficient one.

insert image description here

If you want to construct a binary tree with maximum performance, you need the binary tree to be balanced, which leads to a new definition-balanced binary tree, or AVL tree.

Definition of a balanced binary tree:

First, the definition of a binary search tree, and second, the maximum difference in height between the two subtrees of any node must be 1

The query speed of a balanced binary tree is indeed very fast, but the cost of maintaining a balanced binary tree is very high. Generally speaking, one or more left or right rotations are required to get the balance after insertion or update. However, balanced binary trees are mostly used in memory structure objects, so the maintenance overhead is relatively small.

Example 1:

On the basis of the balanced binary tree, add another piece of data, in order to maintain the status quo of the binary tree, just perform a left rotation.

insert image description here

Example two:

As shown in the figure below, on the basis of a balanced binary tree, after adding a piece of data, it requires multiple rotations to achieve a balanced binary tree.

insert image description here

Guess you like

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