加入查询而不是使用NOT IN

时间:2014-02-10 09:46:58

标签: sql database oracle

我对我的oracle 8i数据库有两个查询

首次查询(执行时间:30毫秒)

select p.atc_id,a.atc_no,b.buyer_no,b.name||b.department,p.del_date_to,p.popack_id, p.popack_no,
p.buyer_po,sum(pd.quantity),nvl(max((select sum(sd.quantity) from scheduling_details sd,scheduling_master s 
where s.schedule_id=sd.schedule_id and s.popack_id=p.popack_id and (s.deleted='N' or status=1) group by s.popack_id)),0) 
from atc_master a, buyer_master b, popack_master p, popack_details pd where a.atc_id=p.atc_id and a.original='Y' and  
 a.buyer_no=b.buyer_no and p.popack_id=pd.popack_id and  a.atc_id=905 group by p.atc_id,a.atc_no,b.buyer_no,
b.name||b.department,p.del_date_to,p.popack_id,p.popack_no, p.buyer_po 

第二次查询(执行时间:3秒)

select distinct s.popack_id from scheduling_master s ,
 scheduling_details sd, popack_details pd where s.schedule_id=sd.schedule_id and  s.popack_id=pd.popack_id and s.deleted='N' 
 group by s.popack_id,sd.quantity having sum(pd.quantity)<sd.quantity  

我想要的结果是第一个查询应该返回第二个中不存在的记录(Po_packid不在第二个中)

在我通过“NOT IN”(执行时间:4.3分钟)实现的现有应用程序中

有没有什么方法可以让我更快地尝试“减号”,但它在查询结果集中需要相同数量的列

所以更好的想法?

1 个答案:

答案 0 :(得分:0)

我以不同的方式使用“减号”运算符和“IN” 它的执行时间是(3秒)(非常好于4.3分钟)

select p.atc_id,a.atc_no,b.buyer_no,b.name||b.department,p.del_date_to,p.popack_id, p.popack_no,
p.buyer_po,sum(pd.quantity),nvl(max((select sum(sd.quantity) from scheduling_details sd,scheduling_master s 
where s.schedule_id=sd.schedule_id and s.popack_id=p.popack_id and (s.deleted='N' or status=1) group by s.popack_id)),0) 
from atc_master a, buyer_master b, popack_master p, popack_details pd where a.atc_id=p.atc_id and a.original='Y' and  
 a.buyer_no=b.buyer_no and p.popack_id=pd.popack_id and  a.atc_id=905  and p.popack_id IN (select p.popack_id
from atc_master a, buyer_master b, popack_master p, popack_details pd where a.atc_id=p.atc_id and a.original='Y' and  
 a.buyer_no=b.buyer_no and p.popack_id=pd.popack_id and  a.atc_id=905 group by p.atc_id,a.atc_no,b.buyer_no,
b.name||b.department,p.del_date_to,p.popack_id,p.popack_no, p.buyer_po 
minus
select distinct s.popack_id from scheduling_master s ,
 scheduling_details sd, popack_details pd where s.schedule_id=sd.schedule_id and  s.popack_id=pd.popack_id and s.deleted='N' 
 group by s.popack_id,sd.quantity having sum(pd.quantity)<sd.quantity  )  group by p.atc_id,a.atc_no,b.buyer_no,
b.name||b.department,p.del_date_to,p.popack_id,p.popack_no, p.buyer_po 

我首先对表进行减号操作得到结果集然后我使用它并使用popack_id“IN”结果集的第一个查询