# Does MySQL optimize UNION with a LIMIT clause?

**URL:** <https://forums.percona.com/t/does-mysql-optimize-union-with-a-limit-clause/438>\
**Category:** Other MySQL® Questions\
**Created:** [September 7, 2007, 5:16am UTC](https://forums.percona.com/t/does-mysql-optimize-union-with-a-limit-clause/438 "2007-09-07T05:16:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spuerhund](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Spuerhund](https://forums.percona.com/u/Spuerhund)\
**Post date:** [September 7, 2007, 5:16am UTC](https://forums.percona.com/t/does-mysql-optimize-union-with-a-limit-clause/438/1 "2007-09-07T05:16:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Peter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/peter/32/2_2.png) [@Peter](https://forums.percona.com/u/Peter)\
**Post date:** [September 10, 2007, 10:25am UTC](https://forums.percona.com/t/does-mysql-optimize-union-with-a-limit-clause/438/2 "2007-09-10T10:25:50Z")

</div>

As far as I remmeber it does not.

So keep limits for inner queries )
