Indexes and query performance · Scenario
My orders API gets slower every week
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Our 'My orders' API used to answer in 80 milliseconds. Over the last month its p99 has crept up to 4 seconds, and today the database CPU is at 90%. Nothing was deployed this week. What do you do?
Then
Follow-ups the interviewer asks next
- What do you ask and look at first?
- Users are complaining now. What can you do in the next 15 minutes?
- How do you find the exact query that is slow?
- You add an index, but p99 is still 1.5 seconds. What next?
- A sale next week brings 5 times the traffic. What must be in place?
- What's the proper long-term fix?
- How do you catch this before users notice next time?
- Who do you update, and how?
- Have you fixed a slow query in production? What was it?
The answer
What a strong answer covers
- Clarifies: which endpoints, all users or heavy users, gradual or sudden. Gradual with no deploy points to data growth. Traces show time in the database, not the app.
- Stops the damage: cancels runaway queries (
pg_cancel_backend), caps page size, caches the first page for heavy users, adds the index with CONCURRENTLY. - Finds it:
pg_stat_statementsby total time, the slow query log (log_min_duration_statement, or MySQL's slow log),pg_stat_activityfor what is running, thenEXPLAIN (ANALYZE, BUFFERS). - Cause: the query filters by
user_idand sorts bycreated_atwith no index on both; fine at 2 million rows, slow at 41 million. - Twist: deep OFFSET pages for a reseller with 180,000 orders, and
SELECT *pulling a large JSONB column the app never shows. - Long-term:
(user_id, created_at DESC), keyset pagination, only the needed columns, archiving old orders. - Prevents it: alerts on p99 per endpoint and on top-query regressions, tests against production-sized data, EXPLAIN in code review for new queries.
- Communicates: incident channel updates with an ETA, then a short postmortem.
The red pen
Common traps
Avoid:"add more CPU" first; restarting the database; indexing every column; CREATE INDEX without CONCURRENTLY on a busy table.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.