2011-08-16 56 views
2

我編寫了一條SQL語句,各個語句運行良好,但是當我使用UNION將它們組合在一起時,語句花費的時間顯着延長。是否有我錯過的錯誤或使其更快的方法?UNION導致SQL語句運行慢很多

select bundle.bundle, bundle.week, bundle.sched_dt, dropper_assign.dropper_id 
from bundle, dropper_assign 
where bundle.bundle not in 
    (select bundle from forecast_entry) 
and bundle.week = dropper_assign.week 
and bundle.sched_zip3 = dropper_assign.zip3 
and bundle.sched_dt = dropper_assign.sched_date 
and bundle.project_cd = dropper_assign.project_code 
and dropper_assign.dropper_id <> 10002 
and bundle.project_cd = 'EXFC' 

union 

select bundle.bundle, bundle.week, bundle.sched_dt, splits.dropper_id 
from bundle, splits 
where bundle.bundle not in 
    (select bundle from forecast_entry) 
and bundle.bundle = splits.bundle 
and splits.dropper_id <> 10002 
and bundle.project_cd = 'EXFC'; 
+0

如果您確定沒有重複項,請改用'UNION ALL' – van

回答

4

UNION取兩個數據集並返回唯一重疊。換句話說,它花費時間去除重複。

UNION ALL但是,不會比較結果集以刪除重複項。

0

UNION將對最終結果應用DISTINCT過濾器,在大型數據集上這可能是一項昂貴的操作。