当前位置:网站首页>Ordering of MySQL composite index

Ordering of MySQL composite index

2022-06-24 08:40:00 An unreliable programmer

Yesterday, a colleague made a statement about the military regulations mysql Consult me about the index , I found that I didn't understand the rules of composite index . So let's make a note :

【 recommend 】 If there is order by Scene , Note the use of index order .order by The last field is part of the composite index , And put it at the end of the index combination order , Avoid file_sort The situation of , Affect query performance .
Example :where a=? and b=? order by c; Indexes :a_b_c
Counter example : Range lookup in index , Then index order cannot be utilized , Such as :WHERE a>10 ORDER BY b; Indexes a_b Cannot sort .

explain :order by The sorting principle of

1. Use the order of the index to obtain ordered data
2. Using memory / Disk file sorting results
1) Two way sorting : First, the corresponding sorting fields and row pointer information that can directly locate row data are extracted according to the corresponding conditions , And then in sort buffer In order .
2) One way sorting : Is to retrieve all fields of the qualified row at one time , And then in sort buffer In order .

The orderliness of composite index and the leftmost prefix principle

【 mandatory 】 Understand the leftmost prefix principle for composite indexes , Avoid duplicate index construction , If you set up (a,b,c), It's kind of set up (a), (a,b), (a,b,c)

Suppose there's an index (A,B)
mysql The rule for creating a composite index is that the leftmost part of the composite index is first , That's the first one A Sort the data of the field , Based on the sorting of the first field , And then to the second one B Field to sort . In fact, it is equivalent to the realization of similar order by A B Such a sort rule .
first A Fields are absolutely ordered , The second field is out of order . So usually , Use the second one directly B There is no index for conditional judgment of fields
So when can I use it ?
Certainly B The index data of fields can only be used when they are in order .
When will it be orderly ?
Only in A If the field is equivalent matching ,B It's order .

Various scenarios for combining index queries

Yes Index (A,B,C) —— Multiple fields of composite index are ordered , And it is a complete BTree Indexes .

The following conditions can be used for the combined index query :

A>5

A=5 AND B>6

A=5 AND B=6 AND C=7

A=5 AND B IN (2,3) AND C>5

The following conditions will not apply to combined index queries :

B>5 —— The query condition does not contain the first column field of the composite index

B=6 AND C=7 —— The query condition does not contain the first column field of the composite index

The following conditions will be able to use the upper part of the combined index query :

A>5 AND B=2 —— When a range query uses the first column , Query criteria can only use the first column

A=5 AND B>6 AND C=2 —— The range query uses the second column , Query criteria can only use the first two columns

Various scenarios of combined index sorting

There's a composite index Index(A,B).

The following conditions can be used to sort by combined index :

ORDER BY A—— First column sort

A=5 ORDER BY B—— After the first column is filtered, the second column is sorted

ORDER BY A DESC, B DESC—— Be careful , At this point, the two columns are sorted in the same order

A>5 ORDER BY A—— Data retrieval and sorting are in the first column

The following conditions cannot be used to sort by combined index :

ORDER BY B —— Sort in the second column of the index

A>5 ORDER BY B —— The range query is in the first column , Sort in second column

A IN(1,2) ORDER BY B —— For the same reason

ORDER BY A ASC, B DESC —— Be careful , At this point, the two columns are sorted in different order

Suggest

If you have any doubts about using the index, you can finish it sql in the future use explain Let's run it sql
Can be more conducive to understanding sql Implementation process of

原网站

版权声明
本文为[An unreliable programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240612375736.html