MySQL EXPLAIN and EXPLAIN ANALYZE: Reading Query Plans

MySQL 60 min min read Updated: Nov 27, 2026 Intermediate
MySQL EXPLAIN and EXPLAIN ANALYZE: Reading Query Plans
Intermediate Topic 2 of 3

What EXPLAIN Does

Put EXPLAIN in front of a SELECT, UPDATE or DELETE and MySQL shows the plan without running it. It is the first tool for a slow query.

ColumnWhat it tells you
tableWhich table this row of the plan is about
typeHow rows are found: const, eq_ref, ref, range, index, ALL (best to worst)
possible_keys / keyIndexes available / index chosen
rowsEstimated rows to examine
ExtraNotes: Using index, Using where, Using filesort, Using temporary

A Full Scan

sql EXPLAIN SELECT * FROM big_users WHERE country = 'IN' AND created = '2024-03-01';

MySQL plan:

tabletypekeyrowsExtra
big_usersALLNULL19696Using where

type is ALL: all 20,000 rows are read and filtered.

With the Right Index

sql EXPLAIN SELECT * FROM big_users WHERE country = 'IN' AND created = '2024-03-01';

MySQL plan:

tabletypekeyrowsExtra
big_usersrefidx_big_country_created55NULL

Sorting: Using filesort

filesort does not always mean a file on disk. It means MySQL sorts rows itself instead of reading them in index order:

First a sort that needs no sorting, because the primary key is already in order (note the backward index scan and 5 rows read):

sql EXPLAIN SELECT id FROM big_users WHERE country = 'IN' ORDER BY id DESC LIMIT 5;

MySQL plan:

tabletypekeyrowsExtra
big_usersindexPRIMARY5Using where; Backward index scan

Now a sort on a column without an index, which needs a filesort of every row, followed by the same query with an index:

sql EXPLAIN SELECT id FROM big_users ORDER BY email LIMIT 5;

MySQL plan:

tabletypekeyrowsExtra
big_usersALLNULL19713Using filesort
sql EXPLAIN SELECT id FROM big_users ORDER BY email LIMIT 5;

MySQL plan:

tabletypekeyrowsExtra
big_usersindexidx_big_email5Using index

The Tree Format

FORMAT=TREE shows the plan as nested steps with cost and row estimates, closer to how the executor works:

sql EXPLAIN FORMAT=TREE SELECT country, COUNT(*) FROM big_users WHERE created >= '2024-06-01' GROUP BY country;

MySQL plan (tree format):

text -> Group aggregate: count(0) (cost=2720 rows=5) -> Filter: (big_users.created >= DATE'2024-06-01') (cost=2046 rows=6738) -> Index scan on big_users using idx_big_country_created (cost=2046 rows=20216)

EXPLAIN ANALYZE: Estimates vs Reality

EXPLAIN ANALYZE (8.0.18+) runs the query and adds actual time and actual rows next to the estimates. Always remember it executes the statement, so do not use it on a DELETE you do not want to run.

sql EXPLAIN ANALYZE SELECT * FROM big_users WHERE country = 'IN' AND created = '2024-03-01';

The output has this shape. Numbers differ on every run and machine, so they are not shown here as a fixed result:

text -> Index lookup on big_users using idx_big_country_created (country='IN', created=DATE'2024-03-01') (cost=... rows=N) (actual time=A..B rows=N loops=1)
  • cost and rows before the arrow are the optimizer's estimate
  • actual time=A..B is time to the first row and to the last row, in milliseconds
  • actual rows is the real number, and loops is how many times the step ran
  • A big gap between estimated and actual rows means stale statistics. Run ANALYZE TABLE

Join Order

sql EXPLAIN SELECT c.name, o.id FROM customers c JOIN orders o ON o.customer_id = c.id WHERE c.city = 'London';

MySQL plan:

tabletypekeyrowsExtra
cALLNULL6Using where
orefcustomer_id1Using index

Small tables like these may be read fully because it is cheaper than an index. Plans change with table size, so test on realistic data.

Checklist for a Slow Query

  1. Run EXPLAIN and look for type: ALL on big tables.
  2. Check key and rows: is the best index used?
  3. Look at Extra for Using filesort or Using temporary on large sets.
  4. Add or change an index, run ANALYZE TABLE, and explain again.
  5. Use EXPLAIN ANALYZE to compare estimate with reality.
  6. Repeat with real production-sized data.

Common Beginner Mistakes

MistakeWhat happensFix
Judging a query on 10 rowsFull scans look fineTest with realistic volume
Ignoring rowsMissing the real costRead estimated and actual rows
EXPLAIN ANALYZE on a writeIt actually runsUse plain EXPLAIN for writes
Stale statisticsBad plansANALYZE TABLE
Forcing an index with a hint firstHides the real problemFix the index and query, then hint if needed

Practice

  1. Explain a query on big_users filtering by created, then add an index and compare.
  2. Find a query that shows Using filesort, and remove it with an index.
  3. Use FORMAT=TREE and read the steps from the inside out.
  4. Run EXPLAIN ANALYZE on a SELECT and compare estimated and actual rows.

Conclusion

EXPLAIN shows the plan, type, key, rows and Extra reveal full scans and sorts, the tree format shows nested steps, and EXPLAIN ANALYZE adds real timings.

Quick Summary: EXPLAIN, type, key, rows, Extra, filesort, FORMAT=TREE, EXPLAIN ANALYZE.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators