Module lessons (1/4)
Indexes and B-Trees
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 behindPRIMARY KEYandUNIQUE. - 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
- 11. Mental Model: Sequential Scan vs B-Tree
- 22. Progressive & Annotated Examples
- 33. Interactive Laboratory: B-Tree vs Sequential Scan on Increasing Scale
- 44. Diagnostic Verification (Quiz)
- 55. Progressive Practical Laboratory
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.