Announcement

Announcement Module
Collapse
No announcement yet.

Does MySQL optimize UNION with a LIMIT clause?

Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does MySQL optimize UNION with a LIMIT clause?

    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.

  • #2
    As far as I remmeber it does not.

    So keep limits for inner queries )

    Comment

    Working...
    X