SQL UNION 性能

SQL UNION performance

我在 sql 查询中使用了三个 UNION。查询需要很多时间。还有其他更快的选择吗?我的数据库是 Vertica,我在 Tableau 中编写了查询。 Select 查询看起来像 -

(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)

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

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