开发者

Which of these SQL queries will have better performance? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

Please review my sample queries (given below), and let me know which query would perform better and why.

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

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

Tha开发者_开发百科nks


In most cases, they will be equivalent, because there is no transformation in the outer query - the optimizer will handle that silently.

I would recommend you ensure that you really do need to use UNION instead of UNION ALL.

In fact, combining UNION and the DISTINCT used in your first part of the UNION is redundant, because the UNION is going to ensure that duplicates are removed from the final set (both within sub-parts of the UNION and across parts of the UNION).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜