Basis Lab

What the Planner Decides

A Factor of Ten at the Bottom Is a Factor of a Thousand at the Top

Last timeGuessing How Many Rows

Estimation errors do not stay where they are made. Each join multiplies the errors of its inputs, so a small mistake low in a plan becomes a catastrophic one higher up.

The previous lesson made estimates from a summary. This one is about what happens to the error in those estimates as they travel up a plan, and the answer is that it does not stay the same size.

How a join is estimated

A join takes two inputs and produces some rows. To cost the operation above it, the planner must estimate how many.

The standard rule is to multiply the two input sizes and divide by the number of distinct values of the join key on the side that has more of them. It assumes every key on the smaller side finds a match, and that matches are spread evenly.

FIG 1The estimated output of a join, and where each of its three numbers comes from
stepwhat the rule needswhere the number comes fromthe value for the customers to orders join of the plan belowwhat an error in it does to the outputwhat happened
1rows in from the leftan estimate, built by the filters underneath it1,200multiplies the output by whatever factor it is out byNot a measurement. It is the number the previous lesson produced from a summary, and it arrives here carrying whatever error it was born with.
2rows in from the righta second estimate, from its own filters8,300multiplies the output again, so the two factors multiply togetherThe two inputs enter as a product. Low by four on the left and low by three on the right is low by twelve, and that is before anything about the rule itself is questioned.
3distinct keys, the larger of the two countsthe summary of whichever table has more of them1,200, one customer per rowdivides the output, so an error here moves it the other wayThe one term that is read rather than derived, and the only term in the rule with a brake on it. It is also the assumption that every key on the smaller side finds a match.
4rows outnothing measured: those three combined, and that is all1,200 times 8,300 over 1,200, so 8,300the product of the two input errors, divided by the error in the countThe number the operation above this one is costed for. A join method is chosen for it, memory is reserved for it, and no part of the execution will revisit it.
5rows that really came outthe execution, afterwards, and nowhere else68,000a factor of 8.2, none of which was made hereThe join introduced no error of its own. It was handed one and passed it on at full size, which is what the next two figures are about.
5 steps
Three numbers go in and two of them are themselves estimates. They appear as a product, so their errors multiply rather than average, and the divisor is the only term that can pull the other way. Read the last two rows together: the arithmetic was done faithfully and the answer is out by a factor of eight, because nothing in the rule can notice that one of the numbers handed to it was wrong.

The errors multiply

Now stack the joins. Each join takes two estimates, each carrying its own error factor, and produces one estimate carrying the product of them.

The lesson stops here

2 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 Topyou are here
  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 Repairsopening only

Read alongside