hunchback (9/23/2013)
I changed [path] by order_val in the recursive part and now it should run flawless.A recursive CTE is still an iterative approach so I do not hold my breath hoping for better performance.
You can add a clustered index by ID and a nonclustered by IdRoot.
Well said. Oddly enough, a well constructed WHILE loop that iterates over the Levels of the menu in a manner similar to the rCTE would probably do as well. In either case, the recursion is necessary and isn't as bad as an rCTE that counts because the rCTE method that we both used (and a similar WHILE loop would use) processes an entire set (all nodes at a given level) instead of just individual rows.
--Jeff Moden
Change is inevitable... Change for the better is not.