Basis Lab

Why a Query Is Slow

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.

FIG 1How the method gets chosen
The branches are decided before any data is read. Nothing in this diagram measures anything, which is why the quality of the estimates from the previous topic determines which of three wildly different performances you get.

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.

FIG 2Twelve outer rows and two million, priced both ways
steprows on the outer sideone row at a time, 0.12 ms eachbuild once and probe, 800 ms then 0.002 ms eachwhich one wins, and by how muchwhat happened
11212 times 0.12, so 1.4 ms800 plus 0.02, so 800 msone at a time, by 560 timesTwelve 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.
2200,000200,000 times 0.12, so 24 seconds800 plus 400, so 1.2 secondsbuild and probe, by 20 timesSomewhere 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.
32,000,0002,000,000 times 0.12, so 4 minutes800 plus 4,000, so 4.8 secondsbuild and probe, by 50 timesTwo 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.
3 steps
Read the two middle columns as shapes rather than as times. The first is the outer row count times a constant and there is nothing in it that flattens, so it either finishes before the alternative starts or it never finishes at all. The second pays 800 ms once and then almost nothing a row. So the gap between them is 560 times in one direction at twelve rows and 50 times in the other at two million, and the only thing deciding which way round it falls is an estimate of the first column.

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 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. 01What a Query Is Actually Spending
  2. 02What an Index Actually Isopening only
  3. 03The Conditions No Index Can Rescueopening only
  4. 04Every Plan Rests on an Estimateopening only
  5. 05The Three Ways to Joinyou are here
  6. 06How to Read a Planopening only
  7. 07The Bill for Every Indexopening only
  8. 08A Method Instead of a Guessopening only

Read alongside