Skip to main content
Module 10 · Lesson 1 of 437/57 in the course~16 min
Module lessons (1/4)

Indexes and B-Trees

Lesson preview5 hands-on exercises readySQL Course · Module 10 — Optimization and Indexes

Speed up selective lookups and joins dramatically with balanced B-Tree indexes.

What you'll learn in this lesson

  • Understand the performance difference between Sequential Scan (O(N)) and B-Tree Index Scan (O(log N)).
  • Create simple indexes (CREATE INDEX) and unique indexes (CREATE UNIQUE INDEX), recognizing the implicit indexes behind PRIMARY KEY and UNIQUE.
  • Evaluate the trade-off between read speed (SELECT) and write cost (INSERT/UPDATE/DELETE) + disk space.

Overview

Without an index, searching for WHERE city = 'Milano' forces PostgreSQL to perform a Sequential Scan: physically reading the entire table row by row from start to finish. Over a few hundred lines you don't notice; on tens of millions of lines, it causes I/O and CPU saturation. Indexes (B-Tree) turn that linear search into an instant logarithmic…

Select 1K, 100K and 10M rows and see how the number of pages read varies in the simulator below! Also try selecting %laptop% or LOWER(email) to find out when a B-Tree suddenly stops working and falls into Sequential Scan:

Sections covered in this lesson

  1. 11. Mental Model: Sequential Scan vs B-Tree
  2. 22. Progressive & Annotated Examples
  3. 33. Interactive Laboratory: B-Tree vs Sequential Scan on Increasing Scale
  4. 44. Diagnostic Verification (Quiz)
  5. 55. Progressive Practical Laboratory
Pro Lesson · Module 10 — Optimization and Indexes

Unlock "Indexes and B-Trees" and the full SQL Course curriculum

Speed up selective lookups and joins dramatically with balanced B-Tree indexes.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in SQL Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.