Share via

Alternative to UNION

Bobby P 271 Reputation points
2026-04-22T20:14:11.0866667+00:00

I have a UNION Query that is combining information from a History Table and the current Master Table. We are talking about a few million rows here. The problem is the amount of [tempdb] space it is allocating and using for the UNION. So we thought if we loaded a New Staging Table from first the History and then the Master, and "dumb" this down a little bit and the SQL Server Optimizer and use of [tempdb] resources, that might help. So we loaded the History ok. And it created a few million rows and now...And as I type this it is now 20-minutes running...we are trying to load the Master Data and it is taking FOREVER! I think the UNION definitely helped by eliminating dupes.

Is there any alternative here? Like is there any way of getting around this UNION that seems to be hogging soooooo much [tempdb] space??

Please let me know your thoughts.

And Thanks in advance for your review and hopeful for a good, quality answer.

SQL Server | SQL Server Transact-SQL
0 comments No comments

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 133.9K Reputation points MVP Volunteer Moderator
    2026-04-22T21:01:21.75+00:00

    Without knowing anything about the tables or the queries it is impossible to give any precise help, but I can only discuss in general terms.

    The UNION operator will by default perform a DISTINCT operation. Consider this:

    DECLARE @a TABLE (a int NOT NULL)
    DECLARE @b TABLE (a int NOT NULL)
    
    INSERT @a (a) VALUES(1), (1), (2), (3)
    INSERT @a (a) VALUES(3), (4), (5), (5)
    
    SELECT a FROM @a
    UNION 
    SELECT a FROM @b
    
    SELECT a FROM @a
    UNION ALL
    SELECT a FROM @b
    
    

    The first SELECT returns 1, 2, 3, 4, 5. That is the distinct values. On the other hand, the second query returns 1, 1, 2, 3, 3, 4, 5, 5. That is, all rows.

    The UNION operation itself typically does not require a lot of tempdb space. But the operation of weeding out the duplicates certainly can require a lot of disk space. Particularly, if the memory grant for the query is too low. (The memory grant is a result of the optimizer's estimates.)

    Introduction of this staging table does not sound like a fruitful approach. What would be a good way to deal with this query is more difficult to say without further knowledge. Possibly, use UNION ALL and have other conditions filtering out lots of rows before applying DISTINCT. But then again, this means that these operators will have to work on more rows, so I am not very optimistic about that approach.


  2. Q&A Assist
    2026-04-22T20:14:26.3433333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    A UNION itself is not the root cause; tempdb pressure comes from how the underlying queries are executed (large sorts, hashes, spills, temp tables, etc.). The context here only provides general UNION behavior, but a few concrete options are supported:

    1. Use UNION ALL if duplicates are not required to be removed

    UNION removes duplicates, which forces a sort or hash operation and can consume a lot of tempdb. If duplicates do not need to be eliminated, replacing UNION with UNION ALL avoids that deduplication step and reduces tempdb usage:

    SELECT col1, col2, ...
    FROM History
    UNION ALL
    SELECT col1, col2, ...
    FROM Master;
    

    The documentation notes that using ALL keeps duplicates and generally runs faster because it avoids the extra work of removing duplicates.

    1. Push work into a single UNION operation instead of staging

    Access/SQL guidance shows that UNION is designed to combine multiple result sets in one operation, and the engine can optimize that better than manual staging into a separate table and then inserting again. A single UNION (or UNION ALL) over the two source tables is usually preferable to:

    • Insert History into a staging table
    • Then insert Master into the same staging table

    because each insert can still require tempdb for sorts, indexes, and constraints on the staging table.

    1. Ensure the UNION query is as simple as possible

    The UNION operator requires:

    • Same number of output columns
    • Same order
    • Same or compatible data types

    Keeping the projection narrow (only needed columns) and avoiding unnecessary expressions reduces row width and tempdb usage.

    1. Investigate the specific code paths that fill tempdb

    From the related tempdb guidance, tempdb can be filled by:

    • Very large intermediate result sets
    • Hash or sort spills
    • Spool operators

    The recommendation is to identify the specific statements that trigger tempdb full (for example, error 1105) and focus tuning efforts there rather than assuming UNION alone is the problem.

    If deduplication is required, UNION must either sort or hash, and that inherently uses tempdb. In that case, the main levers are:

    • Reducing the number of rows before the UNION (filtering earlier)
    • Reducing row width (fewer columns)
    • Using UNION ALL plus a separate, more targeted deduplication strategy if possible

    References:

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.