0% completed
Tree-based indexing, a multi-level indexing technique is an efficient way to store and retrieve data in databases, especially when dealing with large datasets. Two widely used tree-based structures in database indexing are B-trees and B+ trees. These structures ensure logarithmic time complexity for search, insertion, and deletion operations. This section covers the workings of these structures, their differences, and their use in databases.
A B-tree is a self-balancing search tree that keeps data sorted and allows searches, sequential access, insertions, and deletions to be performed in O(\log(n)) time. Unlike binary search trees, B-trees allow nodes to have multiple keys and more than two children, ensuring the tree remains balanced.
Below is an example of a B-tree with three levels:
100
, 155
, 226
) and pointers to child nodes.100
go to the left child.100
and 155
go to the middle child.155
go to the right child.Imagine storing a list of numbers in an unsorted array. Searching for a value would require scanning every element in the worst case, resulting in O(n) time complexity. Sorting the array and applying binary search improves the complexity to O(\log(n)), but maintaining the sorted order during insertions or deletions becomes inefficient.
Suppose you are searching for 117
in the above B-tree:
100
, 155
, 226
).117
is greater than 100
but less than 155
, move to the middle child node (128
, 140
).105
, 117
).117
.This search involves only three levels, demonstrating the efficiency of (O(\log(n))) operations.
A B+ tree is an extension of the B-tree. It stores all the data at the leaf nodes and keeps internal nodes for navigation only. Additionally, leaf nodes are linked, enabling efficient sequential access.
Below is an example of a B+ tree:
13
, 30
, 9
, 11
, 16
, 38
) contain keys for navigation.1, 4, 9, 11, 12, 13, 16, 30, 38, 42
) store all the data in sorted order.Feature | B-tree | B+ tree |
---|---|---|
Data Storage | Keys and data are stored in all nodes | Data is stored only in leaf nodes |
Range Queries | Less efficient | Highly efficient due to linked leaf nodes |
Traversal | Requires accessing multiple nodes | Sequential traversal via linked leaves |
Tree-based indexing, particularly B+ trees, is widely implemented in database systems to improve query performance.
Name
, Marks
, Age
).Index
), which acts as a key in the B+ tree.Example Table:
Example B+ Tree for the Above Table:
12
, 15
, 24
, 25
) are used for indexing.Jone: Marks=5, Age=28
).How It Works:
Tree-based indexing, particularly B+ trees, is fundamental to modern database systems. While B-trees offer efficient general-purpose indexing, B+ trees excel in scenarios requiring range queries and sequential data access. By leveraging these structures, databases can handle large datasets efficiently, ensuring fast and scalable performance.
.....
.....
.....