Query Optimization and Execution Plans Questions
Making queries fast: reading and interpreting execution/explain plans, identifying full scans, spotting SQL anti-patterns, and rewriting queries for better performance. Covers how the planner chooses join order and access methods, and how statistics drive those choices. A core skill for anyone responsible for query performance in production.
A GROUP BY (or DISTINCT) query is spilling to disk during the aggregate step and running much slower than its data size would suggest. How would you confirm from the plan that a spill is actually happening, and what are your options for reducing or avoiding it, including the trade-off of raising memory settings versus changing the query or the data model?
A plan shows a merge join. What has to be true for the optimizer to choose a merge join, and when is it actually faster than a hash join or nested loop on large, already-sorted data? What breaks it, and what could you change (indexes, explicit sort) to make it a live option?
A query sorts (or filters) on a computed expression rather than a bare column, and the plan shows a sequential scan plus an explicit sort even though a similar bare-column query would use an index. Propose an index-based fix, and note what limits it (for example, the expression in the query has to match the indexed expression exactly).
What is a covering index, and how does it let the planner satisfy a query using only the index (an index-only scan) instead of also visiting the table? Given a concrete query that selects a small number of columns with a selective WHERE clause, describe the index you would create and why.
Reading a query plan looks different depending on the engine: PostgreSQL's EXPLAIN, BigQuery's job statistics and query plan, Snowflake's Query Profile, and SQL Server's execution plan viewer all surface roughly the same information in different shapes. Compare how you would locate and interpret this information across two or three of these systems, and describe a debugging checklist you would use regardless of which one you are in.
Unlock Full Question Bank
Get access to all Query Optimization and Execution Plans interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.