Index 6: leftmost matching principle of joint index

Leftmost matching principle of joint index
What is the leftmost matching principle?
The left-most priority to the far left is the starting point of any continuous indexes can be matched. Meanwhile encountered range queries (>, <, between, like ) will stop match.
Here Insert Picture Description
Examples index column A and column B to establish joint index index (A, B) (A , B ordered sequence)
explain select X from XX where A = '' and = B '' go ab index
explain select X from XX where A = 'walking ab index
explain select X from XX where B = ' ' does not go ab index, which is a type ALL take full table scan.
Here Insert Picture Description
: Leftmost matching principle principle
rule mysql create a composite index: composite index would be the first index (ie, the left side of the first index to sort) and then the second index fields to be sorted. A similar order by 1 || and order by 2 , thus ensuring Field 1 is absolutely in order. Field 2 is not guaranteed, according to the rules of the MySQL query optimization, will first look to reduce the time ordered according to the index. The resulting leftmost matching principle.
Here Insert Picture Description
According to the leftmost match is shown in principle similar to a B + tree reduction
according to col3, col2 establish joint index, such as finding alice B + tree in the process, find keywords ALice queries to the leaf node alice, alice presence of multiple cases, based on col2 value is determined.

Here Insert Picture Description
Can see the value of a is sequential, 1,1,2,2,3,3, and the value b is not order 1,2,1,4,1,2. B = 2 so that the query is no way to use the index, because the joint index is based on a first ordered, b is disordered.

We also found that in the case of a value equal to, b value is in sequential order, but this order is opposite. So leftmost matching principle encounter range query will stop, and the rest of the field can not use the index. For example a = 1 and b = 2 a , b fields can use the index, because in a case where b is a value determined relatively ordered, and a> 1and b = 2, a field can be matched to the index, but the value of b No, because a value of a range of b in this range is disordered.
1. The range of values matching

select * from table_name where a> 1 and a <3
may be a range query on the leftmost column

select * from table_name where a> 1 and a <3 and b> 1;
a plurality of columns at the same time the range of search, only the index of the leftmost column range searches only B + tree index is used, which is only used in a index in the range of 1 <a <3, b is disordered, the index can not be used, after recording 1 <a <3 found, only depending on the conditions b> 1 by one filter to continue

2. exact matches a column and a further matching range

If the left column is exactly looking for, the right column to find the range may be

select * from table_name where a = 1 and b> 3;
in the case of a = 1 b are ordered, range searches an index taking the joint
3. leftmost matching
index (A, B, C)

B * from table_name WHERE SELECT = '2'
SELECT * WHERE from table_name = C '. 3'
SELECT * WHERE from table_name = B '. 1' and C = '. 3'
which do not start from the left, did not use the last query index, using a full table scan

select * from table_name where a = ' 1' and c = '3'
if not continuous, only used the column index of a, b and c columns are not used column
reference
https://blog.csdn.net/sinat_41917109 / Article This article was / the Details / 88.94429 million
https://blog.csdn.net/qq_27559331/article/details/89632566

Released seven original articles · won praise 19 · views 3475

Guess you like

Origin blog.csdn.net/qq_41627514/article/details/104299592