B-TreeLSM TreeSSTableComposite IndexCovering IndexPartial IndexQuery Optimization

Indexing

Master database indexing — B-tree indexes, LSM trees & SSTables, composite indexes, partial indexes, and covering indexes. Understand the trade-offs that make queries fast.

28 min read11 sections
01

The Big Picture — What Is an Index?

An index is a data structure that helps a database find rows without scanning every single row in the table. Without an index, finding one user in a table of 100 million rows means reading all 100 million rows. With an index, it means reading ~27 rows (log₂ of 100M). That's the difference between a 10-second query and a 0.1ms query.

📖

The Book Index Analogy

A 500-page textbook without an index: to find 'B-tree', you flip through every page. With an index at the back: you look up 'B-tree → page 247' and jump directly there. The index is a small, sorted lookup structure that trades a bit of extra space (the index pages) for massive speed improvement. A database index works identically — it's a sorted structure that maps column values to the physical location of rows on disk.

🔥 Key Insight

Indexes make reads fast and writes slow. Every INSERT, UPDATE, or DELETE must also update every index on the table. A table with 10 indexes means every write does 11 operations (1 table + 10 indexes). The art of indexing is adding just enough indexes to make your critical queries fast without crippling write performance.

1 / 11