EXPLAIN FORMAT=JSON: everything about attached_subqueries, optimized_away_subqueries, materialized_from_subquery

EXPLAIN FORMAT=JSON: everything about attached_subqueries, optimized_away_subqueries, materialized_from_subquery



The regular EXPLAIN command already provides some information about subquery optimization. For example, you can find out if the subquery is dependent or not, and (since version 5.6) if it was materialized:

However, you can’t find details on exactly how this subquery was materialized. To find out more, use EXPLAIN FORMAT=JSON:

We can see here that the new temporary table ( "using_temporary_table": true) with internal name <subquery2> was materilized from the subquery ( materialized_from_subquery). We also see how this new temporary table was accessed.

We also can see that the member that contains information about the materialized subquery is part of the  nested_loop array. Nested loop optimization applies to a regular JOIN command. In this case, it means that the MySQL Server would join the temporary table with an outer query.

Another interesting case is optimized_away_subqueries: this command shows subqueries that were executed only once and were replaced by their result. A regular EXPLAIN command provides no information about such optimization:

We can see that in select_type: SUBQUERY, the output of SHOW WARNINGS  doesn’t give us much insight either.

However,  EXPLAIN FORMAT=JSON tells us how this subquery was optimized:

After seeing optimized_away_subqueries and "cacheable": true, we know that the subquery was executed only once, and the result was cached and used to resolve the outer query.

Another subquery type that I want to mention in this post is attached_subqueries – which covers subqueries that are not converted to JOIN.

For these subqueries, using this option shows if they are dependent and/or cacheable. There is also a  query_block member inside the  attached_subqueries object which provides all the details on how the query was optimized.

Conclusion: EXPLAIN FORMAT=JSON  provides some great details on subqueries optimization.


Share this post

Comments (2)

  • Leonid Sokolovskiy Reply

    Sveta, could you point to a location where I can read a bit more about json format.
    Other question – are there tools which helps to visualise json plan. I came from SQL Server background and there are number of great free tools to visualise and provides stats about execution plan.
    One more – probably very ignorant question :). Am I right that produced plan is actually estimated and not the actual plan?
    In SQL Server you can get both but actual is much more useful because you get the real and not estimated numbers

    September 4, 2018 at 7:27 am

Leave a Reply