Home » Performance » MySQL » Does MySQL optimize UNION with a LIMIT clause?
Does MySQL optimize UNION with a LIMIT clause? [message #1758] Fri, 07 September 2007 06:46 Go to next message
Spuerhund  is currently offline Spuerhund
Messages: 4
Registered: August 2007
Junior Member
Assume i have a query like:
(SELECT a FROM t1)
UNION ALL
(SELECT a FROM t2)
ORDER BY a LIMIT 10;

Will MySQL be so clever and execute it in the same way (and time) as:
(SELECT a FROM t1 LIMIT 10)
UNION ALL
(SELECT a FROM t2 LIMIT 10)
ORDER BY a LIMIT 10;

In other words:
will the limit clause from the outer UNION statement be applied to the inner SELECT queries automatically? Otherwise the inner queries might return a lot (millions) of rows, although only the first 10 rows have a chance to appear in the final result.
Re: Does MySQL optimize UNION with a LIMIT clause? [message #1782 is a reply to message #1758 ] Mon, 10 September 2007 11:55 Go to previous message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
As far as I remmeber it does not.

So keep limits for inner queries Smile


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Previous Topic:mysql on windows decreasing perf
Next Topic:CPU 100%
Goto Forum:
  


Current Time: Thu Jul 9 21:50:35 EDT 2009

Total time taken to generate the page: 0.09528 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software

MySQL is a trademark of Sun Microsystems.
InnoDB is a trademark of Oracle Corp.

Percona Performance Forums are a service of Percona, Inc.
Not affiliated with Sun Microsystems or Oracle Corp.