美文网首页
防止索引失效

防止索引失效

作者: 换煤气哥哥 | 来源:发表于2018-04-27 13:29 被阅读0次

索引:排好序利于快速查找的数据结构(Btree)

explain/show profile
1、type
all - index - range - ref - eqref - const - system
全文扫描不走索引 - 遍历索引 - 取索引的某个范围 - 取索引字段等于某值的所有行 - 等于某值的唯一行 - ??
2、keylen 索引长度
3、rows 读取行数
4、Extra
using index 使用覆盖索引
using where 没啥用
using temporary 使用临时表,需避免,常见排序分组
using filesort 无法使用索引完后排序,而直接走文件排序

防止索引失效情况:
1、最佳左前缀,中间不可断掉;
2、索引列不要做函数、计算、类型转化等操作,譬如where name = 123
3、范围后边全失效,范围字段本身不失效
4、避免用 != > < 及 is null 、not null
5、索引列不能有null
6、like 'xx%'走索引,like '%xx%' 不走

建立复合索引 create index idx_c1_c2_c3_c4 on table t_xx(c1,c2,c3,c4);
where查询条件:
1、 c1 = 'c1'
2、 c1 = 'c1' and c2 = 'c2'
3、 c1 = 'c1' and c2 = 'c2' and c3 = 'c3'
4、 c1 = 'c1' and c2 = 'c2' and c3 = 'c3' and c4 = 'c4'
以上全都用到索引,type为ref,只不过keylen不同。若where条件中不出现c1则根本不走索引

5、 c1 = 'c1' and c3 = 'c3' and c2 = 'c2' and c4 = 'c4'
6、 c4 = 'c4' and c3 = 'c3' and c2 = 'c2' and c1 = 'c1'
查询条件都是等于某个常量或order by时,即使顺序混乱,mysql会自动优化,结论同1~4

7、 c1 = 'c1' and c2 = 'c2' and c3 > 'c3' and c4 = 'c4'
type为range,用到3个分段索引,c3段的索引也是用到的

8、 c1 = 'c1' and c2 = 'c2' and c4 > 'c4' and c3 = 'c3'
type为range,用到4个分段索引

9、 c1 = 'c1' and c2 = 'c2' and c4 = 'c4' order by c3
10、c1 = 'c1' and c2 = 'c2' order by c3
type为ref,c1、c2段查找,c3段排序,因为索引的另一个功能就是排序

11、c1 = 'c1' and c2 = 'c2' order by c4
type为ref,2分段查找。少了c3破坏了索引,排序只能using filesort

12、c1 = 'c1' and c5 = 'c5' order by c2,c3
type为ref,c1段查找。c2、c3段索引用于排序,无filesort

13、c1 = 'c1' and c5 = 'c5' order by c3,c2
c1用于查找,因为顺序反了,有filesort

14、c1 = 'c1' and c2 = 'c2' and c5 = 'c5' order by c3,c2
无filesort,因为c2已经是常量,c2就不用排序了,不存在顺序颠倒

15、c1 = 'c1' and c4 = 'c4' group by c2,c3
分组前必然排序,Extra很和谐

16、c1 = 'c1' and c4 = 'c4' group by c3,c2
Extra 产生using temporary 和 using filesort

相关文章

  • 防止索引失效

    索引:排好序利于快速查找的数据结构(Btree) explain/show profile1、typeall - ...

  • mysql 高级调优

    索引失效

  • 索引的建立原则, 如何避免索引失效

    源自面试鸭 建立索引 如何避免索引失效 使用索引的缺点

  • oracle 使索引临时失效

    参考 使索引临时失效

  • MySQL索引

    MySQL索引 索引介绍 索引原理与分析 组合索引 索引失效分析 索引介绍 什么是索引索引:包括聚集索引、覆盖索引...

  • Mysql索引失效

    mysql 索引失效的原因有哪些?Mysql索引失效的原因 1、最佳左前缀原则——如果索引了多列,要遵守最左前缀原...

  • mysql 索引失效分析

    索引并不会时时发生,有时就算是where查询字段中添加了索引,索引也会失效,下面我们来讲讲五种索引失效的场景。 1...

  • 索引优化1

    研究索引失效的问题 一、准备工作 1、建表 2、插入数据 3、创建索引 二、索引不失效的口诀 1、全职匹配我最爱 ...

  • 索引失效

    索引失效 只要我们了解索引是如何使用B+这个数据结构创建,那么就更容易理解下面索引失效的原因。 对staff表的(...

  • 索引失效

    在编写sql语句时,一般都会用到索引来提升sql性能,但是有些sql语句使用索引是不生效的。 is null 和...

网友评论

      本文标题:防止索引失效

      本文链接:https://www.haomeiwen.com/subject/scmblftx.html