当前位置:网站首页>MySQL composite index is not used by hits

MySQL composite index is not used by hits

2022-06-10 23:09:00 Li_ XiaoJin

I have this problem today , I haven't thought about it before , Make a note of .

1. preparation

First create a table

create table mytable (
    col1 int primary key,
    col2 int not null,
    col3 int not null,
    col4 int not null,
    col5 int not null,
    col6 int not null
);

Add Composite Index

ALTER TABLE mytable ADD INDEX index_name(col1,col2,col3);

insert data

INSERT INTO cloud.mytable (col1,col2,col3,col4,col5,col6) VALUES
     (1,1,1,1,1,1),
     (2,1,1,1,1,1),
     (3,1,1,1,1,1),
     (4,1,1,1,1,1),
     (5,1,1,1,1,1),
     (6,1,1,1,1,1),
     (7,1,1,1,1,1),
     (8,1,1,1,1,1),
     (9,1,1,1,1,1),
     (10,1,1,1,1,1);
INSERT INTO cloud.mytable (col1,col2,col3,col4,col5,col6) VALUES
     (11,1,1,1,1,1);

2. test

1. The query condition contains the prefix part of the index , That is to say col1, It can trigger the use of indexes

explain select * from mytable where col1=1;//  Hit index 
explain select * from mytable where col2=1;//  Miss index 
explain select * from mytable where col3=1;//  Miss index 

explain select * from mytable where col1=1 and col2 = 1;//  Hit index 
explain select * from mytable where col2=1 and col1 = 1;//  Hit index 
explain select * from mytable where col1=1 and col3 = 1;//  Hit index 
explain select * from mytable where col3=1 and col1 = 1;//  Hit index 
explain select * from mytable where col2=1 and col3 = 1;//  Miss index 
explain select * from mytable where col3=1 and col2 = 1;//  Miss index 

2. Use all keys of the union index , Can trigger the use of indexes

explain select * from mytable where col1=1 and col2 = 1;//  Hit index 
explain select * from mytable where col1=1 and col2 = 1 and col3 =1;//  Hit index 
explain select * from mytable where col1=2 and col3 = 1 and col1 =1;//  Miss index 
explain select * from mytable where col1=3 and col2 = 1 and col1 =1;//  Miss index 

3. According to the leftmost prefix principle, the prefix part of the index is included in the query criteria , That is to say col1, It can trigger the use of indexes

explain select * from mytable where col1=1;//  Hit index 
explain select * from mytable where col1=1 and col4=1;//  Hit index 

4. Use partial keys , But does not include the prefix part of the index , Use of non triggered indexes

explain select * from mytable where col2=1;//  Miss index 
explain select * from mytable where col3=1;//  Miss index 

5. Use all keys of the union index , But it's not AND operation , Use of non triggered indexes

explain select * from mytable where col1=1 or col2=1;//  Miss index 
explain select * from mytable where col2=1 or col1=1;//  Miss index 
explain select * from mytable where col1=1 or col2=1 or col3=1;//  Miss index 

6. Using a federated index , But use comparison in index columns 、 Calculated ( Contains not equal to and not) Use of non triggered indexes ;

But please note that the primary key and int The index of type can be used to compare the index of type ;

Using a federated index , But use comparison in index columns 、 Use of computed non triggering indexes

explain select * from mytable where col1 > 0;//  Hit index 
explain select * from mytable where col1 + 1 > 1;//  Miss index 
explain select * from mytable where col1  > 1 + 1;//  Hit index 

7. Using a federated index , But using leading fuzzy queries in index columns 、 Use of non triggering indexes for regular matching

explain select * from mytable where col1 like  "%1"; // The index cannot be hit 
explain select * from mytable where col1 regexp "^%1"; // The index cannot be hit 
explain select * from mytable where col1 like  "1%"; // Can hit index 

Reference link :https://www.jianshu.com/p/af6075c5e9fb

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/mysql The case that the composite index is not used by hit

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102147101557.html