ContentsThe library

What the Planner Decides

You Asked For Rows, Not For a Method

A query names the answer and says nothing about how to get it. Everything left unsaid is decided by a program you did not write, in a few milliseconds, on every request.

A query describes the rows you want. It contains no instruction about how to

find them, which is the whole point of the language and is also why a query can

be a hundred times slower than it needs to be without looking any different.

What the query actually says

Take an ordinary request: the orders placed last month by customers in one

country, with the customer name, sorted by date.

That names a set of rows. It says which tables the rows come from, which

conditions they satisfy, how the tables relate, what to return and in what

order. Those are all properties of the answer.

FIG 1What is fixed by the query text and what is not
fixed by the querydecided by the databaseroughly how many optionsaffects the answer
which rows are in the an1001
the order of the output1001
how each table is read0140
how each pair is combine0130
the order the tables are0160
where the sorting happen0130
The top two rows are the query. Everything below is open, and the last column is the reason this is safe: none of the open decisions can change the answer, only the time taken to produce it. The two marked cells are where almost all of the variation in that time comes from, which is why the rest of this course spends most of its pages on them.

The useful habit is to read a query and ask what it did not say. It did not say

to use an index. It did not say which table to start from. It did not say to

sort at the end rather than relying on an index that was already in order. Each

of those is a decision with a cost, and the query is silent on all of them.

FIG 2What happens between the query arriving and rows coming back
Eight steps, of which only two involve judgement. The parse and the rewrite are deterministic. The execution does what it is told. Everything that can go wrong happens in the generating and the costing, and of those two, costing is where the trouble nearly always is.

The decisions left open

The open decisions fall into four kinds. For each table, how to get the rows:

scan it, or seek into one of its indexes, and if several indexes apply, which.

For each pair of inputs, how to combine them, which is a choice between three

methods with very different cost shapes. For the set of tables, the order to

combine them in. And for sorting and grouping, where in the plan to do it,

since an input that is already sorted makes it free.

None of these changes the answer. All of them change the time.

FIG 3Roughly how many plans exist for a query over several tables
number of distinct plans
number of tables in the query
join methods available, three in most systems
access paths per table, a scan plus the usable indexes
Three tables with two access paths each gives 6 times 9 times 8, which is 432 plans, and that is a small query. Six tables gives 720 times 243 times 64, which is over eleven million. The factorial is the dominant term and it is the reason planners search rather than enumerate: the space outgrows the few milliseconds available long before the query stops looking ordinary.

How many plans there are

Two consequences follow immediately and both shape everything in this course.

The planner cannot look at every plan, so it uses a search that prunes, which

means the best plan is sometimes not considered at all. And the planner must

compare plans without running them, which means comparing estimates, which

means the quality of the estimates is the quality of the plans.

What is actually at stake

It would be reasonable to assume the spread between a good plan and a bad one

is tens of per cent. It is not.

FIG 4One query, two plans, same answer
stepstepplan chosen when the estimate was rightplan chosen when the estimate was wrongwhat happened
1start fromcustomers in one country, 1200 rows by iall orders of last month, 4.1 million roThe first decision sets everything after it. Starting from the small filtered side keeps every later step small.
2then combine withtheir orders, by index seek, 9400 rowsthe customer table, by hash, 4.1 millionThe second plan is now carrying four million rows into a join that will discard almost all of them.
3then filter tolast month, 820 rowsone country, 9400 rowsBoth arrive at the same place. One of them read four hundred times more to get there.
4rows read in total114008300000Same query text, same answer, same database. The difference is one decision made from one wrong number.
4 steps
The ratio here is over seven hundred, and nothing about the second plan is stupid: combining two large sets with a hash is a sensible method, and scanning a table you expect to need most of is sensible too. The plan is a correct response to a belief about the data that happened to be false, which is the single most important thing to understand about planners.

That is the shape of the subject. A planner is a small compiler with a few

milliseconds, choosing among millions of possibilities, using numbers it cannot

verify. It usually gets it right. When it does not, the result is not a little

slower, and the fix depends entirely on which part of that sentence broke.

The next lesson takes the most important of the open decisions, how two tables

are combined, and derives the cost of each method from the sizes of the inputs.

Recap

  • A query is a description of the answer, so every question of method is left open, and the database has to settle all of them before a single row is read.
  • The open decisions are the access path for each table, the join method for each pair, the order the joins happen in, and where sorting and grouping are done.
  • The number of possible plans grows faster than anything else in the system, and the spread between the best and worst of them is routinely a factor of several hundred.

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

NextThree Ways to Join →

The rest of this course

  1. 01You Asked For Rows, Not For a Methodyou are here
  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 Repairsopening only

Read alongside