As asked
A Postgres query on a 200 million row orders table takes 40 seconds. How do you read the query plan and decide what index to add?
Sample answer outline
Use EXPLAIN ANALYZE to compare estimated rows with actual rows, identify sequential scans, expensive sorts, nested loops over large inputs, and whether time is spent reading buffers or computing. Design the index around the predicate, join, and ordering pattern, not around every column in the table. For common queries, a composite or partial index may beat separate single-column indexes, but it must match selectivity and column order. Check write overhead, bloat, and whether the index supports index-only scans through included columns. Candidates often add indexes blindly without validating the plan before and after.
Expect these follow-ups
- When would a sequential scan be the right plan?
- How do stale statistics affect the optimiser?
- What is the tradeoff between a composite index and two single-column indexes?