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 = '小明'
    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 此时索引也不起作用。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM