当前位置:网站首页>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
边栏推荐
- 视频融合云服务EasyCVR集群录像事件查询无效是什么原因?
- 样板小作坊
- Bottom layer communication protocol ① of single chip microcomputer -- synchronous and asynchronous, parallel and serial, full duplex and half duplex, simplex, level signal and differential signal
- vulnhub之dc3
- 优化代码去除if-else
- Déploiement et utilisation de base de la carte multi - réseau kubernets
- 同花顺开户怎么样?开户安全吗?
- 【006】初识字符串
- 2022g1 industrial boiler stoker test questions and online simulation test
- Reading notes on the way of Huawei data
猜你喜欢

Auto. JS Pro development environment configuration

Static routing configuration of serial interface in router experiment (Supplement)

Niuke: expression evaluation

数据与信息资源共享平台(六)
![[gmm+kde] target tracking simulation based on GMM and KDE kernel estimation of MATLAB](/img/44/f2b6ddf49b6d985129f57195e858eb.png)
[gmm+kde] target tracking simulation based on GMM and KDE kernel estimation of MATLAB

Distributed Foundation

Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事
![[original] analysis of nine price HPV data capture of Yilu app](/img/e0/af4901d119a6e59de02a6786d427dd.png)
[original] analysis of nine price HPV data capture of Yilu app

项目实训13——界面补充
![[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
随机推荐
vulnhub之dc3
Our understanding of the industrial Internet is still trapped in the logic of an Internet like platform and center
The definition of the metauniverse and the seven infinite features
[original] analysis of nine price HPV data capture of Yilu app
28岁自学编程会不会太晚了?靠谱吗?
Template_ Calculate number of combinations
Vscode common plug-ins and configurations
但身示你五县非那最土zaiFKMW
锁机制
玩电子,穷三代
中金财富证券证券股票开户安全吗?靠谱吗?
How about opening an account with flush? Is it safe to open an account?
200 c language words, please collect!
功能测试之设计语言测试:功能测试包含哪些测试?分别有什么作用
2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation
Multus CNI deployment and basic use of kubernetes multi network card scheme
Native support for the first version of arm64! Microsoft win11/10 free tool set PowerToys 0.59 release
数据与信息资源共享平台(六)
【原创】医鹿APP九价HPV数据抓包分析
Untiy reset animation