Make a slow query fast without changing its answer
A dashboard query takes 40 seconds. Read its plan, work out why, and make it fast. The only hard constraint is that the result set must be byte-identical to the original, including for rows involving NULLs. A fix that is fast and slightly wrong is worth less than no fix at all, which is why equivalence carries as much weight here as speed.
What you have to hand in
- The plan is included, before and after.
- The cause is named and supported by the plan, not guessed.
- Timings are shown for both versions on comparable data.
- Equivalence is demonstrated across the full result set, not spot-checked.
- The change is the smallest one that fixes the cause, and any heavier alternative you rejected is named.
The checklist, published before you start
- The cause is correctly identified
The submission names why the query was slow, with evidence from the plan.
- Guesses at a cause with no evidence.
- Names a plausible cause but does not support it from the plan.
- Names the real cause and cites the plan.
- Names the cause, cites the plan, and rules out a competing explanation.
- The query is measurably faster
Before and after timings on comparable data.
- No measurement, or no improvement.
- Improved, but measured only once or on unrepresentative data.
- Clear improvement, measured on representative data.
- Clear improvement with the measurement method described well enough to repeat.
- The result is unchanged
The faster query returns exactly the same rows as the original.
- The result changed and this was not noticed.
- Result is the same on a spot check only.
- Equivalence demonstrated on the full result set.
- Equivalence demonstrated, including for NULL and edge-case rows.
- The fix is proportionate
The change is the smallest one that solves the problem, without unnecessary new indexes or denormalisation.
- Adds indexes or tables with no justification.
- The fix works but does more than needed.
- A minimal, targeted change.
- Minimal, with the rejected heavier alternatives named and dismissed.
The skills a pass would prove
- Query performance basicsRead an EXPLAIN plan, name why a query is slow, and rewrite it to be faster
- Join grain and fan-outJoin three tables at the correct grain and prove the totals were not inflated by fan-out
- Common table expressionsDecompose a complex query into named CTEs that each answer one clear question
- Validating a resultCheck a query result for correctness before trusting it, and say what you checked
This brief is part of the SQL & Data Analysis course
Starting it starts the course: every skill above, in the order they depend on each other, with this brief at the end as the thing you hand in — marked against the checklist you have just read and nothing else.
Start the SQL & Data Analysis course