时隔许久又来更新博客了!
总结一下sql优化中sql语句里会导致索引失效的语句(索引失效,导致全表扫描)
1、like的前置%
eg. select * from user where name like '%张三%';
2、where条件中的 or 语句
eg. select * from user where account= 20 or account = 30;
解决办法:使用 union、union all 语句。
select * from user where account = 20
union all
select * from user where account = 30
3、where条件中的 in 和not in
eg. select * from user where account in (1,2,3);
解决办法:若是连续值采用 between语句
select * from user where account between 1 and 3;
4、 is null 和 is not null
eg.select * from user where account is null;
5、!= 和<>
eg.select * from user where account != 1;
select * from user where account > 1;
6、判断一个值是否存在,用exists 替代in
7、where条件中使用函数对索引列进行操作
暂时只想起来这么多,欢迎留言补充。