B-tree索引is null不会走索引,is not null会走索引;位图索引is null, is not null 都会走索引;联合索引is not null 只要在建立的索引列(不分先后)都会走索引;
条件中用or,只要其中有一个条件不带索引,则整个SQL语句不会使用索引查询;若想使用or,又想索引生效,只能将or条件中的每一列都加索引; EXPLAIN select * from t_student where age =23 or score =371044 or subject like ‘spring2%’; 使用了全表扫描
where a=3 and b=45 and c=5 .... 这种三个索引顺序使用中间没有断点,全部发挥作用;
where a=3 and c=5... 这种情况下b就是断点,a发挥了效果,c没有效果
where b=3 and c=4... 这种情况下a就是断点,在a后面的索引都没有发挥作用,这种写法联合索引没有发挥任何效果;
where b=45 and a=3 and c=5 .... 这个跟第一个一样,全部发挥作用,abc只要用上了就行,跟写的顺序无关
还需要注意,(a,b,c)多列索引和(a,c,b)是不一样的
(0) select * from mytable where a=3 and b=5 and c=4;
abc三个索引都在where条件里面用到了,而且都发挥了作用
(1) select * from mytable where c=4 and b=6 and a=3;
这条语句列出来只想说明 mysql没有那么笨,where里面的条件顺序在查询之前会被mysql自动优化,效果跟上一句一样
(2) select * from mytable where a=3 and c=7;
a用到索引,b没有用,所以c是没有用到索引效果的
(3) select * from mytable where a=3 and b>7 and c=3;
a用到了,b也用到了,c没有用到,这个地方b是范围值,也算断点,只不过自身用到了索引
(4) select * from mytable where b=3 and c=4;
因为a索引没有使用,所以这里 bc都没有用上索引效果
(5) select * from mytable where a>4 and b=7 and c=9;
a用到了 b没有使用,c没有使用
(6) select * from mytable where a=3 order by b;
a用到了索引,b在结果排序中也用到了索引的效果,前面说了,a下面任意一段的b是排好序的
(7) select * from mytable where a=3 order by c;
a用到了索引,但是这个地方c没有发挥排序效果,因为中间断点了,使用 explain 可以看到 filesort
(8) select * from mytable where b=3 order by a;
b没有用到索引,排序中a也没有发挥索引效果
数据库索引失效的情况like “%_" 百分号在前;字符型字段为数字时在where条件里不添加引号;在索引列上使用函数,如substr,decode,instr等,或对索引列进行运算;B-tree索引is null不会走索引,is not null会走索引;位图索引is null, is not null 都会走索引;联合索引is not null 只要在建立的索引列(不分先后)都会走...