Moslem Ben Dhaou
Moslem Ben Dhaou

Reputation: 7005

Use UNION and UNION ALL in same query

I am running into some performance issue and I have a query as follow:

SELECT * FROM Foo

UNION

SELECT * FROM Boo

UNION

SELECT * FROM Koo

I know for sure that Koo is not returning any duplicates. I was considering to use UNION ALL at the end and therefore save the sorting and distinct selection time for Koo. The query should look like:

SELECT * FROM Foo

UNION

SELECT * FROM Boo

UNION ALL

SELECT * FROM Koo

Will that help or it will be affected bu the first UNION?

Upvotes: 3

Views: 2258

Answers (1)

RichardTheKiwi
RichardTheKiwi

Reputation: 107816

Always use UNION ALL if you know that there will not be duplicates.

It's a bit gray here, but still worthwhile - although practically marginal.

If it's a straight UNION-UNION, SQL Server can optimize this to collate all 3 result sets and perform a single sort across both. Since sorting is mostly O(n log n), it amounts to very little difference between that and [(A distinct B) add C].

update

Although it is possible to perform a single merge-sort, SQL Server doesn't seem to do it (at least not always) - so the wisdom of using UNION ALL is definitely worth it here. Compare the plans here: SQLFiddle (click on the "View Execution Plan" links)

Upvotes: 3

Related Questions