2011-08-22 60 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