Pick the fix
SELECT * FROM orders WHERE customer_id = 42 scans the whole 1M-row table every time. What do you add?
Hint 1 of 1
Which column does the WHERE clause filter on?
Choose an answer, then open Show answer below to check it.
Plan shape checked with EXPLAIN on PostgreSQL 17; page counts are a toy table
Text version
No usable index (Seq Scan):
- Root: not used
- Inner: not used
- Leaf: not used
- Table: H1, H2, H3, H4, H5, H6, H7, H8
Primary key index (Index Scan):
- Root: 301–600
- Inner: 401–500
- Leaf: 434–466
- Table: H4
| Scenario | Plan | Index pages | Table pages |
|---|---|---|---|
| No usable index | Seq Scan | 0 | 8 |
| Primary key index | Index Scan | 3 | 1 |
Show answer
Answer: A CREATE INDEX CONCURRENTLY ON orders (customer_id);
Correct. An index on the filtered column; CONCURRENTLY avoids blocking writes while it builds.
Without an index on customer_id, PostgreSQL has to read every row to find matches. A B-tree on that column lets it seek straight to them, and CONCURRENTLY builds it without blocking writes.
Why not the others?
- B
CREATE INDEX ON orders (id, customer_id);. The leading column is id, so a filter on customer_id alone can't seek into it. - C
VACUUM FULL orders;. Compacts the table but doesn't change how rows are found. - D
SET enable_seqscan = off;. A planner debugging switch, not a fix; with no usable index it still scans.