A query is slow in production. How do you diagnose and fix it?
What they are really testing: Whether you reach for EXPLAIN and the query plan rather than guessing, and know the usual culprits (missing index, full scan, SELECT *, N+1). Daily database work.
A real interview question
A query is slow in production. How do you diagnose and fix it?
What most people say
drag me
“I would add an index to make the query faster.”
Adding an index without first reading the query plan is guessing, the problem might be a full scan, an N+1, SELECT *, or a query that cannot use an index. The method is EXPLAIN first, then the targeted fix.
The follow-ups they ask next
What does EXPLAIN tell you that guessing cannot?
The actual execution plan: whether it scans or uses an index, the join strategy and order, estimated vs actual rows, and where time goes. It points you at the real bottleneck instead of a guessed fix.
Why can SELECT * make a query slower?
It fetches columns you do not need (more I/O and network) and can prevent a covering/index-only scan, forcing the DB back to the table. Select only the columns required.
What the interviewer is listening for
- Measures first (slow query log/APM)
- Reads the plan with EXPLAIN/EXPLAIN ANALYZE
- Knows the usual fixes incl. N+1, verifies after
What sinks the answer
- Adds an index without EXPLAIN
- Guesses the slow query
- No verification step
If you genuinely do not know
Say this instead of freezing. Reasoning out loud from what you do know beats silence every single time, and a good interviewer is listening for exactly that.
“Diagnose first: [find the real slow query from the slow log/APM], [run EXPLAIN/EXPLAIN ANALYZE to read the plan, full scan, bad join, huge sort], then [fix the cause: add/correct an index, avoid SELECT *, rewrite, or fix an N+1]. [Re-run EXPLAIN to verify and watch the write-cost trade-off]. Not [reflexively add an index].”
Keep going with databases
Foundation
What is a database index, how does it speed up queries, and what does it cost?
Foundation
What are the ACID properties of a database transaction?
Foundation
What is normalization, and what are primary and foreign keys?
Junior
Why is database connection pooling important, especially at scale?
Junior
How do read replicas help scale a database, and what is replication lag?
Mid
Under load, your RDS instance is the bottleneck and the app is slowing down. Walk me through your options, in order.
Knowing the answer is not the same as recalling it under pressure
Sign in to send the questions you fumble to spaced recall, so they come back right before you would forget them, and learn the concepts behind them with hands-on labs.
Start free