Free to LearnDatabase studio

Database query optimizer

Ask a database a question (a query), and its optimizer picks the cheapest plan to get the answer.

Three Ways In · Layer 3 of 3 · Node.js 22+ · Zero Dependencies

Build With Your Coding Agent — 5 Rungs + Planted Traps

Build a cost-based SQL query optimizer with your coding agent across five rungs: null-safe outer-join predicate pushdown, MCV + equi-depth skew histograms, correlated-column sampling, memory-bounded I/O + spill hash costing, and interesting-order Selinger dynamic programming. Each rung gives you an architect prompt for Claude Code, Codex, or Cursor, a planted bug that unguided agents write, and a deterministic test suite that catches it.

  1. Outer-join pushdown: Predicate pushdown preserving NULL-extended rows on LEFT JOINs — Planted trap: Pushing a null-rejecting WHERE/ON filter below a LEFT JOIN and silently dropping unmatched customers.
  2. Skew histograms: Most-Common-Values (MCV) list paired with equi-depth buckets — Planted trap: Averaging a viral heavy-hitter key across a uniform bucket and underestimating join rows 100x.
  3. Correlated predicates: Joint sample selectivity for linked attributes (make = 'Honda' AND country = 'Japan') — Planted trap: Multiplying marginal probabilities P(A) * P(B) as if strongly linked columns were independent.
  4. Memory-bounded hash cost: Sequential vs. random page I/O plus multi-pass Grace hash spill pricing — Planted trap: Pricing an oversized hash build table as in-memory when it exceeds work_mem and spills to disk.
  5. Interesting-order DP: Bottom-up Selinger search retaining cheapest unordered AND ordered plans per subset — Planted trap: Pruning a slightly pricier index plan per subset that would have saved a final ORDER BY sort.
unzip optimizer-project.zip && cd query-optimizer-lab/agent-track
node --test test/*.test.mjs
mkdir -p src/work
OPTIMIZER_WORK=work node --test test/rung-01.test.mjs

Download 5-Rung Agent Track (optimizer-project.zip) Read AGENT-TRACK.md on GitHub Open agent-track/README.md on GitHub