🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Database Indexing

Data SystemsStorage & RetrievalđŸŸĸ Free Lesson

Advertisement

Data Systems

Database Indexing

Indexes enable efficient data retrieval without scanning entire tables. Choosing the right index structure and strategy is critical for query performance at scale.

  • B-Tree — Balanced tree for reads and range queries
  • LSM Tree — Write-optimized structure for high throughput
  • Composite Index — Multi-column indexes for complex queries

Indexing is the single most impactful optimization for database performance.

Why Index?

Without an index, every query scans the entire table. Indexes create auxiliary data structures that map search keys to row locations.

B-Tree Index

The most common index structure in relational databases.

B-Tree Index Structure[10 | 20 | 30][3 | 5 | 7][12 | 15 | 18][22 | 25 | 28][32 | 35]Leaf: 1-9→ Row pointersLeaf: 10-19→ Row pointersLeaf: 20-29→ Row pointersLeaf: 30-39→ Row pointersLeaf nodes linked for efficient range scans

B-Tree Properties

PropertyValue
HeightO(log n) — typically 3-4 for billions of rows
LookupO(log n) — follow path from root to leaf
Range scanO(k + log n) — traverse linked leaves
Insert/DeleteO(log n) — with rebalancing
Block sizeTypically 4KB-16KB (aligns with disk pages)

LSM Tree

Write-optimized index for high-throughput systems.

LSM Tree StructureMemtableIn-memory sortedflushSSTables (Disk)L0: NewestL1: Sorted runsL2: MergedL3: Largestcompaction

B-Tree vs LSM Tree

AspectB-TreeLSM Tree
Write patternRandom I/OSequential I/O
Read patternO(log n) single lookupMay check multiple levels
Write throughputModerateHigh (10-100x)
Read throughputHighModerate
Space efficiencyFragmentation over timeBetter (compaction)
Write amplificationLowHigh (compaction)
Use caseOLTP, mixed workloadsWrite-heavy, time-series

Index Types

Primary Index

Secondary Index

Composite Index

Covering Index

Index Selection

Practice Exercises

  1. Analysis: A table has 100M rows with a B-tree index on column status. There are only 3 distinct values (active, inactive, pending). Should you use this index for a query filtering status = 'active'?

  2. Design: Design composite indexes for these queries:

    • SELECT * FROM orders WHERE user_id = ? AND status = 'shipped' ORDER BY created_at DESC
    • SELECT * FROM orders WHERE created_at > ? AND total > 100
  3. Comparison: Compare B-tree and LSM tree performance for a time-series database receiving 100K writes/second with occasional range queries.

  4. Optimization: A query takes 5 seconds with a full table scan. The table has 50M rows. After adding an index, it takes 50ms. Explain the improvement and identify what else could be done.


What to Learn Next

-> Databases SQL vs NoSQL, indexing, replication, and sharding.

-> Data Partitioning Horizontal partitioning, range vs hash partitioning.

-> Data Replication Leader-follower, multi-leader, and conflict resolution.

-> Scalability Fundamentals Vertical vs horizontal scaling and capacity planning.

-> Caching Redis, Memcached, cache strategies, and invalidation.

-> Event-Driven Architecture Event sourcing, CQRS, and saga patterns.

Need Expert System Design Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement