Which is better in SQL Server: Recursion or Temp tables?
I have a little dilemma. For nGallery, we want to update it so the “picture count” returns the number of pictures in the album and all of its subalbums, not just in that one album. One thing that complicates it is that you can have subsubalbums, or even subsubsubalbums. Do there is no quick and easy way to “select count(*) from albums where it is this album or is child of this album”.
There are two ways I could do this... with a recursive stored procedure or a stored procedure that uses a temporary table. Which does SQL Server handle faster though? Well, shouldn't say faster... faster != better, necessarily. The recursive function would just parse the album “tree” (get to think back to my fun binary tree exercises in Data Structures classes! seriously, loved that class). The one that uses a temporary table would just loop adding the IDs of albums under the main one until none are left, then just do a “select count(*) from pictures where albumID in (select id from #tmp)”.
I've always thought temporary tables was slow because it had a lot of overhead, but I also tend to think of recursion as a little bit riskier... plus, it could have its own memory overhead, and I don't know if SQL Server has a stack limit.