Why use a graphical comparison of MySQL index B + tree

Mentioned index, the first reaction is certainly able to improve query efficiency. Such as a directory of the book, you want to find a chapter, will start positioning directory. If no directory, then you need to read through everything to find.

Performance is critical to the design process of the index, if the index is too small, the query performance is affected; and if the index is too much, it will affect the increase / change the performance / delete other.

Knowledge Point

MySQL generally supports the following common index:

  • B + tree index
  • Full-text index
  • Hash indexes

Today, we focus on speaking under the B + tree index, and why to use as a B + tree data structure of the index.

B + tree index does not directly find a specific row, just look for the line to be found where the page, and then by the full-page DB into memory, and then look at the memory.

Review the data structure

1.1 Hash structure

If eight data 3,1,2,10,9,0,4,6 established hash index as shown in Figure 1-1.

  • Direct inquiries: Find now the number 6 from 8 this record, only need to calculate the hash value of 6, you can quickly locate records, time complexity is O (1).
  • Range query: If you want to query range (greater than 4 data), then this index is completely useless.
Why use a graphical comparison of MySQL index B + tree

1.2 binary search tree

Is a classic binary tree data structure, requires less than the left subtree root node, the root node is greater than the right subtree.

If eight data 3,1,2,10,9,0,4,6 establish shown in figure II 1-2 search tree.

  • Direct Query: Suppose record lookup keys 6, to find the root 4,4 <6, so look for the right subtree 4 to find 9; 9 is greater than 6, so look for the left subtree 9; total of 3 times to find. But if the sequential search, you need to find eight times (on the last).
  • Range query: If you need to find data more than 4, then traverse the right subtree 4 on the line
Why use a graphical comparison of MySQL index B + tree

 

1.3 balanced binary tree (AVL trees)

By definition a binary search tree, which can be of any configuration, these same numbers may be established in accordance with a binary search tree in Figure 1-3-1. 6 Also look for data, you need to find five times.

Why use a graphical comparison of MySQL index B + tree

FIG 1-3-1 poor performance of a binary search tree

For maximum performance and therefore to construct a binary search tree, it needs to be balanced, that is balanced binary tree.

Balanced binary tree is defined: First, meet the definition of a binary search tree, the other two subtrees of any node maximum height difference is 1.

Balanced binary tree query speed is very fast, but there are drawbacks:

  1. Maintenance cost of the tree is very large, during insert or update, often requiring multiple left or right to maintain balance. FIG 1-3-2
  2. The amount of data and more, the tree will be high, requiring multiple I / O operations.
  3. Range during search, assuming look> = 3, 3 to find, and the need to find a parent node 3, and then traverse right subtree of the parent node.

Why use a graphical comparison of MySQL index B + tree

 

1.4 B + tree

In the B + tree, all the nodes stored records on the leaf nodes, and is kept in order to connect each leaf node pointer. If the traversal order from the leftmost leaf node, the order can get all sort keys.

If eight data 3,1,2,10,9,0,4,6 can create highly 1-4-1 shown in FIG. 2 is a B + tree.

 

Why use a graphical comparison of MySQL index B + tree

FIG  1-4-1 height B + tree 2

 

During updating, B + tree also need similar rotational operation of the binary tree. For example, assume a new 7, it can be filled directly behind 4,6. If you add 8, then it needs to be rotated, following feel during rotation of the B + tree.

 

Why use a graphical comparison of MySQL index B + tree

FIG 1-4-2 height B + tree 3

 

The advantage of using the index structure of the B + tree:

  1. B + tree height is generally 2-4 layers, so that a maximum of only 2-4 times IO find records, relatively balanced binary tree has been greatly reduced.
  2. The range of search, by acquiring the data pointer to leaf nodes. Equal to the data such as finding 3, 3:00 as found in leaf nodes, all of the data acquired by the tail pointer 3 will be able, without the need to re-acquired as a binary tree as the parent node 3.

This should see the index mysql understand why the use of B + tree, right

Guess you like

Origin www.cnblogs.com/CQqf2019/p/10929449.html