A question on the principle of mysql joint index

Start the analysis from an interesting topic:

Suppose a table has a joint index (c1, c2, c3, c4) The following options which fields use the index:
A where c1=x and c2=x and c4>x and c3=x
B where c1=x and c2 =x and c4=x order by c3
C where c1=x and c4= x group by c3,c2
D where c1=? and c5=? order by c2,c3
E where c1=? and c2=? and c5=? order by c2,c3

Let’s get started:

First create the table:

CREATE TABLE t(
c1 CHAR(1) not null,
c2 CHAR(1) not null,
c3 CHAR(1) not null,
c4 CHAR(1) not null,
c5 CHAR(1) not null
)ENGINE myisam CHARSET UTF8;

There are 5 fields from c1 to c5. In particular, the field types are all fixed-length char(1) types, and are not empty, and the character set is utf8 (related to the number of bytes used in the calculation of the index)

Create index:

alter table t add index c1234(c1,c2,c3,c4);

Insert 2 pieces of data: insert into t VALUES('1','1','1','1','1'),('2','2','2','2','2 ')

Use MySql Explain to start analyzing the result of the question:

Option A:

The result can be seen, c1, c2, c3, c4 all use the index, and we make a slight change to the A result:

After removing the c2 condition:

According to the leftmost principle of the index, the c2 field does not use the index, and the fields after c2 cannot use the index. In the following 2 figures, we compare the leftmost principle of the index:

The result of the above figure shows that the direct use of c3 is a full table query, and the index cannot be used. Therefore, the premise of using the index for the c3 field is that both fields c1 and c2 use the index.

It is the leftmost principle of the index (the left prefix principle).

Option B:

The length of key_len indicates that the index is used in the c1 and c2 fields. Extra shows that the temporary table is not used for sorting, indicating that the sorting is using the index, but it is not calculated in the key_len value, and it does not play a role in connecting c4, indicating the index To c3 here is broken.

Sorting is actually done directly using the joint index, that is: the c1234 joint index is used, so that c1 under c2, c2 under c3, and c3 under c4 are ordered, so the actual sorting uses the index, and the c3 field is not The index is not used. (I always feel a little awkward when I write this paragraph, I don’t know if I understand it right or not, it needs further research)

C option:

Use group by to generate temporary files first, and then sort them, but when the field order is c2, c3, the temporary table is not used for sorting, but the index is sorted; when the group by field is c3, c2, because of The order of the index fields is inconsistent, so the index is not used for grouping and sorting.

Determined by the length of key_len, only one field of c1 uses an index.

D option:

Order by is similar to group by. When the field order is consistent with the index, the index sort will be used; when the field order is inconsistent with the index, the index will not be used.

Determined by the length of key_len, only one field of c1 uses an index.

E option:

In fact, the result analysis of option E has been analyzed in the above ABCD results, here only the c1 and c2 fields use this index.

To sum up the answers to the above questions:

A: The index is used in all four fields

B: The index is used in the c1 and c2 fields

C: C1 field uses this index

D: C1 field uses this index

E: c1, c2 fields use the index


to sum up:

The leftmost principle of the index (left prefix principle), such as the joint index of (c1,c2,c3,c4...cN), where conditions are used in the order of the fields created by the index (does not mean that the and conditions must be written in order ), if there is no condition for a column in the middle, or the use of like will cause the following column to not use the index.

Indexes can also be used for grouping and sorting, grouping must be sorted first, calculating the average value, and so on. Therefore, in grouping and sorting, if the order of the fields can be in accordance with the order of the fields of the index, the ordering characteristics of the index can be used.
 

Guess you like

Origin blog.csdn.net/liyang_nash/article/details/104686424