UNION vs UNION ALL Performance
When I was comparing performance of UNION vs MySQL 5.0 index merge algorithm Sinisa pointed out I should be using UNION ALL instead of simple UNION in my benchmarks, and he was right. Numbers would be different but it should not change general point of having optimization of moving LIMIT inside of union clause being cool thing.
But So is UNION ALL indeed faster than UNION DISTINCT (the UNION is shortcut for UNION DISTINCT) ?
Indeed it is. I did not have the same data as I used for the other test but I created similar test case - table with separate indexes on "a" and "b" columns with cardinality of 100, having about 40.000.000 of rows
-
SELECT * FROM test.abc WHERE i=5 union SELECT * FROM test.abc WHERE j=5
This original query was taking about 22 seconds.
As I modified it:
-
SELECT * FROM test.abc WHERE i=5 union ALL SELECT * FROM test.abc WHERE j=5 AND i!=5
The query time dropped to about 6 seconds which is 3.5 times faster - quite considerable improvement.
As you can notice I added "i!=5" clause - this is what allows us to ensure we do not have duplicate rows in result set matching both conditions and so result will be same as query with "i=5 or j=5" where clause.
I also tried this original query (which uses index merge method in MySQL 5.0):
-
SELECT * FROM test.abc WHERE i=5 OR j=5
Such query takes 4 seconds so if you do not need to trick with order by and limit using index merge is faster than UNION as it indeed should be.
So why UNION ALL is faster than UNION DISTINCT ?
The first informed guess would be - because UNION ALL does not need to use temporary table to store result set, however this is not correct - both UNION ALL and UNION distinct use temporary table for result generation. Perhaps one more thing for Optimizer Team to look into.
Interesting enough the fact UNION and UNION ALL require temporary table can only be seen in SHOW STATUS - EXPLAIN does not want to tell you this shameful fact:
-
mysql> EXPLAIN (SELECT * FROM test.abc WHERE i=5) union ALL (SELECT * FROM test.abc WHERE j=5 AND i!=5) \G
-
*************************** 1. row ***************************
-
id: 1
-
select_type: PRIMARY
-
TABLE: abc
-
type: ref
-
possible_keys: i
-
KEY: i
-
key_len: 5
-
ref: const
-
rows: 348570
-
Extra: USING WHERE
-
*************************** 2. row ***************************
-
id: 2
-
select_type: UNION
-
TABLE: abc
-
type: ref
-
possible_keys: i,j
-
KEY: j
-
key_len: 5
-
ref: const
-
rows: 349169
-
Extra: USING WHERE
-
*************************** 3. row ***************************
-
id: NULL
-
select_type: UNION RESULT
-
TABLE: <union1,2>
-
type: ALL
-
possible_keys: NULL
-
KEY: NULL
-
key_len: NULL
-
ref: NULL
-
rows: NULL
-
Extra:
-
3 rows IN SET (0.00 sec)
In fact EXPLAIN output is the same for UNION and UNION ALL (which is too bad as execution for them is obviously different).
The difference in execution speed comes from the fact UNION requires internal temporary table with index (to skip duplicate rows) while UNION ALL will create table without such index.
This also explains why difference becomes larger when on disk table is required (as in this case) - Hash indexes used by MEMORY table are very efficient and do not give so much overhead.
9 Comments











del.icio.us
digg
I read the source for UNION once (Mark Leith pointed me to it). It’s not very much code. The only difference between the two is the temporary table has the “distinct” property set, as I recall. So of course that means a unique index on all columns, which is bound to be slow.
We should mention this in our book… it’s such an instinct for me to use UNION ALL that I forgot about it. I’ll go look and see if we have mentioned it anywhere.
Comment :: October 5, 2007 @ 6:07 pm
Baron,
I think the most important thing here is the fact even UNION ALL uses temporary table, while it could simply be sending result sets one after another in many cases, possibly with little conversion to adjust data types.
The unique index is in fact not index on all columns but some form of hash index - with MyISAM key limit index on all columns would not work for tables with long rows not to mention BLOBs
Comment :: October 6, 2007 @ 2:01 am
[...] Peter Zaitsev of the MySQL Performance Blog compares the performance of UNION vs. UNION ALL. [...]
Pingback :: October 12, 2007 @ 9:34 am
Union All is generally faster than union because it doesn’t have to do a sort / unique step.
With union all, you smoosh two data sets together not caring if there are dups or not. union (distinct) has to then go that extra step to remove dups.
TANSTAAFL
Comment :: November 8, 2007 @ 10:48 am
Hey Scott,
This is what I mentioned too. The point was not to see if it is faster but to get the measure of the thing in numbers.
Comment :: November 8, 2007 @ 2:06 pm
[...] Warum? UNION vs UNION ALL Performance | MySQL Performance Blog __________________ Siste gradum teque aspectu ne subtrahe nostro! Quem fugis? Extremum fato, quod [...]
Pingback :: December 5, 2007 @ 4:56 pm
sldfj
Comment :: March 21, 2008 @ 2:45 am
great article! (as usual)
thanks a lot.
i was looking for this and this text was very helpful
keep the good work, boys
Comment :: March 25, 2008 @ 3:45 pm
Hi,
Can someone answer my related query posted in the mysql performance forum.Query can be found here:
http://forum.mysqlperformanceblog.com/s/mv/tree/743/
Thanks.
Comment :: May 18, 2008 @ 11:55 pm