What Is a B+ Tree Structure in SQL Server?
Understand how SQL Server organizes clustered and non-clustered indexes using root, intermediate and leaf pages, how an index seek navigates the tree, and why this structure makes searching and range queries efficient.
When we create an index in SQL Server, SQL Server needs an efficient way to organize the index information so it can find the required rows without scanning the entire table every time. It uses a balanced tree structure commonly described as a B+ Tree.
A B+ Tree is a tree-based data structure in which index pages are arranged in levels. SQL Server starts at the root page, follows page pointers through intermediate levels, and reaches the leaf level where the required index entry is located.
1. What does a B+ Tree look like?
A B+ Tree has up to three types of levels:
- Root level: The top-level starting point of the index.
- Intermediate level: The navigation levels between the root and leaf.
- Leaf level: The bottom level containing the index's leaf entries.
A small index may have only a root page and a leaf level. Larger indexes may have one or more intermediate levels. Therefore, not every index has exactly three levels.
2. What is the responsibility of each level?
Root page – where the search begins
The root page is the entry point for navigating the index. It contains index key values and child-page pointers. SQL Server compares the search key with the values on this page to decide which lower-level page can contain the requested key.
Intermediate pages – navigation towards the data
Intermediate pages perform the same navigation role at lower levels. They contain key values and pointers to child pages. SQL Server follows the appropriate pointer until it reaches the leaf level.
Leaf pages – where the index entries are stored
The leaf level is where the meaning of the index type becomes especially important:
- Clustered index: The leaf level contains the table's data rows. The clustered index is the table's row storage structure at the leaf level.
- Non-clustered index: The leaf level contains the non-clustered index key and included-column values, together with a row locator that identifies the corresponding row in the base table or clustered index.
3. Clustered index vs. non-clustered index leaf level
Both index types use a B+ Tree navigation structure, but their leaf pages hold different information.
| Point | Clustered index | Non-clustered index |
|---|---|---|
| Leaf level | Contains the actual table data rows | Contains index entries and row locators |
| Row locator | Not needed to locate the row at its own leaf level | Identifies the corresponding base-table row |
| Number per table | At most one clustered index | Multiple non-clustered indexes can be created |
| When more columns are needed | Data is already present in the clustered leaf row | SQL Server may perform a key lookup or RID lookup if required columns are not in the index |
4. How does SQL Server search through a B+ Tree?
Let's assume we have an Employees table and a clustered index on EmployeeId.
CREATE TABLE Employees
(
EmployeeId INT PRIMARY KEY CLUSTERED,
EmployeeName VARCHAR(100),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);
Now execute this query:
SELECT * FROM Employees WHERE EmployeeId = 103;
Conceptually, SQL Server performs the following operations:
- Read the root page.
SQL Server checks the key ranges stored in the root page to determine which child page can contain EmployeeId 103. - Follow the page pointer.
It moves to the relevant intermediate page, if the tree has one or more intermediate levels. - Reach the leaf page.
It follows the next pointer until it reaches the leaf page containing the relevant key range. - Locate the row.
It finds EmployeeId 103 in the clustered leaf level. Because this is a clustered index, the leaf level contains the complete table row. - Return the requested columns.
SQL Server returns the matching row to the query.
When the optimizer chooses this navigation strategy to locate matching rows, the execution plan commonly shows an Index Seek on the clustered index.
5. How does a range query use the B+ Tree?
Consider this query:
SELECT * FROM Employees WHERE EmployeeId BETWEEN 100 AND 200;
SQL Server can navigate from the root to the leaf page containing the starting key, 100. It then reads the matching leaf entries in key order until it has passed the ending key, 200.
Leaf pages are connected in logical key order, which makes it efficient to continue through a range of index entries without repeatedly navigating from the root for every row.
The exact access method chosen by the optimizer depends on the query, available indexes, statistics, estimated row counts and cost. A range query is not guaranteed to use an index seek in every situation.
6. Why is it called a B+ Tree?
B+ Tree is a variation of the balanced tree family. Its key characteristics are that internal nodes are used for navigation, data entries are located at the leaf level, and leaf nodes are linked to support ordered traversal.
| Characteristic | B-Tree (general description) | B+ Tree |
|---|---|---|
| Where records/data references may appear | May appear in internal nodes as well as leaves, depending on the B-tree variant | Data entries are kept at the leaf level |
| Internal nodes | Can contain keys and record references | Primarily contain separator keys and child pointers |
| Leaf-level links | Not a defining requirement of every B-tree variant | Leaf pages are linked for ordered traversal |
| Search path | May find a record before reaching a leaf | Search proceeds to the leaf level |
SQL Server's rowstore index structure is commonly described as a B+ Tree. The important practical point is how its index pages guide searches and how the leaf level differs for clustered and non-clustered indexes.
7. Why does SQL Server use a B+ Tree?
- Efficient point lookups: It can navigate through a small number of levels to find a key.
- Efficient range queries: Ordered and linked leaf pages help SQL Server read consecutive key ranges.
- Balanced structure: The tree remains balanced, so leaf pages are reached through paths of similar length.
- Scales with data size: As the index grows, SQL Server can add pages and levels while maintaining the tree structure.
- Supports modifications: SQL Server can split, redistribute or merge pages as index entries are inserted and deleted.
8. What happens when data is inserted, updated or deleted?
Insert
SQL Server navigates to the appropriate leaf page and inserts the new index entry in key order. If the page has enough free space, the entry can be added there. If the page cannot accommodate it, SQL Server may perform a page split, allocating another page and redistributing entries. Parent-level navigation information is updated as required.
Update
If an update changes a column that participates in the index key, SQL Server may need to remove the old index entry and insert a new one at the correct key position. Updating a non-key column can still modify the leaf row, but it does not necessarily change the tree's key ordering. The exact work depends on the index definition and columns changed.
Delete
SQL Server removes or marks the relevant index entry as deleted, depending on the operation and internal processing. Page space may become available for reuse. Under some conditions, page merges or other index maintenance can reduce sparsely populated pages.
9. Does a B+ Tree mean the rows are physically sorted on disk?
This is a common point of confusion. A clustered index defines the logical key order of rows at the leaf level. However, that does not mean SQL Server guarantees that every page is stored next to the next page in that same order on the physical disk.
SQL Server manages storage through pages, extents and allocation structures. The index links pages in logical key order, while allocation and the underlying storage system determine where those pages are placed. Logical order and physical adjacency are therefore different concepts.
For example, the next page in key order may have a page ID that is not physically adjacent to the current page. SQL Server can still follow the index's page links to continue reading the range.
10. How can you inspect the index tree in SQL Server?
You can inspect index metadata and the number of levels using the Dynamic Management Function sys.dm_db_index_physical_stats.
SELECT
OBJECT_NAME(ips.object_id) AS TableName,
i.name AS IndexName,
ips.index_type_desc,
ips.index_level,
ips.page_count,
ips.avg_page_space_used_in_percent,
ips.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats
(
DB_ID(),
OBJECT_ID('dbo.Employees'),
NULL,
NULL,
'DETAILED'
) AS ips
INNER JOIN sys.indexes AS i
ON ips.object_id = i.object_id
AND ips.index_id = i.index_id
ORDER BY
i.name,
ips.index_level DESC;
The index_level column identifies the level reported by the function: 0 is the leaf level, and higher values represent levels above it. The root is the highest level for that index. The function can return multiple rows for different partitions or allocation units, so interpret the output accordingly.
11. Interview-ready answer
A B+ Tree is a balanced tree structure SQL Server uses to organize rowstore indexes. It contains a root level, optional intermediate levels and a leaf level. The root and intermediate pages store index keys and pointers that help SQL Server navigate to the correct page. In a clustered index, the leaf level contains the actual table rows; in a non-clustered index, it contains index entries and row locators. This structure supports efficient index seeks and range scans because SQL Server can navigate to the required key range and then traverse the leaf pages in key order.
12. Frequently asked follow-up questions
Does every B+ Tree have root, intermediate and leaf pages?
Every tree has a root and leaf level, but an index does not always need intermediate levels. A small index may have only a root page that also serves as the leaf level, or a root page pointing directly to leaf pages.
Are the leaf pages of a clustered index the same as the table data?
Yes. For a rowstore clustered index, the leaf level contains the table's data rows. That is why a table with a clustered index is stored as the clustered index structure.
Are non-clustered index leaf pages just pointers?
No. They contain the non-clustered index key values and any included columns, as well as a row locator. The locator is a clustered key for a table with a clustered index, or a RID for a heap.
Does an index seek always mean better performance than an index scan?
No. An index seek is not automatically faster. If a query needs a large proportion of the table, scanning can be cheaper than performing many lookups. SQL Server's optimizer chooses an access method based on estimated cost and available information.
What is the relationship between a B+ Tree and a page split?
A page split can occur when an insert or key-changing update needs space on a full leaf page. SQL Server allocates another page and redistributes entries, then maintains the parent-level navigation information. Page splits can affect page density and fragmentation.
Key takeaways
- A B+ Tree is the navigation structure behind SQL Server rowstore indexes.
- The root and intermediate pages guide SQL Server through key ranges using child-page pointers.
- The leaf level contains the clustered table rows or the non-clustered index entries and row locators.
- Index seeks navigate to a key range; range scans can continue through linked leaf pages.
- Logical key order does not guarantee physical disk adjacency of pages.
- Page splits and fragmentation are important consequences to understand when indexes are modified.
Next topic: Page Splits and Index Fragmentation in SQL Server — what happens internally when an index leaf page runs out of space?