Junior — Middle
What is the purpose of using a B-Tree index in databases?
sobes.tech AI
Answer from AI
A B-Tree index is used in databases for efficient storage and quick retrieval of data on disk. Its main goal is to minimize the number of disk read operations during search, insertion, and deletion of records.
Features of B-Tree index:
- Balanced: The tree is always balanced, ensuring logarithmic access time to data.
- Multiple keys in a node: Each node contains several keys and links, reducing the height of the tree and the number of I/O operations.
- Optimized for disk systems: The structure takes into account disk read blocks, enhancing performance.
Thus, a B-Tree index allows fast record retrieval by key, efficient range queries, and maintains data in sorted order.
Example usage in Go (simplified):
// In real projects, B-Tree is implemented in a DBMS, but ready-made libraries can be used
import "github.com/google/btree"
func main() {
tree := btree.New(2) // tree degree
tree.ReplaceOrInsert(btree.Int(5))
tree.ReplaceOrInsert(btree.Int(10))
item := tree.Get(btree.Int(5))
if item != nil {
fmt.Println("Element found", item)
}
}