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.
| step | what the rule needs | where the number comes from | the value for the customers to orders join of the plan below | what an error in it does to the output | what happened |
|---|---|---|---|---|---|
| 1 | rows in from the left | an estimate, built by the filters underneath it | 1,200 | multiplies the output by whatever factor it is out by | Not a measurement. It is the number the previous lesson produced from a summary, and it arrives here carrying whatever error it was born with. |
| 2 | rows in from the right | a second estimate, from its own filters | 8,300 | multiplies the output again, so the two factors multiply together | The 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. |
| 3 | distinct keys, the larger of the two counts | the summary of whichever table has more of them | 1,200, one customer per row | divides the output, so an error here moves it the other way | The 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. |
| 4 | rows out | nothing measured: those three combined, and that is all | 1,200 times 8,300 over 1,200, so 8,300 | the product of the two input errors, divided by the error in the count | The 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. |
| 5 | rows that really came out | the execution, afterwards, and nowhere else | 68,000 | a factor of 8.2, none of which was made here | The 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. |
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 contentsThis 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