The Three Ways to Join
Last timeThe Plan Is Built on a Guess
There are only three ways to match rows in one table against rows in another, each wins in a different situation, and most slow joins are the wrong one of the three.
Joining is where queries go from slow to unusable, because the three available methods differ not by a few per cent but by factors of thousands. Every database has all three, and the whole art is in which one gets used.
One row at a time
The simplest method takes the first row of one side, searches the other side for rows matching it, emits the matches, and moves on. The side being walked is the outer side; the side being searched is the inner.
Its cost is the number of outer rows multiplied by the cost of one search, and that multiplication is the whole story. Twelve outer rows against an indexed inner side costs almost nothing. Two million outer rows is two million index descents, and if the inner side has no usable index, two million full scans, which is the worst thing a database can be made to do.
| step | rows on the outer side | one row at a time, 0.12 ms each | build once and probe, 800 ms then 0.002 ms each | which one wins, and by how much | what happened |
|---|---|---|---|---|---|
| 1 | 12 | 12 times 0.12, so 1.4 ms | 800 plus 0.02, so 800 ms | one at a time, by 560 times | Twelve descents into an index, finished before the other method has built anything. This is the branch the planner is hoping for when it calls a side very small. |
| 2 | 200,000 | 200,000 times 0.12, so 24 seconds | 800 plus 400, so 1.2 seconds | build and probe, by 20 times | Somewhere below this row the two crossed over, and nothing in the query says where. The first column grew two hundred thousand times and the second column's fixed 800 ms did not grow at all. |
| 3 | 2,000,000 | 2,000,000 times 0.12, so 4 minutes | 800 plus 4,000, so 4.8 seconds | build and probe, by 50 times | Two million descents, and that is the kind figure, because it assumes the inner side has an index and that each outer row finds few matches. Without a usable index this row is two million full scans instead. |
Building a lookup structure
The second method reads one side completely and arranges it in memory so that any value can be found immediately. Then it reads the other side once, looks each row up, and emits what matches.
The accounting is completely different. Each side is read exactly once, and the probing side's rows cost almost nothing each, so this is the default for joining two large tables and usually what you want to see in a plan.
The condition is memory. What has to fit is not the table but the rows and columns the query uses from it.
The lesson stops here
4 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