SQL SERVER 2008 Hierarchyid数据类型

以往我们在关系数据库中建立树状结构的时候,通常使用ID+ParentID来实现两条纪录间的父子关系。但这种方式只能标示其相对位置。解决这类问题在SqlServer2005出现之前通常是采用游标来操作,但熟悉数据库内部机制的人都知道使用游标带来的性能问题和其他问题是比较严重的。 到了SqlServer2005下,可以选择用CTE来做递归查询,这种方式查询比较简练,但由于数据库内部是采用递归查询的方式,其效率依旧不高;为了能够实现既简练又高效的查询,通常的做法是增加冗余字段,比如增加一个"Path"字段,查询时用模糊查询来进行左匹配。对Path建索引后,这种查询的效率还是相当高的,因此这种方式也是一种常规的设计方式; SQL SERVER 2008引入了新的hierarchyid数据类型,可以用它来做本地存储并且在树层次结构中管理其位置.只用这个函数能简洁地表示层次结构中的位置.该函数提供的一些内置的函数方法可以操作和遍历层次结构,使得存储和查询分层数据更为容易,而不需要像那样通过CTE递归来获得. Hierarchyid类型其实是一个CLR自定义数据类型依次打开:数据库->系统数据库->master->可编程性->类型->系统数据类型->CLR数据类型->hierarchyid,可以看到该数据类型. 于hierarchyid有关的一些函数主要有: •GetAncestor :取得某一个级别的祖先 •GetDescendant :取得某一个级别的子代 •GetLevel :取得级别 •GetRoot :取得根 •IsDescendantOf :判断某个节点是否为某个节点的子代 •Parse :将字符串转换为hierarchyid。该字符串的格式通常都是/1/这样的 •Read :Read 从传入的BinaryReader 读取SqlHierarchyId 的二进制表示形式,并将SqlHierarchyId 对象设置为该值。不能使用Transact-SQL 调用Read。请改为使用CAST 或CONVERT。 •GetReparentedValue :可以用来移动节点(或者子树) •ToString :将hierarchyid转换为字符串,与parse正好相反 •Write : 将SqlHierarchyId 的二进制表示形式写出到传入的BinaryWriter 中。无法通过使用Transact-SQL 来调用Write。请改为使用CAST 或CONVERT。 hierarchyid 数据类型的值表示树层次结构中的位置。hierarchyid 的值具有以下属性: •非常紧凑 在具有 n 个节点的树中,表示一个节点所需的平均位数取决于平均端数(节点的平均子级数)。端数较小时 (0-7),大小约为 6*logAn 位,其中 A 是平均端数。对于平均端数为 6 级、包含 100,000 个人的组织层次结构,一个节点大约占 38 位。存储时,此值向上舍入为 40 位,即 5 字节。 •按深度优先顺序进行比较 给定两个 hierarchyid 值 a 和 b,a Ravi | | Ben<-> Laura Vijay <-> Frank <-> James Use AdventureWorksLT Go --Scheme Creation Create Schema HumanResources Go --Table Creation CREATE TABLE HumanResources.EmployeeDemo ( OrgNode HIERARCHYID, EmployeeID INT, LoginID VARCHAR(100), Title VARCHAR(200), HireDate DATETIME ) Go --Index Creation CREATE UNIQUE CLUSTERED INDEX idxEmployeeDemo ON HumanResources.EmployeeDemo (OrgNode,EmployeeID) 下面插入一些数据 SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (hierarchyid::GetRoot(), 1,'adventure-works\scott', 'CEO', '3/11/05') ; CLARE @Manager hierarchyid LECT @Manager = hierarchyid::GetRoot() FROM HumanResources.EmployeeDemo; SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (@Manager.GetDescendant(NULL,NULL), 2, 'adventure-works\Mark', 'CTO', '4/05/07') CLARE @Manager hierarchyid CLARE @FirstChild hierarchyid LECT @Manager = hierarchyid::GetRoot() FROM HumanResources.EmployeeDemo; lect @FirstChild = @Manager.GetDescendant(NULL,NULL) SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (@Manager.GetDescendant(@FirstChild,NULL), 3, 'adventure-works\ravi', 'Director Marketing', '4/08/07') Insert the First Descendant of a Child Node CLARE @Manager hierarchyid LECT @Manager = CAST('/1/' AS hierarchyid) SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (@Manager.GetDescendant(NULL, NULL),45, 'adventure-works\Ben','Application Developer', '6/11/07') ; Insert the Second Descendant of a Child Node CLARE @Manager hierarchyid CLARE @FirstChild hierarchyid LECT @Manager = CAST('/1/' AS hierarchyid) LECT @FirstChild = @Manager.GetDescendant(NULL,NULL) SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (@Manager.GetDescendant(@FirstChild, NULL),55, 'adventure-works\Laura','Trainee Developer', '6/11/07') ; Insert the first node who is the Descendant of Director Marketing CLARE @Manager hierarchyid CLARE @FirstChild hierarchyid LECT @Manager = CAST('/2/' AS hierarchyid) SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (@Manager.GetDescendant(NULL, NULL),551, 'adventure-works\frank','Trainee Sales Exec.', '12/11/07') ; Insert the second node who is the Descendant of Director Marketing CLARE @Manager hierarchyid CLARE @FirstChild hierarchyid LECT @Manager = CAST('/2/' AS hierarchyid) LECT @FirstChild = @Manager.GetDescendant(NULL,NULL) SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (@Manager.GetDescendant(@FirstChild, NULL),531, 'adventure-works\vijay','Manager Industrial Sales', '12/09/06') ; Insert the third node who is the Descendant of Director Marketing in between 2 existing descendants CLARE @Manager hierarchyid CLARE @FirstChild hierarchyid CLARE @SecondChild hierarchyid LECT @Manager = CAST('/2/' AS hierarchyid) LECT @FirstChild = @Manager.GetDescendant(NULL,NULL) LECT @SecondChild = @Manager.GetDescendant(@FirstChild,NULL) SERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) LUES (@Manager.GetDescendant(@FirstChild, @SecondChild),543, 'adventure-works\james','Manager Consumer Sales', '12/04/06') ; Hierarchyid字段类型提供了一系列相关查询函数,可以方便的查询父子关系数据。下面我们查询下数据 DECLARE @TID hierarchyid SELECT @TID=OrgNode FROM HumanResources.EmployeeDemo WHERE title='cto' SELECT *, OrgNode.GetLevel() as 层次,OrgNode.ToString() as 路径 FROM HumanResources.EmployeeDemo WHERE @TID.IsDescendantOf(OrgNode)=1 SELECT *, OrgNode.GetLevel() as 层次,OrgNode.ToString() as 路径 FROM HumanResources.EmployeeDemo WHERE OrgNode.IsDescendantOf(@TID)=1 下面另外附几个操作的存储过程: •向表里插入记录 SET QUOTED_IDENTIFIER ON GO --Use Serializable Transaction CREATE PROCEDURE [dbo].[AddEmployee](@ManagerID hierarchyid, @EmpID int, @LogID varchar(100), @JobTitle as varchar(200), @JoiningDate datetime) AS BEGIN DECLARE @LastChild hierarchyid SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRANSACTION SELECT @LastChild = Max(OrgNode) From HumanResources.EmployeeDemo WHERE OrgNode = @ManagerID INSERT HumanResources.EmployeeDemo (OrgNode, EmployeeID, LoginID, Title, HireDate) VALUES(@LastChild, @EmpID,@LogID , @JobTitle, @JoiningDate) COMMIT END ; •移动层级关系 CREATE PROCEDURE MoveOrg(@oldMgr nvarchar(256), @newMgr nvarchar(256) ) AS BEGIN DECLARE @nold HierarchyID DECLARE @nnew HierarchyID SELECT @nold = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = @oldMgr ; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRANSACTION SELECT @nnew = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = @newMgr ; SELECT @nnew = @nnew.GetDescendant(max(OrgNode), NULL) FROM HumanResources.EmployeeDemo WHERE OrgNode.GetAncestor(1)=@nnew ; UPDATE HumanResources.EmployeeDemo SET OrgNode = OrgNode.GetReparentedValue(@nold, @nnew) WHERE @nold.IsDescendantOf(OrgNode) = 1 COMMIT TRANSACTION END •获取最大的子节点,传递给GetDescendant() 函数获得新的子节点 Create Function GetMyMaxChild(@ManagerID as BigInt) Returns HierarchyID BEGIN Declare @ManagerNode HierarchyID Declare @MaxChild HierarchyID --Get the ManagerNode Select @ManagerNode = OrgNode from HumanResources.EmployeeDemo Where EmployeeID = @ManagerID --Get the Max Child Select @MaxChild = Max(OrgNode) from HumanResources.EmployeeDemo Where OrgNode.GetAncestor(1) = @ManagerNode --Return the Value RETURN @MaxChild END 此文章链接: http://www.cnblogs.com/shanyou/archive/2011/07/01/2095968.html 参考文章: http://www.bianceng.cn/database/SQLServer/201408/43555_2.htm
posted @ 2016-04-12 15:55  凫.鸳  阅读(415)  评论(0编辑  收藏  举报