MySQL----index usage rules


1 The leftmost prefix rule

If multiple columns are indexed (joint index), the leftmost prefix rule must be obeyed.

The leftmost prefix rule means that the query starts at the leftmost column of the index and does not skip columns in the index.

If you skip a column, the index will be partially invalid (the subsequent field index will be invalid).

show index from tb_user;

Please add image description
The leftmost prefix rule means that when querying, the leftmost column, that is, the profession, must exist, otherwise the index will all fail. And a column cannot be skipped in the middle, otherwise the field index behind the column will be invalid.

Next, let's demonstrate several groups of cases and take a look at the specific execution plan:
query by rules

explain select * from tb_user where profession = '软件工程' and age = 31 and status = '0';

Please add image description
field index

explain select * from tb_user where profession = '软件工程' and age = 31;

Please add image description


explain select * from tb_user where profession = '软件工程';

Please add image description
In the above three sets of tests, we found that as long as the profession on the leftmost field of the joint index exists, the index will take effect, but the length of the index is different. And from the above three sets of tests, we can also infer that the index length of the profession field is 47, the index length of the age field is 2, and the length of the status field index is 5.

explain select * from tb_user where age = 31 and status = '0';

Please add image description
The field index length is null, and the index does not take effect. The reason is that because the leftmost prefix rule is not satisfied, the leftmost column profession of the joint index does not exist.

explain select * from tb_user where profession = '软件工程' and status = '0';

Please add image description
There is a profession field, the leftmost column exists, and the index satisfies the basic conditions of the leftmost prefix rule. However, when querying, the age column is skipped, so the subsequent column index will not be used, that is, the index part takes effect, only the profession takes effect, so the length of the index is 47.

3.1 Change the field order, whether the leftmost prefix rule takes effect

 explain select * from tb_user where age = 31 and status = '0' and profession = '软件工程'

Please add image description
If the leftmost prefix rule is satisfied, the index length is 54, and the joint index is valid.

The leftmost column in the leftmost prefix rule means that when querying, the leftmost field (that is, the first field) of the joint index must exist, regardless of the order in which conditions are written when we write SQL.

2 Range query

In the joint index, a range query (>,<) occurs, and the column index on the right side of the range query is invalid.

explain select * from tb_user where profession = '软件工程' and age > 30 and status = '0';

Please add image description

When the range query uses > or <, the joint index is used, but the length of the index is 49, which means that the status field on the right side of the range query is not indexed.

explain select * from tb_user where profession = '软件工程' and age >= 30 and status = 0';

Please add image description
When the range query uses >= or <=, the joint index is used, but the length of the index is 54, which means that all fields are indexed.

Because there is equals, with equals, the records of equals can be queried, greater than as long as you continue to query on the basis of equals, compared to greater than, there are definite records.

Guess you like

Origin blog.csdn.net/m0_53022813/article/details/124373617