当前位置:网站首页>联合索引ABC的几种索引利用情况
联合索引ABC的几种索引利用情况
2022-07-07 02:37:00 【阿东-007】
对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分。例如索引是key index (a,b,c)。 可以支持a | a,b| a,b,c 3种组合进行查找,但不支持 b,c进行查找 .当最左侧字段是常量引用时,索引就十分有效。
以下是一些例子:
(1) select * from myTest where a=3 and b=5 and c=4; ---- abc顺序
abc三个索引都在where条件里面用到了,而且都发挥了作用
(2) select * from myTest where c=4 and b=6 and a=3;
where里面的条件顺序在查询之前会被mysql自动优化,效果跟上一句一样
(3) select * from myTest where a=3 and c=7;
a用到索引,b没有用,所以c是没有用到索引效果的(b没有使用到,所以索引达不到 c ,所以c未使用索引)
(4) select * from myTest where a=3 and b>7 and c=3; ---- b范围值,断点,阻塞了c的索引
a用到了,b也用到了,c没有用到,这个地方b是范围值,也算断点,只不过自身用到了索引
(5) select * from myTest where b=3 and c=4; — 联合索引必须按照顺序使用,并且需要全部使用
因为a索引没有使用,所以这里 bc都没有用上索引效果
(6) select * from myTest where a>4 and b=7 and c=9;
a用到了 b没有使用,c没有使用(a用了范围所以,相当于断点,之后的b,c都没有用到索引)
(7) select * from myTest where a=3 order by b;
a用到了索引,b在结果排序中也用到了索引的效果,a下面任意一段的b是排好序的
(8) select * from myTest where a=3 order by c;
a用到了索引,但是这个地方c没有发挥排序效果,因为中间断点了,使用 explain 可以看到 filesort
(9) select * from mytable where b=3 order by a;
b没有用到索引,排序中a也没有发挥索引效果
以下条件会导致索引失效:
1.不在索引列上做任何操作(计算、函数、(自动or手动)类型转换),会导致索引失效而转向全表扫描
2.存储引擎不能使用索引范围条件右边的列(例如 只用到b , c)
3.尽量使用覆盖索引(只访问索引的查询(索引列和查询列一致)),减少select ***
4.mysql在使用不等于(!=或者<>)的时候**无法使用索引会导致全表扫描
5.is null,is not null也无法使用索引
6.ike以通配符开头(’%abc…’)mysql索引失效会变成全表扫描的操作。问题:解决like‘%字符串%’时索引不被使用的方法7.字符串不加单引号索引失效
建议:
- 对于单键索引,尽量选择针对当前query过滤性更好的索引
- 在选择组合索引的时候,当前Query中过滤性最好的字段在索引字段顺序中,位置越靠前越好。
- 在选择组合索引的时候,尽量选择可以能够包含当前query中的where子句中更多字段的索引
- 在选择组合索引的时候,尽量选择可以能够包含当前query中的where子句中更多字段的索引
- 尽可能通过分析统计信息和调整query的写法来达到选择合适索引的目的
边栏推荐
猜你喜欢
LM11丨重构K线构建择时交易策略
Redhat5 installing vmware tools under virtual machine
反射(二)
字符串常量与字符串对象分配内存时的区别
博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
HKUST & MsrA new research: on image to image conversion, fine tuning is all you need
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
剑指offer-高质量的代码
企业如何进行数据治理?分享数据治理4个方面的经验总结
【NOI模拟赛】区域划分(结论,构造)
随机推荐
反射(二)
JWT 认证
Pinduoduo lost the lawsuit: "bargain for free" infringed the right to know but did not constitute fraud, and was sentenced to pay 400 yuan
Redis(二)—Redis通用命令
How to solve sqlstate[hy000]: General error: 1364 field 'xxxxx' doesn't have a default value error
Redis (I) -- getting to know redis for the first time
多个kubernetes集群如何实现共享同一个存储
Installing redis and windows extension method under win system
POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...
Overview of FlexRay communication protocol
Symmetric binary tree [tree traversal]
MySQL installation
Abnova 膜蛋白脂蛋白体技术及类别展示
Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading
C language (structure) defines a user structure with the following fields:
Prompt for channel security on the super-v / device defender side when installing vmmare
Abnova 免疫组化服务解决方案
缓存在高并发场景下的常见问题
ICML 2022 | 探索语言模型的最佳架构和训练方法
服装门店如何盈利?