MySQL EXPLAIN Visualizer
Operator: Abdulla Al Fahad. How tools are built and checked
A MySQL plan in JSON runs to hundreds of lines, and the fact that explains a slow query, a table read in full to keep a handful of rows, sits somewhere in the middle of it. This page draws EXPLAIN FORMAT=JSON and EXPLAIN ANALYZE output as a tree, marks the nodes that read far more than they keep, and writes a hint for each pattern it recognises in terms of MySQL's own features.
Key features
- Reads EXPLAIN FORMAT=JSON from 5.6 onward, including the cost_info block, and the indented tree text from EXPLAIN FORMAT=TREE and EXPLAIN ANALYZE (8.0.16 and 8.0.18 onward)
- Each table node shows access type (ALL, index, range, ref, eq_ref, const), key, possible keys, rows examined per scan, rows produced, filtered percent, cost, and the attached or index condition
- Flags for filesort, temporary table, join buffer, hash join, dependent subqueries and covering index, shown as badges on the node; every node, with its numbers, exports as CSV and the hints are sorted from high to info
- For ANALYZE, self time per node, actual rows against estimated rows, loops, and the five nodes that account for the most time
What the JSON and tree plans contain and how the hints are derived
In the JSON form, query_block holds either a single table, a nested_loop list in join order, or operation wrappers such as ordering_operation, grouping_operation and duplicates_removal around them. Costs arrive as strings. A table's rows_produced_per_join is the cumulative row count after that join step, so the page uses the previous table's value as the number of times the next table is probed.
The tree form is one line per iterator, indented four spaces per level, with the estimate in (cost=... rows=...) and, under ANALYZE, (actual time=first..last rows=... loops=...). The times are per loop and in milliseconds, so a node's total is its last-row time multiplied by loops, and self time is that total minus the totals of its children.
How it works
The tree is built with budgets: up to 20,000 nodes, 120 levels of nesting and 8 million characters of input. Input that is the classic tabular EXPLAIN is recognised and turned away with the exact command to run instead.
Index suggestions come from the conditions printed in the plan. Equality comparisons on a table's own columns go first and one range column goes last, which is the usual order for a composite B-tree index; a column wrapped in a function is left out.
When this helps
- A report that ran in 0.2 s in staging takes 14 s in production; the production plan shows ALL on the largest table where staging showed range, and the hint names the possible keys the optimizer ignored.
- A developer adds WHERE DATE(created_at) = CURDATE() to a dashboard query and gets a scan of 9 million rows; the function hint explains the cause and gives the range rewrite.
- A catalogue page shows a review count per product with a correlated subquery, SELECT COUNT(*) FROM reviews r WHERE r.product_id = p.id, for 100 products. In an ANALYZE tree shaped like that, the Select #2 line is marked dependent and the scan of reviews has loops=100 over 40,000 rows: the page reports about 4,000,000 rows read, raises a Dependent subquery hint and suggests an index on product_id.
- A support engineer receives a plan from a customer and cannot reach their database; the CSV of nodes can be attached to the ticket and read without any connection.
Opening a file
- Run EXPLAIN FORMAT=JSON <query> or EXPLAIN ANALYZE <query> in the mysql client and copy the whole result, braces or arrows included.
- Paste it, drop a saved file, or load one of the two built-in plans, then choose Visualize plan.
- Read the hints, then open a node from the tree to see its key, conditions and cost.
Example
An orders-to-customers join with GROUP BY, ORDER BY and LIMIT, as FORMAT=JSON and as ANALYZE text
SELECT c.name, SUM(o.total) FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.status = 'shipped' AND YEAR(o.created_at) = 2024 GROUP BY c.id ORDER BY 2 DESC LIMIT 10;Hints from the JSON plan (query cost 61,853.17)
high: full table scan on o reads about 199,800 rows, 1% filtered; an index on status is suggested
high: year(created_at) wraps a column, so an ordinary index cannot be used; compare a date range instead
medium: filesort on the ORDER BY
medium: temporary table for the GROUP BY
The ANALYZE example (412.8 ms) adds: estimate 1,998 rows, actual 48,307 (24x too low); the table scan is 151.9 ms and the 48,307 primary-key lookups 149.8 msTips for best results
- Prefer EXPLAIN ANALYZE when the query can safely run, since it executes the statement and reports real rows; use FORMAT=JSON for a statement you must not run.
- Read rows examined per scan against loops: a table with 1,000 rows probed 50,000 times is 50 million row reads even though each scan looks small.
- Check the Key and Possible keys lists together. A key of NULL with candidates listed means the optimizer judged a scan cheaper, which is a statistics question as much as an index one.
- On 8.0.13 and later, an index on the expression itself, such as ((YEAR(created_at))), is an alternative to rewriting a function condition.
Limits
FORMAT=JSON shows estimates. A node that looks cheap can still be slow if the estimate is wrong, which only ANALYZE reveals.
Self time is derived from averages (last-row time multiplied by loops), so parts that overlap, such as a hash join's build side, are attributed to the parent and the percentages can add to more than 100.
Common errors and how to fix them
- This is the classic tabular EXPLAIN.
- Run the statement again with EXPLAIN FORMAT=JSON, or with EXPLAIN ANALYZE on MySQL 8.0.18 or later, and paste that output instead.
- This JSON has no query_block object.
- Paste the value printed by EXPLAIN FORMAT=JSON, not a row from the slow query log or the tabular plan.
Reading the access types
const and eq_ref are single-row lookups through a primary or unique key and are rarely the problem. ref and range read a slice of an index. index reads the whole index and then usually the table rows, and ALL reads the whole table. The hints fire on ALL and index when the rows read over the whole query, counting probes by earlier tables, pass about a thousand and ten thousand rows respectively; a full scan of a 12-row lookup table is not reported.
Frequently asked questions
Does it work for MariaDB?
Only partly. MariaDB's JSON plans follow a similar layout but differ in detail, and it has no EXPLAIN ANALYZE tree text, so this page is written against Oracle MySQL 5.7 and 8.0 output and refuses JSON that has no query_block.
How many rows does a table joined by hash join count as reading?
One scan. A hash join (the replacement for the block nested loop since MySQL 8.0.18) buffers the earlier rows and reads the inner table once, so the page does not multiply its rows by the number of outer rows. In a JSON plan the using_join_buffer field marks this; the Join buffer hint still says that no index supports the join column.
Why is a filesort rated medium in one plan and info in another?
A sort under a LIMIT keeps only the top rows in a small priority queue, which is cheap, and a sort of fewer than 10,000 rows is also rated info. A large sort without a limit is rated medium.