ContentsThe library

How an Index Actually Works

The Index Is Right There and the Database Will Not Use It

Last timeThe Other Two Shapes

Five reasons cover almost every case, and in two of them the database is right to refuse. Here is how to tell which one you have, in the order worth checking.

This is the lesson people arrive for. The index exists, it names the column in

the filter, and the query is still reading the whole table. Five causes cover

almost all of it.

An expression where the column should be

The first cause is the commonest and the easiest to fix. An index on a column

stores the values of that column, in order. If the filter does not ask about

those values, there is nothing to seek on.

FIG 1Five causes, their signature, and what each one costs to check
fixable by youvisible in the query texfixable without a new inshare of real cases, per
an expression or a type 11142
the filter matches most 10023
the column is not leadin11116
the statistics are stale00112
the fetch costs more tha1107
The two marked cells are why this is hard. Those two causes are invisible in the query text: the query looks identical whether it matches a hundred rows or half the table, and nothing in it reveals what the database believes about the data. Together they are a third of cases, and both are diagnosed by looking at the estimated and actual row counts rather than by reading the query.

The forms this takes are worth listing because each looks innocent. Wrapping

the column in a function, so the filter asks about the uppercase version of a

name while the index holds the name. Arithmetic on the column, so the filter

asks about a price plus tax. A comparison against a different type, where the

database quietly converts the column rather than the constant. And a leading

wildcard in a text match, which asks for keys with something in the middle,

which sortedness cannot help with.

The fix in every case is to move the computation off the column. Compare the

column against a computed constant instead of comparing a computed column

against a constant, make the types match, and where the expression is genuinely

needed, build the index on the expression itself, which most databases support

and which simply sorts by the computed value instead.

The lesson stops here

5 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. 01The Database Never Reads a Row
  2. 02Eighteen Reads Instead of a Quarter of a Millionopening only
  3. 03Three Reads, and Two of Them Were Already in Memoryopening only
  4. 04The Page Is Full, So Cut It in Halfopening only
  5. 05A Phone Book Sorted by Surname Then First Nameopening only
  6. 06The Index Already Knew the Answeropening only
  7. 07One Is Faster and Cannot Do Ranges, the Other Is for Writingopening only
  8. 08The Index Is Right There and the Database Will Not Use Ityou are here

Read alongside