当前位置:网站首页>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
边栏推荐
猜你喜欢

Html+php+mysql login registration page
![[Axi] explain the principle of two-way handshake mechanism of Axi protocol](/img/79/21da4ef7da4d12e586f2d605e8db78.png)
[Axi] explain the principle of two-way handshake mechanism of Axi protocol

2022g1 industrial boiler stoker test questions and online simulation test

原生支持 ARM64 的首个版本!微软 Win11/10 免费工具集 PowerToys 0.59 发布

联想首次详解混合云Lenovo xCloud五大优势,如何打造智能化数字底座

MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案

How Photoshop opens, edits and exports pictures in webp format

Fallback operation in SVN

数组、List、Set、Map、Properties依赖注入格式

数据与信息资源共享平台(七)
随机推荐
unity 代码为动画注册事件
云数据中心中的SDN/NFV应用
How about opening an account with flush? Is it safe to open an account?
Opencv_ 100 questions_ Chapter II (6-10)
软件测试入门之软件测试的概念与过程(精辟内容)
Software features and functions of the blind box mall app system development
Keras deep learning practice (8) -- using data enhancement to improve neural network performance
js敏感信息泄露检测工具
【接口教程】EasyCVR如何通过接口设置平台级联?
Custom view: graphics and image processing (I): using simple pictures
Niuke.com: sum of two numbers
Lenovo explained for the first time the five advantages of hybrid cloud Lenovo xcloud and how to build an intelligent digital base
vulnhub之dc3
线程池的创建
DC4 of vulnhub
MySQL MVCC 多版本并发控制
联想首次详解混合云Lenovo xCloud五大优势,如何打造智能化数字底座
我们对产业互联网的认识,还是困囿于互联网式的平台和中心的逻辑之中
Open source project PM how to design official website
Vscode common shortcuts