0Pricing
SQL Academy · Lesson

Reading EXPLAIN and EXPLAIN ANALYZE

Read the plan tree, understand cost vs actual time, and pick out the heavy nodes.

Reading EXPLAIN and EXPLAIN ANALYZE is a free SQL Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why EXPLAIN?

EXPLAIN shows the planner's strategy for executing a query — without running it. EXPLAIN ANALYZE runs the query AND shows actual timings.

Basic EXPLAIN

Show the estimated plan:

EXPLAIN SELECT * FROM orders WHERE user_id = 42;
--                            QUERY PLAN
-- ---------------------------------------------------------------
-- Index Scan using orders_user_id_idx on orders
--   (cost=0.43..8.45 rows=5 width=120)
--   Index Cond: (user_id = 42)

EXPLAIN ANALYZE

Runs the query and adds actual timings and row counts:

EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42;
-- Index Scan using orders_user_id_idx on orders
--   (cost=0.43..8.45 rows=5 width=120)
--   (actual time=0.041..0.043 rows=4 loops=1)
-- Planning Time: 0.082 ms
-- Execution Time: 0.057 ms

Reading the Tree

Plans are trees. Indent = child node. Children run before parents.

Aggregate
  -> Hash Join
     Hash Cond: (o.user_id = u.id)
     -> Seq Scan on orders
     -> Hash
        -> Seq Scan on users

Cost vs Time

The cost column has TWO numbers: startup_cost..total_cost. These are arbitrary planner units, not seconds. Use actual time for real numbers.

rows = Estimated; actual rows = Real

The rows is the planner's guess. actual rows is what really happened. If they're wildly different, statistics are off.

loops Multiplies Time

For nested loops, actual time=X loops=N means each iteration took X; total per node is X×N.

Nested Loop  (actual time=0.05..2.03 rows=500)
  -> Seq Scan on orders  (actual ... loops=1)
  -> Index Scan using users_pkey  (actual time=0.01..0.01 loops=500)

BUFFERS Tells You I/O

EXPLAIN (ANALYZE, BUFFERS) reports buffer hits and reads — the I/O behind the timings:

EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
-- Buffers: shared hit=12 read=4

FORMAT Options

For programmatic processing, use JSON or YAML:

EXPLAIN (ANALYZE, FORMAT JSON) SELECT ...;

Don't EXPLAIN ANALYZE Destructive Statements!

EXPLAIN ANALYZE on an UPDATE/DELETE/INSERT actually executes it. Wrap in a transaction with ROLLBACK if you want to inspect without committing:

BEGIN;
EXPLAIN ANALYZE DELETE FROM orders WHERE ...;
ROLLBACK;

Common Plan Nodes

  • Seq Scan — reads the whole table
  • Index Scan — uses an index
  • Bitmap Heap Scan — combines multiple indexes
  • Hash Join / Merge Join / Nested Loop — join strategies
  • Sort, Aggregate, Limit, Materialize

Plan-Reading Workflow

  1. Run EXPLAIN ANALYZE on the slow query
  2. Find the node with the highest actual time
  3. Compare estimated rows vs actual rows
  4. Check whether indexes are being used
  5. Iterate: add index / rewrite / ANALYZE

Recap

EXPLAIN ANALYZE is your primary performance tool.

  • Cost is relative, time is real
  • Estimated vs actual rows reveals stats issues
  • BUFFERS shows I/O
  • Wrap destructive queries in BEGIN; ... ROLLBACK

Quick Check

What's the difference between EXPLAIN and EXPLAIN ANALYZE?

Frequently asked questions

Is the “Reading EXPLAIN and EXPLAIN ANALYZE” lesson free?

Yes — the full text of “Reading EXPLAIN and EXPLAIN ANALYZE” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading EXPLAIN and EXPLAIN ANALYZE”?

Read the plan tree, understand cost vs actual time, and pick out the heavy nodes. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reading EXPLAIN and EXPLAIN ANALYZE” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading EXPLAIN and EXPLAIN ANALYZE
  2. Sequential Scans vs Index Scans
  3. Hash Join vs Merge Join vs Nested Loop
  4. Identifying and Fixing Slow Queries
← Back to SQL Academy