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 此時索引也不起作用。