Skip to main content
Concepts/Algorithms & Data Structures/database-indexing

Database Indexing: B-Trees vs LSM-Trees

Optimize query read latencies using balanced search trees or log-structured merge-tree write buffers.

XLinkedInReddit
advanced

๐Ÿ’ก Why It Matters

Without indexes, searching 10,000,000 rows requires scanning every byte on disk. Indexes keep sorted pointers in memory/disk structures.

๐ŸŒ Real-World Production Usages & Examples

PostgreSQL B-Tree Index

Scenario: Finding user by email out of 5,000,000 users.

Outcome: Query latency drops from 450ms to 0.4ms.

โœ“ When to Use

  • โ€ขHigh-cardinality foreign keys and primary keys.
  • โ€ขRange queries on timestamp or numerical columns.

โœ• When NOT to Use

  • โ€ขLow-cardinality boolean columns (e.g. is_active).
  • โ€ขTables with massive write-only volume where index update overhead slows ingestion.

๐Ÿ”‘ Core Architecture & Rules

  • 1B+ Trees store key-pointer pairs in self-balancing nodes; leaf nodes form a doubly-linked list for fast range scans.
  • 2LSM-Trees buffer writes in a MemTable (RAM) and write sequential SSTables to disk, compacting periodically.

โš–๏ธ Architectural Trade-Off Analysis

Write Amplification

PROB-Trees offer fast point lookup.
CONLSM-Trees provide 10x higher write ingestion speed.

โš ๏ธ Failure Modes & Mitigations

Index Fragmentation

Random updates leave empty page gaps.

Mitigation: Run periodic REINDEX / VACUUM FULL operations.

๐Ÿ› ๏ธ Example Usages

Reviewed practical implementations and concrete code walkthroughs for Database Indexing: B-Trees vs LSM-Trees.

1 Reviewed Example
algorithm walkthroughexecutable tested

LSM-Tree MemTable Flush & Compaction Trace

Step-by-step trace showing RAM write buffering in MemTable flushing sequentially to disk SSTable files.

โฌกExplore Related Concepts & Guides

Curated technical reading and direct concept relationships.

โšก Next Step ยท Apply Concept

Ready to apply Database Indexing: B-Trees vs LSM-Trees?

Test your understanding with a short scenario about real system constraints and trade-offs.

Share this concept

Share with fellow engineers or bookmark for system design discussions.

Share:XLinkedInReddit