1. 程式人生 > 其它 >sql語句中索引失效的幾種情況

sql語句中索引失效的幾種情況

sql語句中索引失效的幾種情況(預設uname是索引列)

  1. 模糊查詢中,like的前置%不會走索引
    eg:select * from user where uname like '%凡凡';
  2. 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:
    select * from user where uname in ('小明','小紅','凡凡');
    解決辦法:若是連續值採用 between語句
    eg: select * from user where uname between '小明' and '小紅';
  3. where條件中的is null和is not null
    eg: select * from user where uname is null;
  4. where條件中"!="和"<"or">"
  5. 對索引列進行運算。這裡運算包括+、-、*、/等運算。也包括使用函式。
    select * from temp where amount+count>10 此時索引不起作用。
    select * from temp where round(amount)>10 此時索引也不起作用。