ContentsThe library

What the Planner Decides

Four Remedies and the One Cause Each of Them Repairs

Last timeReading the Plan It Gives You

Every fix for a bad plan addresses a particular cause. Matching them wrongly is how teams end up with forced plans that stop working when the data moves.

The previous lesson ends with a line of the plan and a cause. This one matches

each cause to the remedy that actually repairs it, which matters because the

four remedies are not interchangeable and the convenient one is last.

FIG 1The four remedies by how much they take away from the planner
repairs the information the planner hasreduces or overrules the choice01refresh the statistics2declare related columns or ask for a finer summary3rewrite the query to restore the estimate4split the query into pieces5force the method or pin the planhow far the remedy reaches into the decision
Work left to right and stop at the first one that applies. Everything in the left span leaves the planner free to re-decide as the data changes, which is the behaviour you are paying for. Everything in the right span takes some of that away, and the right-hand end takes all of it. The order is not about effort, since forcing a plan is often the quickest thing to type. It is about what survives the next six months.

Refreshing the numbers

The cheapest remedy and the most over-prescribed. A refresh resamples the table

and rewrites the summary: row counts, frequent values, histograms, distinct

counts.

It repairs exactly one cause, which is that the data changed after the summary

was taken. That is a real and common cause, particularly on a table that was

recently loaded, recently grown by an order of magnitude, or had a new value

introduced that the frequent-values list has never seen.

It repairs nothing else, and this is where teams waste time. If the summary was

gathered an hour ago and the estimate on a filtered scan is still out by a

factor of eight hundred, the cause is not staleness, and refreshing again will

produce the same number. Check the gathering time first and only then decide

whether this remedy is even eligible.

Giving it more to work with

The second remedy supplies a number the planner was previously guessing. Which

number depends on what broke, and the previous lesson told you which.

The lesson stops here

5 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01You Asked For Rows, Not For a Method
  2. 02One of Them Is Quadratic and Sometimes It Is the Right Answeropening only
  3. 03It Has Never Looked at Your Dataopening only
  4. 04A Factor of Ten at the Bottom Is a Factor of a Thousand at the Topopening only
  5. 05The City Already Told You the Postcodeopening only
  6. 06Six Tables Have Seven Hundred Twenty Orders and They Are Not Alikeopening only
  7. 07Find the Lowest Line Where the Two Numbers Stop Agreeingopening only
  8. 08Four Remedies and the One Cause Each of Them Repairsyou are here

Read alongside