当前位置:网站首页>What happens when the MySQL union index ABC encounters a "comparison operator"?

What happens when the MySQL union index ABC encounters a "comparison operator"?

2022-06-13 05:47:00 Coffee is not bitter**

The last part tells about abc Joint index optimization , details :mysql How to optimize a federated index abc Use
This paper mainly studies the joint index abc Operator encountered “> or < or !=” What will happen? ?

One 、 Data background

The index of this article is as follows :
 Insert picture description here
Field corresponds to type and length as follows :
 Insert picture description here

Two 、 When union index abc encounter “>” perhaps “<” Symbol

① a=a b Greater than or less than b c=c

explain select * from employees where uid=1 and last_name>“2” and first_name=“2”
 Insert picture description here
The top link has a pair of ken_len Calculation method of , this explain,key_len=26
Let's calculate :uid by bigint type , So take 8 Bytes ;last_name and first_name by varchar, And because it's a letter , So it is n+2,last_name=16+2=16,first_name=14+2=16

If we calculate the sum, we can find , This situation only goes away a and b Indexes . How to optimize the index ? It must be changed to acb Chant . Of course, how to use indexes

② a=a b=b c Greater than or less than c

explain select * from employees where uid=1 and last_name=“2” and first_name>“2”
 Insert picture description here
In this case, it is the full index .

③a Greater than or less than a b=b c=c

explain select * from employees where uid>1 and last_name=“2” and first_name=“2”

 Insert picture description here

Index failed

3、 ... and 、 When union index abc encounter != Symbol

Encounter this symbol , Will it invalidate the entire index ? In the process of project development , In fact, the most used is in exists To realize relevant functions , Rumor has it that != Will invalidate the entire index , Let's test it and see :

①a=a b!=b c=c

explain select * from employees where uid=1 and last_name!=“2” and first_name=“2”
 Insert picture description here

②a=a b=b c!=c

explain select * from employees where uid=1 and last_name=“2” and first_name!=“2”
 Insert picture description here

③a!=a b=b c=c

explain select * from employees where uid!=1 and last_name=“2” and first_name=“2” Here is the reference
Look at the situation != Does not invalidate the entire index .

Four 、 summary

Encountered comparison operator , When abc Of a yes ><!= Three ways , Will invalidate the entire index , Other situations will only lead to partial index invalidation . In the daily development process , When creating an index, you should pay attention to .

原网站

版权声明
本文为[Coffee is not bitter**]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507384447.html