Is the Query Cache turned on? It could be that the two individual SELECTs
are artificially fast due to getting the resultset from the QC.
Let's dissect the UNION
to understand its sluggishness:
- Create a temp table for the resultset.
- Perform first
SELECT
, writing results to that temp.
- Perform second
SELECT
, writing results to that temp.
- Read the temp table.
- (De-duplicate. This did not happen since you said
ALL
on UNION
.)
- Deliver the results to the client.
- Drop the temp table.
Two things are probably aggravating the situation:
- Windows is slower than *NIX at building tables.
- It was not until a very recent versions of MySQL that some
UNIONs
can avoid the temp table. That is, in the future, the results from each SELECT
can be delivered directly to the client. You apparently do not have such version.
As for the parentheses, leave them on. Put them on the other SELECT
, too. It does not impact performance, but it may impact the result. Imagine, for example, what the GROUP BY
applies to in these three cases:
-- Case 1
SELECT .. UNION SELECT .. GROUP BY ..
-- Case 2
SELECT .. UNION ( SELECT .. GROUP BY .. )
-- Case 3
( SELECT .. ) UNION ( SELECT .. ) GROUP BY ..
The first one is the same as the second, not the third.