opyeb SQLPlan

Guides · Indexes and rewriting

MySQL EXPLAIN ANALYZE: the tree format and what is different from PostgreSQL

Updated 28 Nov 2026 · 7 min read

MySQL 8.0.18 and later support EXPLAIN ANALYZE SELECT ..., which executes the statement and prints a tree with estimated cost, actual time, rows and loops. The format is visually similar to PostgreSQL's but the node names and the details differ. A MySQL plan uses terms such as Table scan, Index lookup, Covering index lookup, Filter, Sort and Nested loop inner join.

A typical line looks like -> Nested loop inner join (cost=0.70 rows=10) (actual time=0.050..120.400 rows=10 loops=1). Its children are indented with four spaces, and each child may show a Filter detail line. The indentation is the tree, exactly as in PostgreSQL. The actual time is per loop; the total is the value multiplied by loops.

MySQL nodeMeaningPostgreSQL analogue
Table scanReads the whole tableSeq Scan
Index lookupFinds rows through an indexIndex Scan
Covering index lookupAnswers from the index onlyIndex Only Scan
Single-row index lookupUnique lookup per outer rowIndex Scan with a unique condition
Nested loop inner joinNested loop joinNested Loop
FilterApplies a condition to rowsFilter detail on a scan

What the numbers mean

The cost is an abstract unit from MySQL's optimizer, not milliseconds. The actual time is measured and shown as first row to last row in milliseconds, with rows and loops. As in PostgreSQL, a node on the inner side of a nested loop reports per-loop values, so a lookup that shows 0.001 ms with 1,000 loops accounts for one second. The analyzer multiplies before comparing.

MySQL's tree does not print buffer counts the way PostgreSQL does. It has its own instrumentation: the performance schema, status variables such as Handler_read_*, and the slow query log with rows examined. When a MySQL plan is slow, the tree tells you the shape and the row counts; the I/O evidence comes from those other sources.

Table scan is not always bad

A table scan reads the whole table, like a sequential scan. It is the right plan for a small table or a query that needs most of the rows. MySQL may also use a covering index scan, which reads only the index and is cheaper than a table scan when the index contains all needed columns. The tree says Covering index lookup or Index scan in that case.

A single-row index lookup inside a nested loop is the MySQL equivalent of a nested loop with an indexed inner side. It is fast per row and can be slow in total if the outer side has many rows. The loops count is the key number. If the outer side is large and the inner lookup is not unique, the total work can be much larger than the line suggests.

Statistics and plan changes

MySQL uses its own statistics, collected by ANALYZE TABLE and by InnoDB's persistent statistics. A table that has changed a lot may need ANALYZE TABLE orders; before the optimizer sees the new distribution. The optimizer trace, SET optimizer_trace='enabled=on';, shows the alternatives it considered and why it chose one, which is useful when the plan is surprising.

MySQL also has optimizer hints and switches that can force or discourage an index, but they are a last resort. The same advice as for PostgreSQL applies: fix statistics, rewrite the predicate, and design the index before forcing a plan. A forced plan that is fast today can be wrong tomorrow when the data changes.

A reading checklist

  1. Identify the root and the join shape: nested loop, hash join or a single table.
  2. Check the actual time and rows on each node against the estimate.
  3. Multiply inner-side time by loops before comparing.
  4. Look for Table scan nodes with filters that discard most rows.
  5. Check whether a covering index could answer the query from the index alone.
  6. Use ANALYZE TABLE and the optimizer trace when the plan does not match the data.

The analyzer parses both engines' text trees with the same indentation-based logic. It labels MySQL table scans as scans, index lookups as index access, and applies the same nested-loop and estimate rules. The node names differ but the questions are the same: how many rows, how many loops, and where is the time.

The tree format is shared; the details are not. Read MySQL's node names literally and use its own instrumentation for I/O.

Sources

Questions

What version do I need for EXPLAIN ANALYZE?

MySQL 8.0.18 introduced EXPLAIN ANALYZE with the tree output and actual time. Before that, EXPLAIN FORMAT=TREE gave the tree without execution and EXPLAIN FORMAT=JSON gave estimates. On older versions, use EXPLAIN and the optimizer trace for estimates, and measure the query separately.

Why does MySQL show loops on an index lookup?

MySQL's nested-loop joins execute the inner side once per outer row, and the tree shows loops on each node. A single-row index lookup with loops=1000 ran 1,000 times, once for each outer row. Multiply the actual time by the loops, just as in PostgreSQL.

Does MySQL have a cost model like PostgreSQL's?

Yes, but the constants and the output differ. The tree shows a cost per node in an abstract unit, and the optimizer chooses plans by comparing those costs. The actual time is measured in milliseconds. Comparing cost across engines is meaningless; compare measured time on the same server.

Load the MySQL sample: a table scan feeds a single-row index lookup in a nested loop.

Open the analyzer

Written against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.

Back to the guides