SQL UNION性能

时间:2015-03-04 07:04:08

标签: sql union tableau vertica

我在sql查询中使用了三个UNION。查询需要花费大量时间。还有其他更快的替代方案吗?我的数据库是Vertica,我在Tableau中编写了查询。 选择查询看起来像 -

(select A,B,C from (select fun(X) as A, B,C,D from table1 where condition1 group by B,C,D)alias where condition2)
UNION 
(select A,B,C from (select fun(X) as A, B,C,D from table1 where condition1 group by B,C,D)alias where condition3)
]
UNION
[
(select A,B,C from (select fun(X) as A, B,C,D from table1 where condition1 group by B,C,D)alias where condition4) 
UNION
(select A,B,C from (select fun(X) as A, B,C,D from table1 where condition1 group by B,C,D)alias where condition5)

1 个答案:

答案 0 :(得分:2)

UNION ALL比UNION执行得更好,因为数据库服务器不必消除重复的行,这可能代价高昂。通常UNION ALL就是你想要的。

此外,看起来您可以使用OR将所有最外层的条件组合在一起,这样可以简化查询,直到您甚至不需要UNION或UNION ALL