What is B-Tree Index?
A B-Tree index is a self-balancing tree data structure that maintains sorted data for efficient insertion, deletion, and range queries in database systems.
A B-Tree index is a data structure used by relational databases to organize and retrieve data quickly while keeping the underlying storage balanced. Named after Rudolf Bayer and Edward McCreight who described it at Boeing in 1970 (often interpreted as 'balanced tree'), a B-Tree generalizes the binary search tree by allowing many children per node. Each node holds a sorted array of keys and pointers to child nodes or actual data rows.
In practice, a database index using a B-Tree stores key values in internal and leaf nodes. The internal nodes guide search decisions by comparing query keys against node keys. Every leaf node resides at the same depth, ensuring worst-case access time remains logarithmic with the number of rows. Unlike a binary tree, each B-Tree node can contain dozens or hundreds of keys, matching the block size of disk or flash storage. This design minimizes the number of reads or writes when inserting deleting or looking up rows, because each node access reads or writes one full block.
B-Tree indexes are the default index type in PostgreSQL MySQL Oracle and SQL Server for general purpose workloads. They excel at equality lookups (WHERE id = 42) and range scans (WHERE date BETWEEN '2024-01-01' AND '2024-01-31'). Data written to disk is always stored in sorted order across the leaf pages, which supports ordered output without sorting overhead. However, B-Trees introduce some write amplification and internal page splits when keys are inserted into full nodes, making them slightly less optimal for pure append workloads compared to LSM trees used by systems like LevelDB or RocksDB.
Key facts
- B-Tree nodes are filled up to a configurable factor, typically leaving space to reduce split frequency.
- Search, insert, and delete operations run in O(log n) time where n is the number of rows.
- All leaf nodes of a B-Tree are at the same depth, guaranteeing balanced tree height.
- The fanout (number of keys per node) is usually hundreds, matching the operating system's page size (4 KB, 8 KB, etc.).
- B-Tree indexes support composite indexes where keys are column tuples sorted lexicographically.
How it works in practice
Related terms
References
More in Databases
ACID
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties guaranteeing reliable database transaction processing, ensuring data integrity despite concurrent access or failures.
BASE
BASE is a design philosophy for distributed databases that prioritizes availability and partition tolerance over immediate consistency, making it a looser alternative to ACID in NoSQL systems.
CAP Theorem
CAP theorem states that a distributed data system cannot simultaneously provide consistency, availability, and partition tolerance; it can only guarantee two of the three.
Connection Pool
A managed cache of database connections that applications reuse rather than opening and closing a connection for each query, reducing latency and server load.
Failover
Failover is the process of automatically or manually promoting a replica database to primary when the active node fails, ensuring continued availability.
Foreign Key
A column or set of columns in a database table whose values must match a primary key in another table, ensuring referential integrity between the two tables.
Hash Index
A data structure that maps keys to storage locations using a hash function, providing constant-time equality lookups but no ordered or range scans.
Materialized View
A database object that stores the precomputed result of a query as a table, refreshed periodically or on demand to improve read performance and reduce computational overhead.
NoSQL
NoSQL is a family of non-relational database systems designed for flexible schemas, horizontal scaling, and high-throughput data access that traditional SQL databases cannot easily provide.
Read Replica
A read replica is an asynchronously updated copy of a primary database instance used to offload and scale read-only query traffic without affecting the source database's write performance.