Understanding B+tree Indexes and how they Impact Performance

本文深入探讨了B+树索引的工作原理,解释了它们如何通过减少磁盘访问次数来提高数据库查询速度。重点分析了B+树的结构特性,包括节点大小选择、高度限制和节点链接方式,以及这些特性如何影响性能。同时,阐述了索引值长度的重要性,以及它如何影响B+树的高度和查询效率。

Indexes are a very important part of databases and are used frequently to speed up access to particular data item or items. So before working with indexes, it is important to understand how indexes work behind the scene and what is the data structure that is used to store these indexes, because unless you understand the inner working of an index, you will never be able to fully harness its power.

B+tree Indexes

Indexes are stored on disk in the form of a data structure known as B+tree. B+tree is in many ways similar to a binary search tree. B+tree follows on the same structure as of a binary search tree, in that each key in a node has all key values less than the key as its left children, and all key values more than the key as its right children.
But there are some very important differences,

  • B+tree can have more than 1 keys in a node, in fact thousands of keys is seen typically stored in a node and hence, the branching factor of a B+tree is very large and that allows the B+trees to be a lot shallower as compared to their binary search tree counterparts.
  • B+trees have all the key values in their leaf nodes. All the leaf nodes of a B+tree are at the same height, which implies that every index lookup will take same number of B+tree lookups to find a value.
  • Within a B+tree all leaf nodes are linked together in a linked-listed, left to right, and since the values at the leaf nodes are sorted, so range lookups are very efficient.

A typical B+tree

Following is a typical B+tree:
Bplustree

If you need more information on B+trees, you can have a detailed look at the article available on Wikipedia.

Why use B+tree?

B+tree is used for an obvious reason and that is speed. As we know that there are space limitations when it comes to memory, and not all of the data can reside in memory, and hence majority of the data has to be saved on disk. Disk as we know is a lot slower as compared to memory because it has moving parts. So if there were no tree structure to do the lookup, then to find a value in a database, the DBMS would have to do a sequential scan of all the records. Now imagine a data size of a billion rows, and you can clearly see that sequential scan is going to take very long.
But with B+tree, its possible to store a billion key values (with pointers to billion rows) at a height of 3, 4 or 5, so that every key lookup out of the billion keys is going to take 3, 4 or 5 disk accesses, which is a huge saving.

The reason why B+tree is chosen over other tree structures is that B+trees tend to be very shallow, and since every lookup translates to a disk access, the number of disk accesses required to fetch a value is directly proportional to the height of the tree, so the shallower the tree, the less number of disk accesses.

How is B+tree structured?

B+trees are normally structured in such a way that the size of a node is chosen according to the page size. Why? Because whenever data is accessed on disk, instead of reading a few bits, a whole page of data is read, because that is much cheaper.
Let us look at an example,
Consider InnoDB whose page size is 16KB and suppose we have an index on a integer column of size 4bytes, so a node can contain at most 16 * 1024 / 4 = 4096 keys, and a node can have at most 4097 children.
So for a B+tree of height 1, the root node has 4096 keys and the nodes at height 1 (the leaf nodes) have4096 * 4097 = 16781312 key values.
This goes to show the effectiveness of a B+tree index, more than 16 million key values can be stored in a B+tree of height 1 and every key value can be accessed in exactly 2 lookups.

How important is the size of the index values?

As can be seen from the above example, the size of the index values plays a very important role for the following reasons:

  • The longer the index, the less number of values that can fit in a node, and hence the more the height of the B+tree.
  • The more the height of the tree, the more disk accesses are needed.
  • The more the disk accesses the less the performance.

So the size of the index values have a direct bearing on performance!

I hope you have understood how B+tree indexes work and how they are used to improve the performance of lookups. I hope you have also understood how important it is to keep the height of the B+tree smaller so as to reduce the number of disk accesses.

### InnoDB 存储引擎中 B+ 树二级索引工作机制 #### 1. B+ 树结构概述 B+ 树是一种平衡树数据结构,广泛用于数据库管理系统中的索引实现。在 MySQL 的 InnoDB 存储引擎里,B+ 树被用来构建高效的索引体系。与普通的 B 树不同,在 B+ 树中只有叶子节点存储真实的数据记录,而非叶节点仅保存键值用于引导查找路径[^3]。 #### 2. 二级索引定义 对于 InnoDB 表而言,默认情况下会创建一个主键作为聚簇索引(clustered index),而其他任何额外建立的索引则被称为二级索引(secondary indexes) 或者辅助索引(auxiliary indexes)[^5]。当用户基于某个字段创建了二级索引之后,该字段上的查询操作就可以利用这个索引来加速访问速度。 #### 3. 工作流程解析 假设存在一张 `users` 表,并且已经针对 `email` 列建立了名为 `idx_email` 的二级索引: - 当执行如下 SQL 查询语句时: ```sql SELECT * FROM users WHERE email='example@domain.com'; ``` - 首先会在内存缓存池(Buffer Pool) 中尝试定位到对应的页(Page), 如果命中,则直接读取;如果没有找到对应页面,则需要从磁盘加载相应部分进入缓冲区; - 接着按照 idx_email 这棵 B+ 树自顶向下逐层比较目标邮箱字符串与其他节点内的关键字大小关系直到抵达最底层即叶子结点位置; - 叶子结点除了包含 email 键外还会携带指向实际行记录物理地址的信息(通常是主键ID),因此下一步就是依据此信息再次回表扫描获取完整的列值集合返回给客户端应用。 这种设计使得即使是在大规模数据集上也能保持较高的检索效率,因为每次只需要遍历一棵相对较小的高度固定的树形结构即可完成精准匹配过程[^4]。 ```python def search_secondary_index(index_tree, key_value): current_node = index_tree.root while not is_leaf(current_node): # 不断深入直至到达叶子节点 child_pointer = find_child_pointer(current_node, key_value) current_node = follow(child_pointer) row_id = locate_row_in_leaf(current_node, key_value) return fetch_full_record(row_id) def is_leaf(node): """判断当前节点是否为叶子节点""" pass def find_child_pointer(node, value): """寻找合适的子节点指针""" pass def follow(pointer): """跟随指针移动至下一个节点""" pass def locate_row_in_leaf(leaf, target_key): """在叶子节点内定位具体的行号""" pass def fetch_full_record(record_identifier): """根据行标识符抓取整条记录""" pass ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值