Skip to content

Add lesson 28: Indexes basics#87

Merged
exekias merged 4 commits into
mainfrom
lesson/28-indexes-basics
Jul 2, 2026
Merged

Add lesson 28: Indexes basics#87
exekias merged 4 commits into
mainfrom
lesson/28-indexes-basics

Conversation

@exekias

@exekias exekias commented Jul 2, 2026

Copy link
Copy Markdown
Member

Adds the first lesson of the new Module 7: Performance & indexing — indexes basics (roadmap item 28-indexes-basics).

What's in the lesson

  • The mental model: a Seq Scan vs a B-tree index (the book-index analogy)
  • CREATE INDEX, with a light before/after EXPLAIN (deep plan-reading is the next lesson)
  • What B-tree covers (=, ranges, BETWEEN, IN, IS NULL, sorting, prefix LIKE)
  • Selectivity: an index helps for selective predicates; the planner may (correctly) keep a Seq Scan for common values — shown both ways on the same column
  • Composite indexes and the left-prefix rule
  • Unique indexes (and that a PK / UNIQUE constraint creates one automatically)
  • The write/space cost of indexes and when not to index

Seed

One events table populated to ~50k rows via generate_series, with a skewed status (low-selectivity demo) and ~5k distinct user_ids (high-selectivity demo), plus ANALYZE. The generate_series approach keeps the seed file tiny.

Checks

One query-returns check (user-id-index-created): confirms the learner created the user_id B-tree index by querying pg_indexes for a definition matching (user_id). It's robust to whatever the learner names the index and ignores the composite (user_id, status). Verified on Postgres 16 (0 before CREATE INDEX, 1 after).

Notes

This PR also bootstraps lessons/07-performance-and-indexing/module.yaml, byte-identical across the Module 7 lesson PRs so the shared folder merges cleanly.

npm run lessons:validate and next build both pass.

Part of #6

🤖 Generated with Claude Code

Part of #6

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
learn-postgres Ready Ready Preview Jul 2, 2026 12:23pm

Grows the events seed from ~50k to ~3,000,000 rows (30k distinct users) so an index's benefit is perceptible in real query time, and adds before/after EXPLAIN ANALYZE beats so learners see Execution Time drop from tens of ms to a fraction of one. Verified on Postgres 16: seq scan ~37ms -> index scan ~0.2ms; seed loads in ~2-3s.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Since the DB is seeded once into a base branch (learners get copy-on-write branches), the table can be much larger. Grows events from 3M to ~25,000,000 rows (250k users). Seq scan now ~370ms vs index ~0.3ms (~1000x) — the win is unmistakable on query time. Prose and seed comment updated; comment flags that CREATE INDEX writes per-learner so index size scales with the knob. Verified on Postgres 16.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
query-returns check confirms the learner created the user_id B-tree index by querying pg_indexes for a definition matching (user_id). Robust to index naming and ignores the composite (user_id, status). Verified on Postgres 16: 0 before, 1 after CREATE INDEX.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@exekias exekias merged commit 6478f20 into main Jul 2, 2026
3 checks passed
@exekias exekias deleted the lesson/28-indexes-basics branch July 2, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant