← Back to Notes

Database Indexing

2026-01-24article
Originally Published ↗Download PDF ⬇

Database Indexing

Database performance is critical for modern applications, where the difference between a slow sequential scan and a fast index lookup can be substantial. Without indexes, a database must check every record sequentially ("sequential scan"), which becomes prohibitively slow as data grows. Indexes optimize this by providing a structured path to the data, significantly reducing the amount of disk I/O required to locate records.

However, indexes are not free. They introduce trade-offs: while they drastically improve read performance, they consume additional disk space and slow down write operations (inserts, updates, deletes) because the index structures must be maintained in sync with the data. A balanced approach involves creating indexes for frequent query patterns while avoiding unnecessary ones that would degrade write performance without sufficient benefit.

The article highlights B-Trees as the most common and versatile index type. B-Trees are self-balancing tree structures that maintain data in sorted order. They are designed to align with disk hardware, with node sizes typically matching disk page sizes to maximize I/O efficiency. B-Trees excel at both equality searches (finding a specific user) and range queries (finding users by age), making them the default choice for many databases like PostgreSQL and MongoDB. Other index types like LSM Trees, Hash Indexes, and Geospatial indexes serve more specialized use cases.

Key Concepts

  • Sequential Scan vs. Index Scan: Without an index, the database reads every page (Sequential Scan). An index allows the database to "jump" to the relevant pages (Index Scan), minimizing I/O.
  • Index Costs: Indexes improve read speeds but incur costs in Writer Performance (maintenance during updates) and Storage Space (additional files on disk).
  • B-Tree Architecture: A self-balancing tree data structure that keeps data sorted and allows for logarithmic time searches, insertions, and deletions. Nodes are optimized to fit in disk blocks.
  • Disk I/O Optimization: The primary goal of indexing is to reduce the number of disk pages that need to be loaded into memory, as disk access is much slower than memory access.
  • Balanced Workloads: B-Trees are ideal for general-purpose workloads, maintaining performance stability even with random inserts and deletes.