MySQL View containing UNION is not optimized well ... In other words, SLOW!

I have a view containing UNION ALL. For instance:

CRATE VIEW myView as
(SELECT col1, col2, col3
 FROM tab1)
UNION ALL
(SELECT col1, col2, col3
 FROM tab2)

These are large tables containing 10 milliseconds each. If I write:

SELECT * 
FROM myView
LIMIT 1;

instead of being immediate, in principle, it never returns, like other requests written against this submission. If I use LIMIT in a query for individual base tables, this is immediate. I have indexes on base tables. It seems that MySQL creates the entire aggregate data set (queries in the view) for presentation before applying any filtering criteria. This is madness. So does MySQL optimize view queries? By the way, I can’t even run an explanation plan against a presentation because it never returns.

+3
1

, , - , . MySQL , ...

- // - :

SELECT * 
  FROM myView
 LIMIT 1

... :

SELECT x.*
  FROM (SELECT col1, col2, col3
          FROM TAB1
        UNION ALL
        SELECT col1, col2, col3
          FROM TAB2) x
 LIMIT 1

ORDER BY, , :

SELECT col1, col2, col3
  FROM TAB1
 LIMIT 1

... TAB2 - , UNIONed. 10 ...

+6

Source: https://habr.com/ru/post/1773720/


All Articles