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.
| Column | What it tells you |
|---|---|
table | Which table this row of the plan is about |
type | How rows are found: const, eq_ref, ref, range, index, ALL (best to worst) |
possible_keys / key | Indexes available / index chosen |
rows | Estimated rows to examine |
Extra | Notes: Using index, Using where, Using filesort, Using temporary |
A Full Scan
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ALL | NULL | 19696 | Using where |
type is ALL: all 20,000 rows are read and filtered.
With the Right Index
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ref | idx_big_country_created | 55 | NULL |
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):
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | index | PRIMARY | 5 | Using 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:
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ALL | NULL | 19713 | Using filesort |
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | index | idx_big_email | 5 | Using index |
The Tree Format
FORMAT=TREE shows the plan as nested steps with cost and row estimates, closer to how the executor works:
MySQL plan (tree format):
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.
The output has this shape. Numbers differ on every run and machine, so they are not shown here as a fixed result:
costandrowsbefore the arrow are the optimizer's estimateactual time=A..Bis time to the first row and to the last row, in millisecondsactual rowsis the real number, andloopsis how many times the step ran- A big gap between estimated and actual rows means stale statistics. Run
ANALYZE TABLE
Join Order
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| c | ALL | NULL | 6 | Using where |
| o | ref | customer_id | 1 | Using 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
- Run
EXPLAINand look fortype: ALLon big tables. - Check
keyandrows: is the best index used? - Look at
ExtraforUsing filesortorUsing temporaryon large sets. - Add or change an index, run
ANALYZE TABLE, and explain again. - Use
EXPLAIN ANALYZEto compare estimate with reality. - Repeat with real production-sized data.
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Judging a query on 10 rows | Full scans look fine | Test with realistic volume |
Ignoring rows | Missing the real cost | Read estimated and actual rows |
EXPLAIN ANALYZE on a write | It actually runs | Use plain EXPLAIN for writes |
| Stale statistics | Bad plans | ANALYZE TABLE |
| Forcing an index with a hint first | Hides the real problem | Fix the index and query, then hint if needed |
Practice
- Explain a query on
big_usersfiltering bycreated, then add an index and compare. - Find a query that shows
Using filesort, and remove it with an index. - Use
FORMAT=TREEand read the steps from the inside out. - Run
EXPLAIN ANALYZEon aSELECTand 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.

