DEV Community

Harsh Mange
Harsh Mange

Posted on • Originally published at harshmange.hashnode.dev on

What is Database Indexing by B-Tree Indexing approach?

Introduction

B-Tree indexing is a widely used technique for indexing data in relational databases. It provides fast access to data by creating a balanced tree structure of index pages that contain pointers to the data pages in the table. In this approach, each node in the tree contains a range of values and a pointer to either another index page or a data page.

Under The Hood Structure

Under the hood, a B-Tree index is typically implemented as a multi-level tree structure, with the root node at the top and leaf nodes at the bottom. Each node in the tree contains a range of values and a pointer to either another index page or a data page. The root node contains pointers to the index pages below it, and the leaf nodes contain pointers to the data pages in the table.

Example

To create a B-Tree index, we first select the column or set of columns that we want to index. For example, we might create a B-Tree index on the last_name column in a table of customers.

Next, we create the root node of the tree. The root node contains pointers to the index pages below it, and its range of values covers the entire range of values in the indexed column. For example, if the last_name column contains values from A to Z, the root node would have a range of values from A to Z and pointers to the index pages below it.

Next, we create the first level of index pages. Each index page contains a range of values and pointers to the next level of index pages or the leaf nodes. For example, we might create index pages for the ranges A-F, G-L, M-R, and S-Z.

Finally, we create the leaf nodes of the tree. Each leaf node contains a range of values and pointers to the data pages in the table that contain the actual data for the indexed column. For example, if we have a customer named John Smith with a last name of "S", the leaf node for the S-Z range would contain a pointer to the data page that contains John Smith's record.

To retrieve records from the table using the B-Tree index, we traverse the tree starting at the root node and following the appropriate index page pointers until we reach the leaf node that contains the desired range of values. We then use the pointers in the leaf node to look up the corresponding records in the table.

For example, if we want to retrieve all records with a last_name starting with "S", we would start at the root node and follow the pointer to the S-Z index page. We would then follow the pointer in the leaf node for the S-Z range to look up the corresponding records in the table.

Overall, B-Tree indexing provides a fast and efficient way to access data in large relational databases. The balanced tree structure ensures that lookup times are proportional to the logarithm of the number of records in the table, making it suitable for handling large datasets.

Process

To perform a query using an index, the database engine first looks up the search value in the root node of the B-tree. If the value is found, the engine follows the corresponding pointer to the next node in the tree, continuing this process until it reaches a leaf node that contains the matching data. This process is called a B-tree traversal.

Diagram

                  +---------------+
                  | Root |
                  +---------------+
                 / \
        +--------+--------+ +--------+--------+
        | Node 1 | | Node 2 |
        +--------+--------+ +--------+--------+
       / / \ / \
+--------+--------+ ... +--------+--------+
| Leaf 1 | Leaf 2 | | Leaf 3 | Leaf 4 |
+--------+--------+ +--------+--------+

Enter fullscreen mode Exit fullscreen mode

In this example, the index has two levels: the root node and the leaf nodes. The root node contains pointers to two child nodes, Node 1 and Node 2, which each contain pointers to the leaf nodes. The leaf nodes contain pointers to the rows in the table that match the indexed some DB {field} values.

When a query is executed that filters by some DB {field}, the database engine uses the B-tree index to traverse the tree and locate the leaf nodes that contain the matching rows. This process is much faster than scanning the entire table for matching rows, especially for large tables with many rows.

Top comments (0)