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 typePostgreSQL nodeMeaningGood or bad?
const / eq_refIndex Scan (unique)At most one row via unique keyExcellent
ref / rangeIndex Scan / Bitmap Index ScanSeek then read matching entriesGood
indexIndex Only Scan / full index scanRead the whole indexOK if covering, else suspicious
ALLSeq ScanRead every rowFine for small tables, bad on big ones
Using filesortSortSeparate sort stepCostly on large sets; index may avoid it
Using temporaryHashAggregate / MaterializeTemp table neededWatch for large row counts

Reading a PostgreSQL plan

psql -c "EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = 42 ORDER BY created_at DESC LIMIT 20"
Limit (cost=0.43..18.9 rows=20) (actual time=0.031..0.088 rows=20 loops=1)
Buffers: shared hit=24
-> Index Scan using idx_orders_cust_created on orders (cost=0.43..1702.5 rows=1840)
Index Cond: (customer_id = 42)
Planning Time: 0.15 ms
Execution Time: 0.11 ms
A healthy plan: index scan, no sort node, 24 cached pages read.

How to read any plan

  1. Find the most expensive node (highest actual time or rows)
  2. Compare estimated rows vs actual rows; 10x+ off means stale stats or skew
  3. Look for Seq Scan / type=ALL on large tables
  4. Look for Sort / filesort and Rows Removed by Filter
  5. Fix: add or reorder an index, rewrite the predicate, or ANALYZE
A checklist for any slow query plan.

Reading a MySQL plan

mysql> EXPLAIN SELECT * FROM orders WHERE YEAR(created_at) = 2026;
+----+-------+------+---------------+------+---------+------+--------+-------------+
| id | table | type | possible_keys | key | key_len | ref | rows | Extra |
| 1 | orders| ALL | NULL | NULL | NULL | NULL | 984211 | Using where |
type=ALL and key=NULL: the function on created_at prevents index use
MySQL tabular EXPLAIN showing a full scan caused by wrapping the column in a function.

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_at

Refresh statistics

Stale statistics lead to wrong estimates.

ANALYZE orders;                 -- PostgreSQL
ANALYZE TABLE orders;           -- MySQL

Find 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.

More Databases articles