sql語句中索引失效的幾種情況
阿新 • • 發佈:2021-12-07
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 = 20
union all
select * from user where uname = 30
1.where條件中的 in 和not in
eg:
解決辦法:若是連續值採用 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 此時索引也不起作用。