Skip to content

MySQL and InnoDB in depth · Part 8 of 10

Reading EXPLAIN and EXPLAIN ANALYZE in MySQL

EXPLAIN shows how MySQL plans to run a query, and EXPLAIN ANALYZE shows what really happened. Learn the columns that matter and a repeatable method for tuning a slow query.

· 4 min read

mysqlperformancesql

Query tuning without measurement is guessing. MySQL gives you two tools for looking inside a query: EXPLAIN describes the plan the optimizer chose, and EXPLAIN ANALYZE runs the query and reports what actually happened. Reading them well turns "this is slow" into a specific, fixable cause.

EXPLAIN and EXPLAIN ANALYZE

Code
EXPLAIN SELECT id, amount FROM payments
WHERE wallet_id = 7 ORDER BY created_at DESC LIMIT 20;

EXPLAIN ANALYZE SELECT id, amount FROM payments
WHERE wallet_id = 7 ORDER BY created_at DESC LIMIT 20;

EXPLAIN shows estimates and does not run the statement. EXPLAIN ANALYZE, available from MySQL 8.0.18, executes it and adds the real time and row counts for each step. Be careful with it on statements that change data, because they are executed.

The columns that matter

  • type: the access method, from best to worst: const and eq_ref (a single row by unique key), ref (matching rows by an index), range (an index range), index (a full index scan) and ALL (a full table scan).
  • key: the index actually used. NULL means none.
  • possible_keys: the indexes MySQL considered. If your index is here but not in key, the optimizer decided it was not worth it.
  • rows: the estimated number of rows to examine. Smaller is better, and a large number next to a small LIMIT is a red flag.
  • filtered: the percentage of examined rows expected to survive the conditions.
  • Extra: notes about how the query runs, covered next.

Reading the Extra column

  • Using index: a covering index answered the query without touching the table. Good.
  • Using index condition: index condition pushdown filters inside the index before fetching rows. Usually good.
  • Using where: some conditions are checked after rows are read. Fine, unless the number of examined rows is large.
  • Using filesort: MySQL sorts rows itself instead of reading them in index order. Watch it on big results.
  • Using temporary: a temporary table is needed, often for GROUP BY or DISTINCT. Watch it too.

What EXPLAIN ANALYZE adds

The output is a tree. Each node shows the estimated cost and rows, then actual time for the first and last row, the real rows returned and the number of loops. Compare estimated rows with actual rows. A large gap means the optimizer's statistics are stale or misleading, and running ANALYZE TABLE can refresh them. Multiply the per-loop time by the loops to find the step where time really goes, especially inside nested joins.

A tuning method

  1. Find the slow query with data. Use the slow query log or the Performance Schema digest tables, with a sensible long_query_time, and pick the statements that cost the most in total, not only the slowest single run.
  2. Look at the plan. Is it scanning many rows, sorting, or building a temporary table?
  3. Form one hypothesis. For example, "the filter column has no index" or "the index order does not match the sort".
  4. Change one thing: add or reorder an index, or rewrite the query.
  5. Measure again with the same data volume and compare before and after, both the plan and the real time.
  6. Check the side effects. A new index slows writes and takes space. Other queries might change plan.

Test with data of realistic size. On a tiny table the optimizer may prefer a full scan, and a plan that looks fine at a thousand rows can collapse at fifty million.

Takeaway

Use EXPLAIN to see the plan and EXPLAIN ANALYZE to see reality. Check type, key, rows and Extra, compare estimates with actual rows, and change one thing at a time. A good optimization story has a measured before, a clear cause, a targeted change and a measured after.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.