Thank you, that is what I was thinking, however the next problem comes when I do LIMIT BY 10, 21.
By performing the limit by 3 times on query 2 the results aren't the same as query 1, anyway around this?
Main Topics
Browse All TopicsIf I have two select statements and use union to join them where is the best place to put order by and limit.
For example:
( select * AS alias_names FROM T1 )
UNION
( select * AS alias_names FROM T2 )
ORDER BY alias_id DESC
LIMIT BY 1,11
or would this be better:
( select * AS alias_names FROM T1 ORDER BY T1_id DESC LIMIT BY 1,11)
UNION
( select * AS alias_names FROM T2 ORDER BY T2_id DESC LIMIT BY 1,11)
ORDER BY alias_id DESC
LIMIT BY 1,11
I know alias names can not be used like that.
I'm interested in the performance issues of both queries, which query is faster?
My thoughts are Query 1 is faster up to X records and then Query 2 is faster, but what is X?
I have looked at the MySQL Reference Manual it seems to suggest this, can anyone provide additional information about this and experience with it?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
use UNION ALL ! You wil avoid an unecassary sort :-)
Most important is question about indexes. if you have index on T1_id and T2_id is it ok to use
( select * AS alias_names FROM T1 ORDER BY T1_id DESC LIMIT 11)
UNION ALL
( select * AS alias_names FROM T2 ORDER BY T2_id DESC LIMIT 11)
ORDER BY alias_id DESC
LIMIT 11
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. The following example uses both clauses:
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
read more at: http://dev.mysql.com/doc/r
Business Accounts
Answer for Membership
by: rrjegan17Posted on 2009-04-12 at 04:09:08ID: 24125128
Query 1:
1. Takes all records from T1 and then from T2 and Does UNION operation to join both results and then Limit the output to 11 records.
Query 2:
1. Takes only 11 records from T1 and 11 Records from T2 and does UNION operation on the results and then Finally Limit the Result to 11 records.
Based on the cost wise, Second query will perform better * if you have huge no of records in the tables T1 and T2. Otherwise both will give similar performance.
* -- It depends upon the Index on the alias_id column too.