Hierachical Indexing

You can create a depth-first index or a breadth-first index (or both) on your hierarchical tables. The two types differ in how SQL Server physically stores node references in the index. Defining depth-first and breadth-first indexes can have a significant impact on performance for accessing data in hierarchical tables.As their names imply, depth-first indexing stores parent and child node references near each other, whereas breadth-first indexing stores references for nodes at the same hierarchical level near each other. Therefore, you will choose the appropriate type of index based on an understanding of how your hierarchical data is shaped in the table, how it will grow, and how it will be typically queried by client applications. You can create both types of indexes as well, for efficient access both vertically and horizontally across the tree

 

Depth-First Indexing

Defining either a primary key index or a unique index on a hierarchyid column results in a depth-first index. Because the NodeId column (of type hierarchyid) is designated as the primary key in the employee table, you have already created a depth-first index.With depth-first indexing, SQL Server tries to physically store references to nodes in a subtree as near to each other as possible. By “near each other,” we mean that SQL Server records them on disk in the same page, if possible—or in the same extent, if not, and so on—in order to maximize query performance. This strategy yields high query performance if your hierarchy runs very many levels deep. Creating a depth-first index for such a hierarchy will result in very fast vertical searching on the tree (that is, querying ancestors and descendants up and down a potentially long chain)

 

Breadth-First Indexing

With breadth-first indexing, reference to nodes at the same level of the hierarchy are physically stored as near to each other as possible. This type of index yields high query performance for trees that grow very broad. If there are many children beneath the parents in your hierarchy, you will want to create a breadth-first index to enable fast horizontal searching across a potentially large number of nodes at the same level

 

To define a breadth-first index, you create a composite index on your hierarchical table that includes two columns: the integer column that holds the level of the node within the hierarchy (such as the NodeLevel column defined in the employee table, based on the GetLevel method against the NodeId column) and the hierarchyid column itself (NodeId). So to create a breadth-first index, run the following code:

CREATE UNIQUE INDEX IX_EmployeeBreadth
 ON Employee(NodeLevel, NodeId)

As we mentioned already, one table can have both depth-first and breadth-first indexes by creating one primary key or unique index on the hierarchyid column and another composite index on the node-level column and the hierarchyid. This will carry slightly more overhead for data manipulation language (DML) actions, because updates will need to be performed in both indexes; however, query performance will be very fast for both horizontal and vertical searching across large hierarchies that are both very broad and very deep.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值