当前位置:网站首页>MySQL组合索引不被命中使用的情况
MySQL组合索引不被命中使用的情况
2022-06-10 21:48:00 【Li_XiaoJin】
今天遇到这个问题,之前没有仔细想过这个问题,记录一下。
1. 准备工作
先创建一张表
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
);
添加组合索引
ALTER TABLE mytable ADD INDEX index_name(col1,col2,col3);
插入数据
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. 测试
1. 查询条件中包含索引的前缀部分, 也就是 col1, 可以触发索引的使用
explain select * from mytable where col1=1;// 命中索引
explain select * from mytable where col2=1;// 未命中索引
explain select * from mytable where col3=1;// 未命中索引
explain select * from mytable where col1=1 and col2 = 1;// 命中索引
explain select * from mytable where col2=1 and col1 = 1;// 命中索引
explain select * from mytable where col1=1 and col3 = 1;// 命中索引
explain select * from mytable where col3=1 and col1 = 1;// 命中索引
explain select * from mytable where col2=1 and col3 = 1;// 未命中索引
explain select * from mytable where col3=1 and col2 = 1;// 未命中索引
2. 使用联合索引的全部索引键, 可触发索引的使用
explain select * from mytable where col1=1 and col2 = 1;// 命中索引
explain select * from mytable where col1=1 and col2 = 1 and col3 =1;// 命中索引
explain select * from mytable where col1=2 and col3 = 1 and col1 =1;// 未命中索引
explain select * from mytable where col1=3 and col2 = 1 and col1 =1;// 未命中索引
3. 根据最左前缀原则查询条件中包含索引的前缀部分, 也就是 col1, 可以触发索引的使用
explain select * from mytable where col1=1;// 命中索引
explain select * from mytable where col1=1 and col4=1;// 命中索引
4. 使用部分索引键, 但不包含索引的前缀部分,不可触发索引的使用
explain select * from mytable where col2=1;// 未命中索引
explain select * from mytable where col3=1;// 未命中索引
5. 使用联合索引的全部索引键, 但不是AND操作, 不可触发索引的使用
explain select * from mytable where col1=1 or col2=1;// 未命中索引
explain select * from mytable where col2=1 or col1=1;// 未命中索引
explain select * from mytable where col1=1 or col2=1 or col3=1;// 未命中索引
6. 使用联合索引的,但是在索引列使用比较、计算的(包含不等于和not)不可触发索引的使用;
但是请注意在对主键和int类型的索引使用比较类型的则可以出发索引;
使用联合索引的,但是在索引列使用比较、计算的不可触发索引的使用
explain select * from mytable where col1 > 0;// 命中索引
explain select * from mytable where col1 + 1 > 1;// 未命中索引
explain select * from mytable where col1 > 1 + 1;// 命中索引
7. 使用联合索引的,但是在索引列使用前导模糊查询、正则匹配的不可触发索引的使用
explain select * from mytable where col1 like "%1"; //不可命中索引
explain select * from mytable where col1 regexp "^%1"; //不可命中索引
explain select * from mytable where col1 like "1%"; //可以命中索引
参考链接:https://www.jianshu.com/p/af6075c5e9fb
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/mysql组合索引不被命中使用的情况
边栏推荐
- MySQL MVCC 多版本并发控制
- Distributed Foundation
- Template_ Calculate number of combinations
- [tcapulusdb knowledge base] tcapulusdb tcapproxy capacity expansion introduction
- Operation of simulated examination platform for welder (primary) test questions in 2022
- vulnhub之dc3
- dc_ Study and summary of labs--lab1
- IP反查域名
- Icml2022 | reexamine end-to-end voice to text translation from scratch
- What should I do if the mobile phone number cannot be updated
猜你喜欢

C automatically generates Oracle table creation statement according to excel

线程池——治理线程的法宝

Déploiement et utilisation de base de la carte multi - réseau kubernets

数组、List、Set、Map、Properties依赖注入格式
![[tcapulusdb knowledge base] Introduction to tcapulusdb engine parameter adjustment](/img/74/6ce32e007c064c9255269fe38761a4.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb engine parameter adjustment

简单阻抗匹配电路及公式

Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事

Redis从入门到入土

Opencv_ 100 questions_ Chapter II (6-10)

Static routing configuration of serial interface in router experiment (Supplement)
随机推荐
线程池——治理线程的法宝
smb匿名
Opencv_100问_第四章 (16-20)
Online questionnaire system based on php+web+mysql
Can Huawei matepad become the secondary screen of your laptop?
QT custom delegation
Matlab - Implementation of evolutionary game theory
Display of successful cases of target customer matching data table
数组、List、Set、Map、Properties依赖注入格式
[tcapulusdb knowledge base] Introduction to tcapulusdb engine parameter adjustment
盲盒商城APP系统开发的软件特点和盲盒功能介绍
Informatics Olympiad all in one 1280: [example 9.24] skiing | openjudge noi 2.690: skiing | Luogu p1434 [show2002] skiing
200个C语言单词,请收藏!
Locking mechanism
I have made a dating app for myself. Interested friends can have a look
SMB anonyme
MySQL主从复制解决读写分离
Sdn/nfv application in cloud data center
Redis数据结构
Typescript - declaration files and built-in objects