Mysql leftmost matching principle and index heap

Foreword:

The basis of the leftmost match is the joint index

joint index

A joint index is also called a conforming index, which is suitable for multi-condition queries .
Simply put, it is an index built on multiple columns, and the joint index is also sequential.
As shown in the figure below, a composite index is built with columns A and B, and a certain level of nodes records the index (5, 8) to (8, 3). This value may come from the hash map. It may also represent a certain meaning in itself.
insert image description here
InnoDB will use the primary key index B+ tree to maintain indexes and data files, and the joint index (A, B) will also generate a B+ tree. For the created index, as shown in the figure on the right, the internal sorting of the index built from left to right is first sorted by A, and then sorted by B when the values ​​of A are equal .
So there will be the following rules:

  1. Because A is ordered, using B=1, or simply using B as the query condition cannot use the index, because the joint index is based on the leftmost matching principle, and the index is built from the left A.
  2. When the leftmost matching principle meets the range query, it will stop. For example, A=1, B=2, both A and B can use the index, but A>1 and B = 2, A can use the index, but B cannot, because the value of A is a range, and B in this range is out of order. So the index will be invalid.
  3. When the A values ​​are equal, the B values ​​are sorted by value. This order is local, but A > 1 and B = 2, the A field can match the index, but the B value cannot, because the value of A is a range, and B is unordered in this range.

The joint index query process
InnoDB will use the primary key index, and the joint index (A, B) will also generate a B+ tree, but
the data of the joint index B+ tree stores the primary key value of the row where the joint index is located .
So **relates back to the table*.

leftmost matching principle

The leftmost is first, and any continuous index starting from the leftmost can be matched, but when a range query is encountered, the matching will stop . This is related to the construction method of the joint index, that is, the storage structure.
The short answer is that if you ask about the leftmost match , you will talk about the construction rules of the joint index . · If you check B=2, you can also use the index. The type field in the result of explain() is an index, which is different from all in that it only traverses the index tree, which is also faster than the simple full table scan, but far inferior to the above three, and the above type field is ref.



heap under index

Requirement: Retrieve the first character of the name in the table is Zhang, and there is no deleted information (is_del = 1)
select * from t_user where name like '张%' and is_del=1
Before mysql5.6, non-clustered or joint indexes also belonged to non-clustered indexes, and the corresponding value of the index B+ tree node found at last is only the primary key of a certain row, so it is necessary to search according to the current primary key. This process is to return to the table. The heap under the index can solve the problem of returning to the table of the joint index.
In fact, for this kind of fuzzy query at the beginning of Zhang, after all the Zhangs are found, the data of each Zhang will be returned to the table once. Using the index heap, it will first check whether the index field contains the field in the query process. If it does not match, it will be removed first, thus reducing the number of table returns.

Guess you like

Origin blog.csdn.net/myscratch/article/details/131871254