(1)索引面试题分析



所谓索引:就是排好序的快速查找数据结构,排序家查找是索引的两个用途


select * 在where使用到了索引,当select * 有模糊查询%在左边索引会失效


当select * where后面索引的顺序发生变化,也会用到索引4个
我们where后面倒着排序,也会使用到索引4个

上面,因为有MySql查询优化器,它会把Msql的命令自动的调整和优化,已达到最佳效果
查询* ,where后面c3>a3,用到了范围导致索引失效 ,用到了3个索引c1 c2 c3,大小93

用到了范围导致索引失效 ,Msql优化器话进行优化,用到了4个索引c1 c2 c3c4 ,大小124

用到2个索引,大小62,严格来说c3也用到了,它没有用于查找,用于了排序


Extra:出现了Using filesort,因为where后面没有按照我建立索引的顺序使用缺少了c3,用c4去排序,产生了文件内排序,这个是非常影响性能的

用到1个索引:c1,c2c3用到去排序去啦
出现了文件内排序,因为order by 后面没有按照建立索引的顺序使用

用到2个索引c1c2 查找和排序都是按照顺序的

用到2个索引c1c2 查找和排序都是按照顺序的
只要order by 使用的跟建立的顺序不一样们就会产生文件内排序,这个是个例外,因为排序字段已经是一个常量了,常量了排序就没用了,就没有产生文件内排序,跟下面那个进行对比

用到了group by后按照顺序,这里索引用到了一个c1

用到了group by,这里索引用到了一个c1,但是group by没有按照索引的顺序,多了Using filesort 和Using temporary


(2)总结口诀
当中间使用like %(在右边)也是范围查询是具体的范围查询,type为range它跟>不一样,他能使用到like后的索引,用到了3个索引

当中间使用like %(在左边)不是具体的范围,这个type不是range,只用到一个索引c1
当中间使用like % ... %(两个%)不是具体的范围,这个type不是range,只用到一个索引c1
当中间使用like %(最左边有具体的开头)使用到3个索引

