在b-树中,您可以将键和数据存储在内部节点和叶节点中,但在b+树中,您必须仅将数据存储在叶节点中。
在b+树中这样做有什么好处吗?
为什么不在所有地方都使用b-树而不是b+树,因为直觉上它们看起来更快?
我的意思是,为什么需要在b+树中复制键(数据)?
在b-树中,您可以将键和数据存储在内部节点和叶节点中,但在b+树中,您必须仅将数据存储在叶节点中。
在b+树中这样做有什么好处吗?
为什么不在所有地方都使用b-树而不是b+树,因为直觉上它们看起来更快?
我的意思是,为什么需要在b+树中复制键(数据)?
当前回答
B+树相对于B树的主要优点是,它们允许您通过删除指向数据的指针来打包更多指向其他节点的指针,从而增加扇出并潜在地降低树的深度。
缺点是,当您可能在内部节点中找到匹配时,无法提前退出。但由于这两种数据结构都有巨大的扇出,绝大多数匹配都将在叶节点上,这使得B+树的平均效率更高。
其他回答
Adegoke A, Amit
我想你们忽略的一个关键点是数据和指针之间的区别,就像本节中解释的那样。
指针:指向其他节点的指针。
数据:—在数据库索引的上下文中,数据只是另一个指向其他地方的真实数据(行)的指针。
因此在B树的情况下,每个节点都有三个信息键,指向与键相关的数据的指针和指向子节点的指针。
在B+树中,内部节点保存指向子节点的键和指针,而叶节点保存指向相关数据的键和指针。这允许为给定大小的节点提供更多的键数。节点大小主要由块大小决定。
每个节点拥有更多键的好处已经在上面解释过了,这样可以节省我的输入工作量。
In a B tree search keys and data are stored in internal or leaf nodes. But in a B+-tree data is stored only in leaf nodes. Full scan of a B+ tree is very easy because all data are found in leaf nodes. Full scan of a B tree requires a full traversal. In a B tree, data may be found in leaf nodes or internal nodes. Deletion of internal nodes is very complicated. In a B+ tree, data is only found in leaf nodes. Deletion of leaf nodes is easy. Insertion in B tree is more complicated than B+ tree. B+ trees store redundant search keys but B tree has no redundant value. In a B+ tree, leaf node data is ordered as a sequential linked list but in a B tree the leaf node cannot be stored using a linked list. Many database systems' implementations prefer the structural simplicity of a B+ tree.
B+树尤其适用于基于块的存储(例如:硬盘)。考虑到这一点,你会得到几个优势,例如(从我的脑海中):
high fanout / low depth: that means you have to get less blocks to get to the data. with data intermingled with the pointers, each read gets less pointers, so you need more seeks to get to the data simple and consistent block storage: an inner node has N pointers, nothing else, a leaf node has data, nothing else. that makes it easy to parse, debug and even reconstruct. high key density means the top nodes are almost certainly on cache, in many cases all inner nodes get quickly cached, so only the data access has to go to disk.
举个例子——你有一个每一行都有大量数据的表。这意味着对象的每个实例都是大的。
如果在这里使用B树,那么大部分时间都花在扫描带有数据的页面上——这是没有用的。在数据库中,这就是使用B+树来避免扫描对象数据的原因。
B+树将键和数据分开。
但如果你的数据量比较小,你可以用键来存储它们就像B树那样。
由于终端节点形成了一个链表,B+树更容易进行全面扫描,而且性能更高,可以查看树索引的每一块数据。要使用B-Tree进行完整扫描,您需要进行完整的树遍历以查找所有数据。
另一方面,当您执行seek(按键查找特定数据段)时,B-Trees可以更快,特别是当树驻留在RAM或其他非块存储中时。由于可以提升树中常用的节点,因此获取数据所需的比较较少。