SQL UNION ALL to eliminate duplicates
But in the example, the first query has a condition on column a
, whereas the second query has a condition on column b
. This probably came from a query that's hard to optimize:
SELECT * FROM mytable WHERE a=X OR b=Y
This query is hard to optimize with simple B-tree indexing. Does the engine search an index on column a
? Or on column b
? Either way, searching the other term requires a table-scan.
Hence the trick of using UNION to separate into two queries for one term each. Each subquery can use the best index for each search term. Then combine the results using UNION.
But the two subsets may overlap, because some rows where b=Y
may also have a=X
in which case such rows occur in both subsets. Therefore you have to do duplicate elimination, or else see some rows twice in the final result.
SELECT * FROM mytable WHERE a=X
UNION DISTINCT
SELECT * FROM mytable WHERE b=Y
UNION DISTINCT
is expensive because typical implementations sort the rows to find duplicates. Just like if you use SELECT DISTINCT ...
.
We also have a perception that it's even more "wasted" work if the two subset of rows you are unioning have a lot of rows occurring in both subsets. It's a lot of rows to eliminate.
But there's no need to eliminate duplicates if you can guarantee that the two sets of rows are already distinct. That is, if you guarantee there is no overlap. If you can rely on that, then it would always be a no-op to eliminate duplicates, and therefore the query can skip that step, and therefore skip the costly sorting.
If you change the queries so that they are guaranteed to select non-overlapping subsets of rows, that's a win.
SELECT * FROM mytable WHERE a=X
UNION ALL
SELECT * FROM mytable WHERE b=Y AND a!=X
These two sets are guaranteed to have no overlap. If the first set has rows where a=X
and the second set has rows where a!=X
then there can be no row that is in both sets.
The second query therefore only catches some of the rows where b=Y
, but any row where a=X AND b=Y
is already included in the first set.
So the query achieves an optimized search for two OR
terms, without producing duplicates, and requiring no UNION DISTINCT
operation.
Related videos on Youtube
user3685285
Updated on September 15, 2022Comments
-
user3685285 over 1 year
I found this sample interview question and answer posted on toptal reproduced here. But I don't really understand the code. How can a UNION ALL turn into a UNIION (distinct) like that? Also, why is this code faster?
QUESTION
Write a SQL query using UNION ALL (not UNION) that uses the WHERE clause to eliminate duplicates. Why might you want to do this? Hide answer You can avoid duplicates using UNION ALL and still run much faster than UNION DISTINCT (which is actually same as UNION) by running a query like this:
ANSWER
SELECT * FROM mytable WHERE a=X UNION ALL SELECT * FROM mytable WHERE b=Y AND a!=X
The key is the AND a!=X part. This gives you the benefits of the UNION (a.k.a., UNION DISTINCT) command, while avoiding much of its performance hit.
-
Siyual over 7 yearsThis explanation is misleading... It's essentially doing a
UNION ALL
but filtering out the results from the first query. It's just filtering using theWHERE
clause, rather than using an expensiveDISTINCT
operation. -
Paul Spiegel over 7 yearsThe answer is wrong. This query does not eliminate duplicates that alreaydy exist in the table, while UNION DISTICT would do.
-
Dan Bracuk over 7 years@PaulSpiegel, assuming there is a primary key that does not involve
a
, there will be no duplicates because of theselect *
. -
DVT over 7 yearsCan you quote the source of the question and answer? Are you sure this is ALL of the content of the question and answer?
-
user3685285 over 7 yearstoptal.com/sql/interview-questions Quoted exactly.
-
-
user3685285 over 7 yearsWow, this answer makes a lot of sense to me. But why are some people saying it's wrong? Does it fail in some cases?
-
Bill Karwin over 7 yearsThe objection is that it doesn't account for cases where the table itself has duplicate rows (which should never be the case in a normalized database, but hey it happens). The query with
UNION DISTINCT
would eliminate duplicates from the result set. TheUNION ALL
keeps such duplicates. -
Connor Low about 3 yearsHi, thanks for your answer. This creates
table2
, and is not what the author of the question was asking. Try answering "How can a UNION ALL turn into a UNION (distinct) like that? Also, why is this code faster?" from the post.