updating nodes in a nested set model | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

updating nodes in a nested set model

We have a nested set L and R design in our database.
The design allows multiple instances of nodes in the hierarchy. Each node has a combination of node name and its instance id as the primary key. We also maintain a unique_qty column that has the unique number of nodes below a particular node. This unique qty basically ignores the multiple instances of nodes below it and counts only the distinct node names(ignoring their instance ids).
the problem that im facing is…how do i update the unique_qty when i perform any move in the tree.
UPDATE Hierarchy
SET unique_qty = ( SELECT COUNT(DISTINCT node_name) FROM Hierarchy AS H2 WHERE H2.L > Hierarchy.L AND H2.R < Hierarchy.R ) I am using the above query to find out the unique_qty when i initially populate the table.
My question is.. when i make a move of a subtree within the hierarchy, then i need to update this unique_qty for the source parents and the destination parents of the subtree( and the unique_qty for eah node in the subtree being moved remains the same ).
I had two ideas on how to update the parent nodes of the subtree: 1) for each node in the path to the root in the hierarchy, from the parent nodes( both source side and destination side) recalculate the unique_qty 2) for each node in the subtree find out until what level in the path to the root, we need to update the unique_qty, and then update only those unique_qtys any suggestions on my methods? which one is better? any more ideas on how to do this??
You know that Joe Celko has written a whole book on this stuff? [<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]<br /><br />–<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
]]>