数据库索引【索引失效】

以下情况不走索引:

  • 通配符在搜索词首出现时,oracle不能使用索引:
select * from student where name like '%xiaoyao';
  • 在索引列上使用not,oracle碰到not会停止使用索引,而采用全表扫描:

select * from student where not (score=100);

select * from student where score <> 100;

select * from student where score != 100;

select * from student where score not in (80,90,100);

--not exist也不走索引
  • 当数据类型是字符串类型的时候,如果条件数据没有被引号引起来,索引失效 
select * from student where dept_id = 1

--应该使用:
select * from student where dept_id = '1'
  • 当使用or的情况下,如果不是每一列的条件都有索引,索引失效
--student表内name建立了索引,sex没有建索引
select * from student where name = 'zhangsan' or sex = 1
  • 组合索引,不是使用第一列索引,索引失效
--建立组合索引(key1,key2);
select * from key1 = 1;--组合索引有效;
select * from key1 = 1 and key2= 2;--组合索引有效;
select * from key2 = 2;--组合索引失效;不符合最左前缀原则
  • 当全表扫描速度比索引速度快时,数据库会使用全表扫描,此时索引失效
  • 具体看索引有没有失效,可以看sql的执行计划,根据执行计划来分析调优
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值