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 · with code: Go · about 15 min to read

Play it as a journeyTutor comes with a plan

Build sort and aggregate with a memory limit

The Query Planner
The Query Planner · Open board

The plan as a tree of steps, with the steps that must see every row before they can answer. Sort and aggregate are two of those steps. This lesson builds them with a limit on how much memory each may use.

Before you start. The plan, running watched Postgres sort and group with work_mem as the limit, and counted temp-file blocks as the sign of a spill. Sorting & Searching has merge sort in memory. This lesson builds the version that goes to disk, so you can see why the numbers in 0579 come out as they do. The code is in teaching/code/database-internals/02-sortagg. It reads Go.

Lesson 20 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

  • 1985: fifteen minutes to sort a hundred megabytes
  • A sort that does not fit
  • A top-N sort never spills
  • Grouping when the groups do not fit

Terms it explains: run, fan-in, merge pass, one more row arrives.

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