Reading EXPLAIN Plans to Check Index Usage (MySQL and PostgreSQL)
Learn to read EXPLAIN and EXPLAIN ANALYZE output in MySQL and PostgreSQL: scan types, key, rows, cost, filesort, and how to spot missing indexes.
Published November 27, 2026
Guessing whether a query uses an index is a waste of time when the database will tell you. EXPLAIN shows the plan the optimizer chose: which tables it reads, in what order, with which index, and how many rows it expects. EXPLAIN ANALYZE actually runs the query and adds real timings. Learning to read both is the single most useful database skill after writing SQL.
The concept
The optimizer is cost-based. It estimates how many rows each step will produce using table statistics (row counts, value distributions, histograms) and chooses the plan with the lowest estimated cost. When estimates are wrong, usually because statistics are stale or data is skewed, it can choose badly, for example a full scan instead of an index or a nested loop over millions of rows.
That is why EXPLAIN ANALYZE is so valuable: it shows estimated rows next to actual rows. A big mismatch points straight at the problem. In PostgreSQL, add BUFFERS to see how many pages were read from cache versus disk. In MySQL 8.0.18+, EXPLAIN ANALYZE prints a tree with actual timings, and EXPLAIN FORMAT=TREE shows the plan structure.
What the scan types mean
| MySQL type | PostgreSQL node | Meaning | Good or bad? |
|---|---|---|---|
| const / eq_ref | Index Scan (unique) | At most one row via unique key | Excellent |
| ref / range | Index Scan / Bitmap Index Scan | Seek then read matching entries | Good |
| index | Index Only Scan / full index scan | Read the whole index | OK if covering, else suspicious |
| ALL | Seq Scan | Read every row | Fine for small tables, bad on big ones |
| Using filesort | Sort | Separate sort step | Costly on large sets; index may avoid it |
| Using temporary | HashAggregate / Materialize | Temp table needed | Watch for large row counts |
Reading a PostgreSQL plan
How to read any plan
- Find the most expensive node (highest actual time or rows)
- Compare estimated rows vs actual rows; 10x+ off means stale stats or skew
- Look for Seq Scan / type=ALL on large tables
- Look for Sort / filesort and Rows Removed by Filter
- Fix: add or reorder an index, rewrite the predicate, or ANALYZE
Reading a MySQL plan
Practical examples
Fix the plan above
Rewrite as a range so the index on created_at can be used.
SELECT * FROM orders
WHERE created_at >= '2026-01-01' AND created_at < '2027-01-01';
-- type: range, key: idx_orders_created_atRefresh statistics
Stale statistics lead to wrong estimates.
ANALYZE orders; -- PostgreSQL
ANALYZE TABLE orders; -- MySQLFind slow queries to explain in the first place
Use the built-in statistics views.
-- PostgreSQL (pg_stat_statements extension)
SELECT calls, round(mean_exec_time::numeric, 1) AS ms, query
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;
-- MySQL performance_schema
SELECT digest_text, count_star, round(avg_timer_wait/1e9, 1) AS avg_ms
FROM performance_schema.events_statements_summary_by_digest
ORDER BY sum_timer_wait DESC LIMIT 10;Common mistakes
- Running EXPLAIN ANALYZE on a DELETE or UPDATE in production; it really executes. Wrap it in BEGIN ... ROLLBACK.
- Reading only the top cost number instead of finding the slow node.
- Ignoring estimate-vs-actual mismatches, the clearest sign of the root cause.
- Testing plans on a tiny dev database where everything is a sequential scan.
- Forcing indexes with hints before understanding why the optimizer avoided them.
Key takeaways
- EXPLAIN shows the chosen plan; EXPLAIN ANALYZE runs it with real timings.
- Seq Scan / type=ALL on big tables and Sort / filesort are the usual red flags.
- Large estimated-vs-actual row differences point to stale statistics or skew.
- Find candidate queries with pg_stat_statements or performance_schema.
FAQ
What does cost=0.43..18.9 mean in PostgreSQL?
Arbitrary planner units: startup cost (before the first row) and total cost. They are useful for comparing plans, not as milliseconds.
Why did the optimizer choose a full scan when an index exists?
It estimated that a large fraction of rows match, so scanning sequentially is cheaper than many random index lookups. That is often correct.
Is there a visual EXPLAIN tool?
Yes. MySQL Workbench has Visual Explain, and for PostgreSQL tools like explain.dalibo.com and pgMustard visualise JSON plans.