Pcornel
Databases · Indexes & Query Plans

Reading EXPLAIN Plans

Cheat sheet · https://pcornel.dk/topic.php?topic=6171

Back to the Topic

Answer

An EXPLAIN plan shows how the database will (or did, with ANALYZE) run a query: scans, joins, sorts, estimated and actual rows, and costs.

Remember

Read from the innermost node. Big gaps between estimated and actual rows mean bad statistics. Look for sequential scans on large tables, nested loops over many rows, and sorts spilling to disk.

First checks

  • EXPLAIN (ANALYZE, BUFFERS) <query> — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Verify in this order

query → planner → plan tree → execution with actual timings

Where it usually breaks

Running EXPLAIN ANALYZE on a DELETE deletes the rows.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6171

Confirmation

Confirm action

Continue?