2014-02-10 62 views
0

查詢我有兩個查詢到我的Oracle 8i數據庫加入的,而不是使用NOT IN

首先查詢(執行時間: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)返回第二個不存在的記錄

在我現有的應用程序,通過「不在」(執行時間:4.3分鐘)

有什麼辦法我可以做得更快我曾嘗試過「減」,但它需要相同數量的列兩個查詢結果集

有沒有更好的點子?

+0

根據[這個答案](http://stackoverflow.com/a/2246793/1048425) Oracle將以與NOT IN或NOT EXISTS相同的方式優化連接方法。 – GarethD

回答

0

我曾在我曾用「減」運算符和「IN」 不同的方式來完成它的執行時間(3秒)(比4.3min非常好)

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」結果集的第一個查詢