Query Optimization Discussion
5 exercises — practise the English vocabulary for query performance discussions: EXPLAIN scan types, N+1 anti-pattern, query plan cost units, buffer hit rates, and index-only scan terminology.
0 / 5 completed
1 / 5
A senior DBA says in a performance review meeting: "The query is doing a Seq Scan on a 4-million-row table — that's why it's slow." What does Seq Scan mean, and why is an Index Scan preferred for selective queries?
EXPLAIN ANALYZE node type vocabulary:
Option A is the correct definition. A Sequential Scan is not inherently bad — for queries that return a large percentage of rows, it can be faster than an Index Scan because it reads pages sequentially (which is cache-friendly). The problem arises when a Seq Scan occurs on a large table with a highly selective WHERE clause.
How to communicate scan types in reviews: "The planner chose a Seq Scan here because there is no index on the status column and the filter is not selective enough. Adding a partial index on WHERE status = 'pending' would allow an Index Scan and reduce cost significantly."
Option A is the correct definition. A Sequential Scan is not inherently bad — for queries that return a large percentage of rows, it can be faster than an Index Scan because it reads pages sequentially (which is cache-friendly). The problem arises when a Seq Scan occurs on a large table with a highly selective WHERE clause.
| Scan type | How it works | Best when |
|---|---|---|
| Seq Scan | Reads every page of the table in order | Query returns >10–20% of rows; no suitable index |
| Index Scan | Traverses the index, then fetches heap pages | Query is selective (<5–10% of rows); index exists |
| Index Only Scan | Reads only the index; never touches the heap | All projected columns are in the index (covering index) |
| Bitmap Index Scan | Builds a bitmap of matching heap pages, then reads them | Moderate selectivity; reduces random I/O |
How to communicate scan types in reviews: "The planner chose a Seq Scan here because there is no index on the status column and the filter is not selective enough. Adding a partial index on WHERE status = 'pending' would allow an Index Scan and reduce cost significantly."