有两个百万级的大表a,b,现在需要关联两个表查询是否存在满足条件的记录,我的sql如下: select count(a.id) from a,b where a.id=b.id and ... 当满足查询条件的记录很多时,查询效率非常低下,常常需要几十甚至100多秒,请教各位大侠有没有解决此效率问题的好方法
最终终于用KingSunSha(弱水三千) 的查询方法搞定了,当初之所以效率低可能是因为建了索引之后没有更新oracle的统计信息,现在通过下面的查询
select 1 from dual
where exists (select 1 from a,b where a.id=b.id and ...);
已经使查询速度提高到了毫秒级。
select count(a.id)
from a,b where a.id=b.aid and a.dept = '01' and a.flag = 'Y' and b.productid = 'ABC'
可以看到a表的所有字段都在索引中,那你可以试试索引扫描。
select /*+ index_ffs(a 索引名称)*/ count(a.id)
from a,b where a.id=b.aid and a.dept = '01' and a.flag = 'Y' and b.productid = 'ABC'