码迷,mamicode.com
首页 > 数据库 > 详细

oracle查询优化之子查询条件优化

时间:2017-08-18 14:37:01      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:sel   div   weight   span   子查询   查询   lag   左右   11g   

  环境:oracle 11g

  现有a表与b表通过a01字段关联,要查询出a表的数据在b表没有数据的数据;sql如下

select count(1) from (select a.*,(select count(1)  from b where b.a01=a.a01) as flag from a) where flag=0

  因为flag是虚拟字段没有走不了索引导致这条sql执行起来特别慢 310W条数据查总数花费2秒左右。

  利用not exists优化sql如下

select count(1) from a where not exists(select 1 from b where a.a01=b.b01)

  利用not exists走索引,执行花费时间大约为0.2秒

oracle查询优化之子查询条件优化

标签:sel   div   weight   span   子查询   查询   lag   左右   11g   

原文地址:http://www.cnblogs.com/many-object/p/7389177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!