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 = '小明'
    union all
    select * from user where uname = '小红'
  3. where条件中的 in 和not in
    eg: select * from user where uname in ('小明','小红','凡凡');
    解决办法:若是连续值采用 between语句
    eg: select * from user where uname between '小明' and '小红';
  4. where条件中的is null和is not null
    eg: select * from user where uname is null;
  5. where条件中"!="和"<"or">"
  6. 对索引列进行运算。这里运算包括+、-、*、/等运算。也包括使用函数。
    select * from temp where amount+count>10 此时索引不起作用。
    select * from temp where round(amount)>10 此时索引也不起作用。
posted on 2021-12-07 15:27  it_hww  阅读(1892)  评论(0)    收藏  举报