2011-08-22 53 views
-2

请查看我的示例查询(下面给出),并让我知道哪个查询会更好地执行以及为什么。哪些SQL查询会有更好的性能?

方法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... 
) 

方法2:

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... 

感谢

+1

考虑http://dba.stackexchange.com/用于SQL问题。 –

回答

4

在大多数情况下,他们将是等效的,因为在外部没有改造查询 - 优化器将自动处理它。

我会建议你确保你确实需要使用UNION而不是UNION ALL。

实际上,组合UNION和在UNION的第一部分中使用的DISTINCT是多余的,因为UNION将确保从最终集合中删除重复项(两者都在UNION的子部分内以及跨部分的联盟)。

+0

:啊,是的。这是有道理的,我会从它删除'独特'。谢谢! – gin

+0

请注册并将此答案标记为已接受 –

+1

@ hamlin11完成!:) – gin