System Design Academy

Saving…

Lesson 6 of 10 in Module 30 of 54 lessons complete

Indexing

Module 3, lesson 6Concept10 min

Definition

Index

In plain English
An index is an extra sorted structure that lets a database find rows without reading the whole table.
Think of it like
It is like the index at the back of a textbook that points straight to page 214.

Full table scan

In plain English
A full table scan is when the database reads every row to answer a query.
Think of it like
It is like flipping every page of a book to find one word.

Why it exists

Your receptionist searches two million pet records by owner phone. Reading every row makes each lookup slow.

The database needs a shorter path to matching rows. An extra lookup structure can provide that path.

How it works

B-tree

In plain English
A B-tree is the sorted, branching structure most databases use for indexes, so a lookup takes a few steps even across millions of rows.
Think of it like
Like a library directory: floor, then aisle, then shelf.

Composite index

In plain English
A composite index sorts by several columns in a fixed order, such as tournament then score.
Think of it like
Like a phone book sorted by surname, then first name.

A phone-range root branches to two ranges; one range leads to matching entries and then the pet rows.

A simplified B-tree narrows the search before reaching matching rows.

A phone-range root branches to two ranges; one range leads to matching entries and then the pet rows.

The index stores owner-phone values with a way to locate matching rows.
SQL
CREATE INDEX idx_owner_phone ON pets (owner_phone);
SELECT id, name
FROM pets
WHERE owner_phone = '5550100';
WorkWithout this indexWith this index
Selective lookupRead up to 2,000,000 rowsTraverse branches and fetch matches
Illustrative timeAbout 1.5 secondsA few milliseconds
Write costNo extra structure hereMaintain index entries too
  • This lesson describes sorted B-tree indexes; other index types also exist.
  • Composite indexes are usually most efficient when filters include their leading columns.
  • Measure on your data: row count, stored layout, and memory affect speed.

Examples

Search by owner phone

You look up pets by owner phone among two million records.

Use the owner-phone index created above for this selective lookup.
SQL
SELECT id, name FROM pets WHERE owner_phone = '5550100';

A few tree levels can narrow the search, but fetching rows takes more work. Treat timing figures as illustrations, then measure.

The insight

Index the columns your useful queries search by.

Trade-offs

Pros

  • Selective reads can avoid full scans.
  • Suitable indexes can avoid extra sorting.

Cons

  • Uses additional storage.
  • Writes maintain affected index entries.
  • Unused indexes still cost resources.

Use when

  • Large-table queries filter specific rows.
  • Frequent joins and sorting need faster access.

Avoid when

  • Tables are tiny.
  • Rarely read columns add no useful search path.

Quick check

A query filters on owner_phone and takes 2 seconds on a big table. Most likely fix?
Why not index every column?

Takeaways

  • Indexes trade storage and write work for read speed.
  • Index useful search, join, and sort paths.
  • Column order matters in composite indexes.

Try the quick check above whenever you feel ready.

Previous: SQL vs NoSQL: How to Choose