Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 01 · Database Internals · One Database, to the Bolt · about 23 min to read

Play it as a journeyTutor comes with a plan

The Query Planner

The Query Planner
The Query Planner · Open board

The Database Internals board behind most “why is this query slow” questions.

Before you start. The last lesson showed that Postgres, the open-source database this series opens up, never deletes a row on update: it writes a fresh copy and leaves the old one alone for anyone still reading it, until vacuum clears it away later. This lesson is about a different machine entirely: the one that decides, for any query you type, which of the table’s rows to even look at, and in what order.

Lesson 10 of 26 in Database Internals

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • 1992: the optimizer that stopped following a script
  • What SQL never tells the database
  • The gambler who never sees the cards
  • The one question asked over and over: selectivity
  • Where the bet goes wrong: assuming two things are unrelated
  • Catching it: reading the bet slip
  • What’s actually on the tip sheet
  • The independence bug, precisely
  • Reading a table: three strategies, and when each wins
  • Joining two tables: three strategies
  • Reading EXPLAIN (ANALYZE, BUFFERS) in full
  • The one thing

Terms it explains: index, cost, planner, selectivity.

Lab

Your query has an index and Postgres refuses to use it. At what fraction of the table does reading all of it in order start to win?

Drag What fraction of the table the query actually matches upward one notch at a time, and watch The plan it picks.

Open The selectivity crossover

Your sort spills to disk. Give it more memory and it stops. Why does nobody set work_mem to a gigabyte?

Click The default, then A big desk and The whole room, drag Sessions running it at once, and watch The whole box.

Open The work_mem dial

Related papers

Review Database Internals