Index type B-Tree index

I. Introduction

An index is a data structure used in a storage engine to quickly find records . There are many types of indexes that can provide better performance for different scenarios. In MySQL, indexes are implemented at the storage engine layer rather than at the service layer. Here is an introduction to commonly used B-Tree indexes .

2. B-Tree index

1 Introduction

When people talk about indexes, if they don't specify the type, they're probably referring to B-Tree indexes . B-Tree indexes
are used in different ways on storage engines , with different performances and advantages and disadvantages. For example, MyISAM uses prefix compression technology to make the index smaller, but InnoDB stores the data in the original data format . Another example is that the MyISAM index refers to the indexed rows through the physical location of the data , while InnoDB refers to the indexed rows based on the primary key .

2. Structure of B-Tree on InnoDB

B-Tree usually means that all values ​​are stored in order , and each leaf is the same distance from the root .

Figure 1 shows the structure of the B-Tree tree. Each layer of nodes will carry data.
Insert image description here

Figure 2 shows the instantiated B+Tree. Only leaf nodes will carry data.
Insert image description here

There are many articles about the difference between B-tree and B+ tree. Here are just a few obvious differences.

  1. B+ tree is a variant of B-tree and is also a multi-way search tree, and all leaf nodes are on the same level.
  2. B+Tree adds a link pointer to all leaf nodes, which supports range retrieval.
  3. In B+Tree, only leaf nodes store data, and internal nodes only store index values.

B-Tree indexes can speed up access because the storage engine no longer needs to perform a full table scan to obtain data, but instead searches from the root node of the index . The root node stores pointers to leaf nodes, and the storage engine searches for lower layers based on these pointers. By comparing the value of the node page with the value you are looking for, you can find appropriate pointers into the lower child nodes. These pointers actually define the upper and lower bounds of the values ​​in the child node page .
(In Mysql, although its index type is BTREE, the data structure uses B+Tree)

3. Case description

Table creation statement:

CREATE TABLE `People` (
  `last_name` varchar(50) NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `dob` date NOT NULL,
  `gender` enum('m','f') NOT NULL,
  KEY `key` (`last_name`,`first_name`,`dob`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

We inserted some data. For each row of data in the table, the index contains the value of the last_name, first_name, dobcolumn. The following figure shows how the data is stored.
Insert image description here

B-Tree indexes are suitable for full key value , key value range or key prefix lookup . The key prefix search only applies to searches based on the leftmost prefix .

  • Full value matching
    Full value matching refers to matching against all columns in the index. For example, the miniature mentioned above can be used to find people named Cuba Allen, born on 1960-01-01.

  • Match the leftmost prefix
    As mentioned earlier, the index can be used to find all people with the last name Allen, that is, only the first column of the index is used.
    You can also match only the beginning of a column's value. For example, as mentioned earlier, the index can be used to find all people with a surname starting with J. Here, only the first column of the index is used.

  • Matching range values
    ​​For example, the index mentioned above can be used to find all people whose last name is Allen and whose first name starts with the letter K (such as Kim, Karl, etc.). That is, the first column last_name matches completely, and the second column first_name matches the range.

Tips:

  1. If you do not search by the leftmost prefix, you cannot use the index. For example, you cannot find people whose first_name is Bill, nor can you find people with a specific birthday. Similarly, there is no way to find people whose last name ends with a certain letter
  2. Columns in the index cannot be skipped. For example, if you want to find a person whose last name is Smith and who was born on a specific date, if you do not specify a name (first_name), Mysql can only use the first column of the index.
  3. If there is a position query for a column in the search, all columns to the right of it cannot use index optimization to search. For example, there is a query Where last_name='Smith' And first_name Like 'J%' And dob = '1976-12-23'. This query can only use the first two columns of the index, because Like here is a range condition.

Guess you like

Origin blog.csdn.net/TheWindOfSon/article/details/135346724