当前位置:网站首页>联合索引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的写法来达到选择合适索引的目的
边栏推荐
- Redhat5 installing vmware tools under virtual machine
- Leetcode T1165: 日志分析
- MATLAB小技巧(29)多项式拟合 plotfit
- 学习笔记|数据小白使用DataEase制作数据大屏
- Pinduoduo lost the lawsuit: "bargain for free" infringed the right to know but did not constitute fraud, and was sentenced to pay 400 yuan
- How can I check the DOI number of a foreign document?
- leetcode 509. Fibonacci Number(斐波那契数字)
- Symmetric binary tree [tree traversal]
- POI export to excel: set font, color, row height adaptation, column width adaptation, lock cells, merge cells
- 隐马尔科夫模型(HMM)学习笔记
猜你喜欢

【从零开始】win10系统部署Yolov5详细过程(CPU,无GPU)

大促过后,销量与流量兼具,是否真的高枕无忧?

健身房如何提高竞争力?

Audio distortion analysis of DSP and DAC based on adau1452

当前发布的SKU(销售规格)信息中包含疑似与宝贝无关的字

What books can greatly improve programming ideas and abilities?

Common problems of caching in high concurrency scenarios

Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)

Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading

Abnova 体外转录 mRNA工作流程和加帽方法介绍
随机推荐
How to solve sqlstate[hy000]: General error: 1364 field 'xxxxx' doesn't have a default value error
大促过后,销量与流量兼具,是否真的高枕无忧?
Stack and queue-p79-9
MySQL的安装
ip地址那点事
Can't you really do it when you are 35 years old?
Postgresql中procedure支持事务语法(实例&分析)
Performance comparison between Ceres solver and g2o
JWT certification
博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
Abnova 免疫组化服务解决方案
docker-compose启动redis集群
impdp的transform参数的测试
怎样查找某个外文期刊的文献?
Cloudcompare point pair selection
健身房如何提高竞争力?
[start from scratch] detailed process of deploying yolov5 in win10 system (CPU, no GPU)
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
How can I check the DOI number of a foreign document?
mobx 知识点集合案例(快速入门)
