A query became slow in production. How do you investigate?
Answer
- Confirm and measure: which query, how slow, since when (slow query log,
pg_stat_statements, APM traces). - Reproduce with
EXPLAIN ANALYZE; look for sequential scans, wrong row estimates, expensive sorts, nested loops over big sets. - Check recent changes: deploys, data growth, new filters, outdated statistics (
ANALYZE). - Fixes: add or adjust indexes (composite, covering, partial), rewrite the query, avoid N+1, paginate, cache, denormalize or add a read replica.
- Check locks and connection pool saturation.
- Verify the fix with the same measurement and add monitoring.
Practice this question in Repaso