手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle Database 中 B*Tree 索引内部维护

[日期:2012-12-28] 来源:Linux社区  作者:renfengjun [字体:大 中 小]

删除B*Tree索引中大多数的行后…


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If a table has 100,000 rows and 99,999 of 100,000 rows and index entries are deleted. How is the index balanced?

如果表中拥有100000行数据,并且其中99999行数据和索引条目被删除了,那么索引时怎样平衡的呢 ?

In this case the rows are deleted from the index, and the empty blocks inserted onto the index free list. These blocks are still part of the index and will need to be accessed when traversing the index.

在这里数据会从index中删除,并且空的blocks 会被插入到索引 free list中。这些blocks一直是这个索引的一部分,并且并且在检索数据的时候,依然需要访问这些块。
<p>
Thus if an index has one entry in it, there is only one block (a root/leaf block) in the tree. When searching the tree only one block needs to be accessed to answer thequery. If you load a B* Tree with 100,000 rows and get a tree with say 3 Levels.

因此,如果一个索引只有一个条目,只有一个block,root branch block=leaf block 。访问的时候仅需要访问这个块即可。如果加载了数据,with 100000条,并且得到了tree with 3 levels 。

Levels zero and one are Branch blocks used to access the data in the Leaf blocks on level 2. When querying this tree you first access the root block using the search key to find correct Branch block in level one of the Tree.

level 0 和 1 是分支块branch block 用来取得leaf block中的数据(on level 2)。当查询这个tree , 第一需要访问的是root 块,去查找 相应的 branch block (level 1) 。

Next you use the search key and the Branch block to find the correct Leaf block that should contain the key being sought. So it takes three block accesses to answer the same query. Now if 99,999 rows were deleted from the tree the rows are removed from the index but the index is not collapsed.

下一步,需要使用 需要查找的键值 和branch block 分支块去查找正确的 叶块。 所以需要访问3个块去相应查询。现在如果99999行数据被删除,但是索引没有收缩、合并。

In this case you still have a 3 level index to store the one row and it will still take three block accesses to access that row. The fact that we need three block accesses instead of one does not mean this tree is unbalanced. The tree is still balanced it just contains a lot of empty blocks.

在这种情况下,仍然需要3个级别的索引 去存储 这行数据,并且依然需要访问3个块才能够得到相应的数据。这意味着我们需要访问3个block 而不是一个block ,但是这并不意味着tree 不是平衡的tree 依然是平衡的 ,只是包含了一些 空块。
</p>
The reason for doing this is that, when data is inserted into a Leaf block, and there is no room for the insert, a very expensive operation called a split occurs. The split creates a new Leaf block and possibly new Branch blocks as well to maintain the balance of the tree. The split operation is by far the most expensive operation that is done in the maintenance of B* trees so we go to great lengths to avoid them. By not collapsing the now unused levels out of the B*Trees after large deletes these levels (with splits) do not have to be recreated during future inserts.

索引分裂操作是为了维护btree索引的 平衡(这是一个非常耗费资源的操作)。如果不收缩未使用的levels block ,那么这些块中的空闲空间,则会在索引条目插入的时候用到,有效的减少了索引分裂操作。

If most of the rows in a table are going to be deleted, and not refilled soon after, it is advisable to drop the index, delete the rows, and then recreate the index.

如果表中大多数的行被删除,并且在短期内不会被重新加载。那么这个时候可以考虑重建索引,drop 掉 index ,然后recreate it 。

By dropping the index you save the index maintenance that needs to be done during the delete thus speeding up the delete. By recreating the index after the delete you create an index of optimal height and with the Leaf blocks filled to an optimal level.

删除索引,可以让我们省去索引维护的事情,比如说删除索引中数据的操作会导致性能降低。在删除数据后重建索引,可以调整height ,将leaf block 放在一个合适的级别上。

linux
相关资讯       Oracle索引  Oracle Database 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款