The API is slow, the database shows 100% CPU, and the slow query log is full of one query doing a sequential scan.

BeginnerTroubleshootingLatency
Answer

Likely causes → A missing index on a filter column, a query changed to wrap the indexed column in a function, or a table that grew past the point where a seq scan was cheap.

Diagnose →

SELECT query, calls, mean_exec_time FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = 42;

Fix →

CREATE INDEX CONCURRENTLY idx_orders_customer ON orders (customer_id);

CONCURRENTLY avoids locking writes on a live table.

Prevent → Run EXPLAIN in code review for new queries, track pg_stat_statements top queries on a dashboard, and alert on DB CPU above 80%.

All Real-World Interview questions Roadmap: Interview readiness

More on Latency