索引失效的场景

1.违反了最左前缀匹配法则

联合索引最左匹配原则_Pr Young的博客-CSDN博客

2.在索引列上做计算或者函数操作,也会导致索引失效从而进行全表扫描

比如select  * from table  where  name=”zhangsan“ 是会走索引的

但是select  * from table  where  left(name,2)=”zhangsan“,对索引列进行了函数操作,就不会走索引了

3.where子句中使用不等于,也会使得索引失效

select  * from table  where  name!=”zhangsan“

4.is null可以使用索引,但是is not null无法使用索引

select  * from table  where  name  is  null可以使用索引

select  * from table  where  name  is  not null就无法使用索引

5.like以通配符开头   也会导致索引失效

select  * from table  where  name like ”%明“

注意:不以通配符开头,只是包含通配符,还是会走索引的

(a,b,c)三个字段组成一个联合索引

where a = 3 and b like ‘kk%' and c=4     使用到了a,b,c
where a = 3 and b like ’%kk' and c=4     只用到了a
where a = 3 and b like ‘%kk%’  and c = 4    只用到了a

where a = 3 and b like ‘k%kk%  and c = 4 使 用到a,b,c

6.字符串不加单引号也会使得索引失效

select  * from table  where  name=123

7.在where子句中使用or导致索引失效

select  * from table  where  name=”zhangsan“ or age=22

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值