sql语句中索引失效的几种情况(默认uname是索引列)
- 模糊查询中,like的前置%不会走索引
eg:select * from user where uname like '%凡凡'; - where条件中的or语句:
eg: select * from user where uname = '小明' or uname = '小红';
解决办法:使用 union、union all 语句。
eg:
select * from user where uname = '小明'
union all
select * from user where uname = '小红' - where条件中的 in 和not in
eg: select * from user where uname in ('小明','小红','凡凡');
解决办法:若是连续值采用 between语句
eg: select * from user where uname between '小明' and '小红'; - where条件中的is null和is not null
eg: select * from user where uname is null; - where条件中"!="和"<"or">"
- 对索引列进行运算。这里运算包括+、-、*、/等运算。也包括使用函数。
select * from temp where amount+count>10 此时索引不起作用。
select * from temp where round(amount)>10 此时索引也不起作用。