tags:

views:

27

answers:

1

Hey,

Let's say I want to perform this query:

(SELECT a FROM t1 WHERE a=10 AND B=1) 
UNION ALL 
(SELECT a FROM t2 WHERE a=11 AND B=2) 
UNION ALL 
(SELECT a FROM t3 WHERE a=12 AND B=3) 
ORDER BY a LIMIT 1000;

Is MySQL smart enough to skip "t3" if 550 results are available in "t1" and 450 in "t2"?

I'm looking at MySQL docs (http://dev.mysql.com/doc/refman/5.1/en/union.html) but can't seem to find the answer.

Thanks for your help.

A: 

As specified in UNION Syntax description (http://dev.mysql.com/doc/refman/5.1/en/union.html):

The default behavior for UNION is that duplicate rows are removed from the result. The optional DISTINCT keyword has no effect other than the default because it also specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.

I suppose, that's the answer to your question.

Kel
Got it, thanks for your answer
Fretre
In the result set, does data from t1 will always be before t2 and t3 and t2 before t3? I need to read the data ordered by "B ASC" and wondering if I can skip the "order by B ASC" following the unions.
Fretre
Is specified in manual, "UNION by default produces an unordered set of rows", so, as far as I understand, there's no guarantee.
Kel