Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is another way to store/retrieve a tree hierarchy as explained in <a href="http://www.fogcreek.com/blog/post/Subcases-and-Hierarchy.aspx" rel="nofollow noreferrer">this fogbugz</a> blog post:</p> <blockquote> <p>Turns out there's a pretty cool solution for this problem explained by Joe Celko. Instead of attempting to maintain a bunch of parent/child relationships all over your database -- which would necessitate recursive SQL queries to find all the descendents of a node -- we mark each case with a "left" and "right" value calculated by traversing the tree depth-first and counting as we go. A node's "left" value is set whenever it is first seen during traversal, and the "right" value is set when walking back up the tree away from the node. A picture probably makes more sense: </p> <p><img src="https://i.stack.imgur.com/4Otew.png" alt="enter image description here"></p> <p>The Nested Set SQL model lets us add case hierarchies without sacrificing performance.</p> <p>How does this help? Now we just ask for all the cases with a "left" value between 2 and 9 to find all of the descendents of B in one fast, indexed query. Ancestors of G are found by asking for nodes with "left" less than 6 (G's own "left") and "right" greater than 6. Works in all databases. Greatly increases performance -- particularly when querying large hierarchies</p> </blockquote> <p><a href="http://articles.sitepoint.com/article/hierarchical-data-database/2" rel="nofollow noreferrer">Here's another post</a> going into more detail. It's written using <code>Sql</code> and <code>php</code> but I think you can get the gist of it and easily translate in Linq to Sql.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload