HomeInterview QuestionsHow do you optimize a slow-running query?

How do you optimize a slow-running query?

🟡 Medium Conceptual Junior level
1Times asked
May 2026Last seen
May 2026First seen

💡 Model Answer

Optimizing a slow-running query starts with examining the execution plan using EXPLAIN or similar tools. Look for full table scans, missing indexes, or expensive joins. Add appropriate indexes on columns used in WHERE, JOIN, and ORDER BY clauses, ensuring they are selective and not over‑indexed. Consider covering indexes that include all columns needed for the query to avoid lookups. Rewrite the query to reduce complexity: replace subqueries with JOINs, avoid SELECT *, and filter rows as early as possible. Use query hints sparingly to force index usage when the optimizer misbehaves. For large tables, partitioning or sharding can reduce scan scope. Update statistics regularly so the optimizer has accurate cardinality estimates. Finally, test the query after each change, measuring response time and resource usage to confirm improvements. This systematic approach balances index maintenance overhead with performance gains.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500