title: Optimized SQL for tree structures
body: <p>How would you get tree-structured data from a database with the best performance? For example, say you have a folder-hierarchy in a database. Where the folder-database-row has <strong>ID</strong>, <strong>Name</strong> and <strong>ParentID</strong> columns.</p>
  
  <p>Would you use a special algorithm to get all the data at once, minimizing the amount of database-calls and process it in code?</p>
  
  <p>Or would you use do many calls to the database and sort of get the structure done from the database directly?</p>
  
  <p>Maybe there are different answers based on x amount of database-rows, hierarchy-depth or whatever?</p>
  
  <p><strong>Edit</strong>: I use Microsoft SQL Server, but answers out of other perspectives are interesting too.</p>