← Lessons

quiz vs the machine

Gold1440

Databases

Query Execution Plans

Reading the planner's chosen strategy to make queries fast.

5 min read · core · beat Gold to climb

What a Plan Is

When you run a query, the planner decides how to fetch the data: which indexes to use, which join order, and which algorithms. The result is an execution plan, a tree of operations the engine runs.

Reading a Plan

  • EXPLAIN shows the chosen plan; EXPLAIN ANALYZE also runs it and reports real timings.
  • A sequential scan reads the whole table; an index scan jumps to matching rows.
  • Estimated rows versus actual rows reveals when statistics are stale.

Why Plans Vary

The planner uses statistics about data distribution and a cost model to compare options. A small table may favor a full scan, while a selective filter favors an index.

Tuning From Plans

  • Spot a sequential scan on a large table that needs an index.
  • Find a join that processes far more rows than expected.
  • Update statistics when estimates are wildly off.

Key idea

An execution plan is the planner's cost based strategy for a query, and reading it reveals missing indexes and bad estimates.

Check yourself

Answer to earn rating on the learn ladder.

1. EXPLAIN ANALYZE differs from EXPLAIN because it:

2. A large gap between estimated and actual rows usually signals: