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

  环境: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秒

posted @ 2017-08-18 13:32  many-object  阅读(3660)  评论(0编辑  收藏  举报