索引失效的情况有哪些
- 使用左或者左右模糊匹配(
like %xx
或者like %xx%
),xx%
可以走索引 - 对索引使用函数 (
where sum(age)= 5、where length(name) = 6
等) - 对索引进行表达式计算 (
where id + 1 = 10
),但是进行计算式可以的,如where id = 10 - 1
- 对索引进行隐式类型转换,索引是字符串类型,查询条件中,输入的是整型,此时就不会走索引,直接走全表扫描,用
where age = 60
去查age varchar
此时不会走索引,直接走全表扫描;但是,如果索引是整型,查询条件中即使是字符串,也会走索引 (用where age = ‘ 60’
去查age int
) - 联合索引非最左匹配。创建一个(a,b,c)联合索引
- where a=1;
- where a=1 and b=2 and c=3;
- where a=1 and b=2;
- 以上三种情况是走索引
- where b=2;
- where c=3;
- where b=2 and c=3;
- 这三种情况不走索引
- where 子句中的 or 比如
where id = 1 or age = 18
,null、!=操作符
...大约 1 分钟