荆州拉人去编程 2019-11-28 11:08 采纳率: 100%
浏览 566
已采纳

一个关于oracle多列索引是否生效的问题

  • 假如表A有索引(a, b, c),然后现在查询SQL是
select *
  from A, B
 where b = xxx
   and a = xxx
   and d = xxx
   and c = xxx

请问这种情况下生效的索引依然是(a, b, c)三列么?

  • 写回答

1条回答 默认 最新

  • 无双_ 2019-11-28 11:24
    关注

    索引(a,b,c)
    (0)select * from mytable where a=3 and b=5 and c=4; --abc三个索引都在where条件里面用到了,而且都发挥了作用
    (1)select * from mytable where c=4 and b=6 and a=3; --这条语句列出来只想说明 mysql没有那么笨,where里面的条件顺序在查询之前会被mysql自动优化,效果跟上一句一样
    (2)select * from mytable where a=3 and c=7; --a用到索引,b没有用,所以c是没有用到索引效果的
    (3)select * from mytable where a=3 and b>7 and c=3; --a用到了,b也用到了,c没有用到,这个地方b是范围值,也算断点,只不过自身用到了索引
    (4)select * from mytable where b=3 and c=4; --因为a索引没有使用,所以这里 bc都没有用上索引效果
    (5)select * from mytable where a>4 and b=7 and c=9; --a用到了 b没有使用,c没有使用
    (6)select * from mytable where a=3 order by b; --a用到了索引,b在结果排序中也用到了索引的效果,前面说了,a下面任意一段的b是排好序的
    (7)select * from mytable where a=3 order by c; --a用到了索引,但是这个地方c没有发挥排序效果,因为中间断点了,使用 explain 可以看到 filesort
    (8)select * from mytable where b=3 order by a; --b没有用到索引,排序中a也没有发挥索引效果

    可以看得出来与你在where后边的排序是没有关系的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services