Skip to content

Indexing Basics (System Design)

An index is a data structure that speeds up data retrieval. Think of it as the index at the back of a textbook — instead of flipping through every page, you look up the topic and jump directly to the right page.


Without an index, finding a user by email scans every row (full table scan → O(N)).

SELECT * FROM users WHERE email = 'alice@example.com';
-- Without index: Scan all 10M rows (SLOW)
-- With index: Jump directly to the row (FAST)

With a B-Tree index, the database stores a sorted tree of (email, row_location) pairs. Lookup takes O(log N) — about 4-5 steps for 10M rows.


flowchart TB
Root["Root Node<br/>'a' → 'm'"] --> Left["'a' → 'g'"]
Root --> Right["'h' → 'm'"]
Left --> Leaf1["alice → row 101<br/>bob → row 203<br/>charlie → row 45"]
Left --> Leaf2["david → row 78<br/>eve → row 312<br/>frank → row 156"]
Right --> Leaf3["grace → row 89<br/>hannah → row 234"]
style Root fill:#7c3aed,color:#fff
style Left fill:#4f46e5,color:#fff
style Right fill:#4f46e5,color:#fff
style Leaf1 fill:#6366f1,color:#fff
style Leaf2 fill:#6366f1,color:#fff
style Leaf3 fill:#6366f1,color:#fff

TypeDescriptionUse Case
Primary IndexOn the primary key, stored with dataClustered — fastest access
Secondary IndexOn non-primary columnsEmail, username lookups
Composite IndexOn multiple columns(city, last_login) queries
Unique IndexEnforces uniquenessEmail, username
Full-Text IndexFor text searchSearch within content

When designing systems, think about which queries need indexes:

Query PatternIndex On
Get user by emailemail (unique index)
Get recent orders for user(user_id, created_at) (composite)
Search by locationlocation (geospatial index)
Count by statusstatus (but careful — low cardinality)

  • Indexes speed up reads but slow down writes (the index must be updated on every insert/update).
  • Each index consumes storage (disk space).
  • Too many indexes = slow writes, large storage. Too few = slow reads.
  • Rule of thumb: Index columns used in WHERE, JOIN, and ORDER BY. Don’t index everything.

  • Index = lookup table that helps the database find data without scanning everything.
  • B-Tree is the most common index structure — O(log N) search time.
  • Indexes make reads faster and writes slower. Put them on columns you search by.