以下哪种SQL查询的性能更好?

以下哪种SQL查询的性能更好?,sql,performance,Sql,Performance,请查看我的示例查询(如下所示),并让我知道哪个查询的性能更好以及原因 方法1: select team_id,grp_name,grp_desc from ( select distinct team_id,grp_name,grp_desc from team_table tt where ..... UNION select team_id,grp_name,grp_desc from sec_team_table stt where .... UNION select team_id,gr

请查看我的示例查询(如下所示),并让我知道哪个查询的性能更好以及原因

方法1:

select team_id,grp_name,grp_desc from
(
select distinct team_id,grp_name,grp_desc from team_table tt where .....
UNION
select team_id,grp_name,grp_desc from sec_team_table stt where ....
UNION
select team_id,grp_name,grp_desc from ter_team_table ttt where...
)
select distinct team_id,grp_name,grp_desc from team_table tt where .....
UNION
select team_id,grp_name,grp_desc from sec_team_table stt where ....
UNION
select team_id,grp_name,grp_desc from ter_team_table ttt where...
方法2:

select team_id,grp_name,grp_desc from
(
select distinct team_id,grp_name,grp_desc from team_table tt where .....
UNION
select team_id,grp_name,grp_desc from sec_team_table stt where ....
UNION
select team_id,grp_name,grp_desc from ter_team_table ttt where...
)
select distinct team_id,grp_name,grp_desc from team_table tt where .....
UNION
select team_id,grp_name,grp_desc from sec_team_table stt where ....
UNION
select team_id,grp_name,grp_desc from ter_team_table ttt where...

谢谢在大多数情况下,它们是等价的,因为外部查询中没有转换-优化器将以静默方式处理

我建议您确保确实需要使用UNION而不是UNIONALL


事实上,将联合体和联合体第一部分中使用的独立体组合在一起是多余的,因为联合体将确保从最终集合中删除重复项(在联合体的子部分内和联合体的各个部分之间)。

:啊,是的。这很有意义。我将从中删除“独立体”。谢谢!请投票并将此答案标记为已接受。请考虑SQL问题。