2011-08-16 27 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过滤器,在大型数据集上这可能是一项昂贵的操作。