<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx</link><description>There is much debate about how to implement hierarchies (trees) relationally in SQL Server. I decided to test the main techniques out and see which performed best for my application.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#532849</link><pubDate>Thu, 16 Feb 2006 02:40:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:532849</guid><dc:creator>Roger Bucks</dc:creator><description>There seems to be another mechanism for this in SQL Server 2005 (I found the link to it &lt;a rel="nofollow" target="_new" href="http://www.sqlservercentral.com/columnists/sSampath/recursivequeriesinsqlserver2005.asp"&gt;http://www.sqlservercentral.com/columnists/sSampath/recursivequeriesinsqlserver2005.asp&lt;/a&gt;)&lt;br&gt;Unfortunately I have not had a chance to try this out. Have you experimented with this technique?</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#532857</link><pubDate>Thu, 16 Feb 2006 02:54:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:532857</guid><dc:creator>AnthonyBloesch</dc:creator><description>This is the adjacency approach mentioned in the post. They use recursive CTE queries also.&lt;br&gt;In early betas of SQL Server 2005, recursive CTE queries generated poor query plans so I did this calculation with a stored procedure. However, now recursive CTE queries generate good query plans so I use CTE queries.</description></item><item><title>re: Broken image links</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#532865</link><pubDate>Thu, 16 Feb 2006 03:15:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:532865</guid><dc:creator>Chui</dc:creator><description>Images referring to your file system my friend&lt;br&gt;&lt;br&gt;file://Tkzaw-pro-14/Mydocs3/ABloesch/My%20Documents/Projects/Blog/Tree%</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#532869</link><pubDate>Thu, 16 Feb 2006 03:24:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:532869</guid><dc:creator>Roger Bucks</dc:creator><description>Thanks for the update.&lt;br&gt;Do you have a sample of the CTE query that you might use in the example you give above?&lt;br&gt;(To understand how the performance figures were obtained, is it possible to link to/display the SQL used?)&lt;br&gt;</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#544208</link><pubDate>Mon, 06 Mar 2006 06:01:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:544208</guid><dc:creator>Cel;ko</dc:creator><description>Someone quoted your blog on an SQL Sever newsgroup that an adjacency list tbel is just fine. &amp;nbsp;I took a quick peek at the blog. &amp;nbsp;So here Ia m for details. &lt;br&gt;&lt;br&gt;You seemed to be measring loading time, without telling us the fomat of the data. If you are loading (superior, subordinate) pairs without any cycle and gap checking. &amp;nbsp;You did do FULL checking on EACH edge of the graph? No cycles? &amp;nbsp; Do you have a solution to an NP-Complete Problem? Or a proof it is impossible? &amp;nbsp;Ghod, you will be famous!! &lt;br&gt;&lt;br&gt;I have 20+ years of SQL in 177+ produccts and when I wrote my code, it sucked rocks. &amp;nbsp;Show what you do for data integrity. I am amazed because you did better than I have been able to do in 10+ years of trying!! &amp;nbsp;&lt;br&gt;&lt;br&gt;How many thousand summary queries in a tree of depth 5, 10 and 20 levels did you do in the models? &amp;nbsp;What ere the numbers? &amp;nbsp;&lt;br&gt;&lt;br&gt;If you do the adjacency list model done right, you have to insert (in_node, out_node) pairs and then check the ENTIRE schema for cycles with a TRIGGER every time. &amp;nbsp;Then you have to look for gaps to assure it is one tree. &amp;nbsp;And finally check that &amp;quot;# of nodes = (# of edges) - 1&amp;quot;&lt;br&gt;&lt;br&gt;This is not an option -- it is vital. &amp;nbsp;I am waiting for $1000/day contract to come thru with an insurance company that forgot these constraints [Opps!! we are dead! and since we do not expect a right answer, it is always 42:)]&lt;br&gt;&lt;br&gt;Test methods 20 levels deep and 20 levels across -- the schema for a 747 airplane parts explosion. Really. &amp;nbsp;&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#643668</link><pubDate>Fri, 23 Jun 2006 07:24:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:643668</guid><dc:creator>shanzy</dc:creator><description>wonderful article!!&lt;br&gt;&lt;br&gt;A question:&lt;br&gt;&lt;br&gt;Where I can found the Class (namespace) Hierarchy Chart for .net 2.0? Just like the MFC Hierarchy Chart in MSDN2001? Is it &lt;br&gt;&lt;br&gt;available from microsoft or MSDN? Can you give me a &amp;nbsp;link?&lt;br&gt;&lt;br&gt;Thank you very much!!&lt;br&gt;</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#793363</link><pubDate>Thu, 05 Oct 2006 13:20:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:793363</guid><dc:creator>Misha</dc:creator><description>&lt;p&gt;Do you have any data on the relative speed of the recursive CTE vs (your old) stored procedure versions of the adjacency model?&lt;/p&gt;
&lt;p&gt;Have you tried a materialised ancestor version of the adjacency model (using either triggers or indexed views)?&lt;/p&gt;
&lt;p&gt;Can you publish the code used in the tests?&lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#1547301</link><pubDate>Sun, 28 Jan 2007 18:25:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1547301</guid><dc:creator>Chandrashekar Kollipara</dc:creator><description>&lt;p&gt;When you post such an article on msdn.com there should be considerable amount of test data that would proove what you put forward. I have been working in this area for last 10 years and agree with Celko 100%. Please dont misguide people if you cant guide them to the correct path&lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#1997825</link><pubDate>Sat, 31 Mar 2007 08:35:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1997825</guid><dc:creator>Cesar</dc:creator><description>&lt;p&gt;I know it is too late to ask, but where is the data to test your experiment?&lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#9143106</link><pubDate>Wed, 26 Nov 2008 05:06:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9143106</guid><dc:creator>dan</dc:creator><description>&lt;p&gt;To the person making comments that sound a bit nasty, and mentioning NP-Complete. &amp;nbsp;it may come as a surprise to you, but not everyone releases everything they have, and there are indeed some solutions out there you have not heard of.&lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#9397851</link><pubDate>Thu, 05 Feb 2009 11:41:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9397851</guid><dc:creator>mbuzina</dc:creator><description>&lt;p&gt;I am missing the classical datawarehouse approach. I know it is mostly focused on querying, but in my personal experiance, the insert performance is not too bad, depending on the model of inserts.&lt;/p&gt;
&lt;p&gt;I have successfully used a combination of adjencency with a cache table for holding all relationships. I would have the following cache table&lt;/p&gt;
&lt;p&gt;emp_cache&lt;/p&gt;
&lt;p&gt;descendant,ancestor,distance&lt;/p&gt;
&lt;p&gt;2,2,0&lt;/p&gt;
&lt;p&gt;1,1,0&lt;/p&gt;
&lt;p&gt;1,2,1&lt;/p&gt;
&lt;p&gt;3,3,0&lt;/p&gt;
&lt;p&gt;3,2,1&lt;/p&gt;
&lt;p&gt;4,4,0&lt;/p&gt;
&lt;p&gt;4,3,1&lt;/p&gt;
&lt;p&gt;4,2,2&lt;/p&gt;
&lt;p&gt;5,5,0&lt;/p&gt;
&lt;p&gt;5,3,1&lt;/p&gt;
&lt;p&gt;5,2,1&lt;/p&gt;
&lt;p&gt;Additionally I would add a depth column to the base table, which allows a quicker computation of the distance value inside the trigger maintaining the cache and have the cache table indexed (depending on your query patterns). Inserting, updateing &amp;amp; deleting is done exactly as with adjacency, which makes sure there are no loops at all. Querying will use the cache table (&amp;amp; depth column). Querying the cache is quite natural to sql.&lt;/p&gt;
&lt;p&gt;I used this to handle a cost center structure which was 17 levels deep and handled approx. 15.000 nodes.&lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#9530459</link><pubDate>Fri, 03 Apr 2009 12:37:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9530459</guid><dc:creator>Yoseph</dc:creator><description>&lt;p&gt;When using Nested set, if you want to retrieve all the tree (root node and all of its descendants), all you need to do is: SELECT * FROM tree ORDER BY left ASC. The process of generating the tree is supposed to be handled by your application. So the query needed is only 1, not 6072.&lt;/p&gt;
&lt;p&gt;You can see the implementation of this tree in PHP ORM framework (Propel) -&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/Tree/NestedSet"&gt;http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/Tree/NestedSet&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is some code of the implementation of retrieving tree:&lt;/p&gt;
&lt;p&gt;public static function retrieveTree(PropelPDO $con = null)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		$c = new Criteria(TreePeer::DATABASE_NAME);&lt;/p&gt;
&lt;p&gt;		$c-&amp;gt;addAscendingOrderByColumn(self::LEFT_COL);&lt;/p&gt;
&lt;p&gt;		/*&lt;/p&gt;
&lt;p&gt;		EXECUTE QUERY:&lt;/p&gt;
&lt;p&gt;		SELECT * FROM tree ORDER BY lft ASC&lt;/p&gt;
&lt;p&gt;		*/&lt;/p&gt;
&lt;p&gt;		$stmt = TreePeer::doSelectStmt($c, $con);&lt;/p&gt;
&lt;p&gt;		if (false !== ($row = $stmt-&amp;gt;fetch(PDO::FETCH_NUM))) {&lt;/p&gt;
&lt;p&gt;			$root = new Tree();&lt;/p&gt;
&lt;p&gt;			$root-&amp;gt;hydrate($row);&lt;/p&gt;
&lt;p&gt;			$root-&amp;gt;setLevel(0);&lt;/p&gt;
&lt;p&gt;			$tree = self::hydrateDescendants($root, $stmt);&lt;/p&gt;
&lt;p&gt;			$stmt-&amp;gt;closeCursor();&lt;/p&gt;
&lt;p&gt;			return $tree;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;		return false;&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;Note: only 1 query is executed.&lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#9595151</link><pubDate>Fri, 08 May 2009 01:31:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9595151</guid><dc:creator>Niclas</dc:creator><description>&lt;p&gt;Hi there, &lt;/p&gt;
&lt;p&gt;I have a problem which i wonder if you could point me in direction how to solve. &lt;/p&gt;
&lt;p&gt;I have a tree structure where each node have a &amp;quot;rating&amp;quot; column (int). For &amp;quot;composite&amp;quot; nodes, nodes with children the rating column should take the value of the max value of all its children. What kind of structure do i represent such a tree best in? &lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#9893608</link><pubDate>Thu, 10 Sep 2009 15:23:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9893608</guid><dc:creator>OrbMan</dc:creator><description>&lt;p&gt;The high value for all descendents of the path model makes me think the path column was not indexed - can you confirm whether it was or not?&lt;/p&gt;
</description></item><item><title>re: Hierarchies (trees) in SQL Server 2005</title><link>http://blogs.msdn.com/anthonybloesch/archive/2006/02/15/Hierarchies-in-SQL-Server-2005.aspx#9922891</link><pubDate>Mon, 16 Nov 2009 11:28:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9922891</guid><dc:creator>Jon</dc:creator><description>&lt;p&gt;I don't know this Celko. Wikipedia says:&lt;/p&gt;
&lt;p&gt;He is often corrected by product experts including a number of the SQL Server MVP's which lead to protracted discussions which more often than not he comes out losing.&lt;/p&gt;
&lt;p&gt;All I know is he is a brag who's not helping average programmers... I have 20+ experience and paid $1000s of a day. Ashol. Same with you Chandrashekar. I bet you dont even know what you are saying.&lt;/p&gt;
</description></item></channel></rss>