Software Engineer
hardoptimize-slow-database-query
How do you optimize a slow database query?
Answer
A reliable approach is:
1) Use **EXPLAIN/EXPLAIN ANALYZE** to see the plan.
2) Add or fix **indexes** on WHERE/JOIN/ORDER BY columns.
3) Avoid `SELECT *` and reduce returned rows.
4) Rewrite expensive joins/subqueries; watch N+1 ORM issues.
5) Use pagination and avoid large offsets.
6) Cache hot reads (Redis) when appropriate.
7) Check database config and statistics.
**Interview tip:** Explain trade-offs: more indexes speed reads but slow writes and increase storage.
Related Topics
DatabasesPerformanceSQL