Lesson 16 · Query performance
Scans & joins
The building blocks a plan is made of — and when the planner picks each.
Your win: name the scan types and join strategies you'll see in
EXPLAIN, and explain what makes the planner choose one over another.
Scan strategies — how rows are read
| Scan | What it does | Chosen when |
|---|---|---|
| Seq Scan | read every page of the heap | small table, or the query wants most rows |
| Index Scan | walk the index, heap-fetch each match | few, selective rows |
| Index-Only Scan | answer from the index, no heap fetch | covering index (Lesson 12) + all-visible pages |
| Bitmap Index+Heap Scan | collect matching page ids into a bitmap, read pages in physical order | medium selectivity; can combine several indexes |
The bitmap scan is the clever middle ground: too many rows for a per-row Index Scan, too few for a full Seq Scan — so it gathers the page list first, then reads sequentially.1
Join strategies — how two inputs combine
| Join | How | Chosen when |
|---|---|---|
| Nested Loop | for each outer row, look up matches in the inner | one side small, or the inner side indexed (cheap to start) |
| Hash Join | build a hash table of one side, probe with the other | both sides large, equality join |
| Merge Join | walk two sorted inputs in lockstep | both inputs already sorted (e.g. by index) |
EXPLAIN (Lesson 15) is the first thing to check.
WHERE resource_path = $1 AND status = $2 — becomes an
Index Scan (or Bitmap) on the resource_path index. A join
emails ⋈ email_recipients on the indexed email_id is a Nested
Loop when few emails match, a Hash Join over a large batch. You'll see exactly which in
the plan when you /query-db → EXPLAIN ANALYZE a real query.
PostgreSQL — Using EXPLAIN + pgMustard glossary
The docs show each node type in real plans; pgMustard's glossary explains when the planner prefers each scan and join.
→ postgresql.org/docs — Using EXPLAIN
→ pgMustard — EXPLAIN glossary
Check yourself (from memory)
Q1. A Bitmap scan is chosen for…
Q2. A Hash Join is typically used when…
Q3. A Nested Loop is cheap when…
1. PostgreSQL — Using EXPLAIN; pgMustard — EXPLAIN glossary.