当前位置:网站首页>How MySQL optimizes the use of joint index ABC

How MySQL optimizes the use of joint index ABC

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

mysql I have talked about the classification and application of indexes before mysql Index application This article is mainly aimed at the joint index abc To carry out practical operation instructions .
Everybody wants to find employees The data package can be viewed from the above link blog .

One 、 Data background

Create a federated index abc
 Insert picture description here
Indexed type as well as length as follows :
 Insert picture description here

Two 、key-len Calculation rules for Columns

 Insert picture description here

3、 ... and 、SQL Perform the test

1)sql perform a,b,c Fields exist

1>abc perform

explain select * from employees where uid=4 and first_name=“Cristinel” and last_name=“Haddadi”
 Insert picture description here
type by ref, Prove that the index is gone ,key_len=42, We can calculate that .uid Of type by bigint, So is 8 Bytes .first_name and last_name Are all varchar The type is n+2, That is to say 14+2+16+2=34+8=42.

explain select * from employees where first_name=“Cristinel” and uid=“4” and last_name=“Haddadi”
 Insert picture description here

explain select * from employees where uid=“4” and last_name=“Haddadi” and first_name=“Cristinel”
 Insert picture description here
key_len All are 42, So we all went to the full index

2)sql perform a,b,c Some index fields are query criteria

1>ac perform

explain select * from employees where uid=“4” and last_name=“Haddadi”
 Insert picture description here We found that key_len=8, That is, only uid Indexes ,last_name The index is not valid .

2> ba perform

explain select * from employees where first_name=“Cristinel” and uid=“4” Here is the reference
We calculate key_len The calculation shows that ,14+2+8=24, He walked two indexes ,ab It's all used .

3) For conditions , Optimized index

I once met such a problem , Two query statements need to be satisfied
select * from A where a=a and b=b and c=c;
select * from A where a=a and c=c;
How to build an index ?

Well, if we continue a,b,c The statement is definitely not satisfied 2 It's all indexed . So let's change , Change it to a,c,b Indexes can meet the above two requirements .
 Insert picture description here
Pictured above , I reset an index a,c,b Pattern , Through the above test, we can determine that the above statement is satisfied .
1>a,b,c perform

explain select * from employees where uid=“4” and first_name=“Cristinel” and last_name=“Haddadi”
 Insert picture description here

2>a,c perform

explain select * from employees where uid=4 and last_name=“Haddadi”
 Insert picture description here
key_len:16+2+8=26, Indexes are used .

Four 、 summary

Leftmost prefix principle , From the above we can conclude that , As long as the fields of join and index participate in the query , So whatever the sequence , All of them can be indexed directly .
If there are too many indexes in the table , It will affect INSERT And UPDATE performance , Simply put, it will affect the data writing performance . Because while updating the data , Also update the index at the same time .
So it can complete the work with an index , It is definitely not recommended to establish two .

原网站

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