Just like the old cartoon, I've got to say... "Ah, Magoo! You've done it again!"
Here are the run results using the same machine I used in testing for the article for a million nodes using your fine addition.
Building the initial table and SortPath...
There are 1000000 rows in dbo.Hierarchy
Cumulative Duration = 00:00:14:420
Building the Nested Sets...
1000000 rows have been updated to Nested Sets
If the rowcounts don't match, there may be orphans.
Cumulative Duration = 00:00:42:023
Building the indexes...
Cumulative Duration = 00:00:47:330
===============================================
RUN COMPLETE
===============================================
In whole numbers, that a rock solid 13% improvement. Well done, Magoo!
My only question now is, is it the "Identity Hack" that did it or is it the fact that you used a While Loop to replacce the rCTE (and we've previously proven that such loops will frequenctly beat rCtEs). I'll give it a try tomorrow.
Speaking of that, tomorrow is only a half hour a way so I need to hit the hay or the people at work are going to have to put up with a really cranky ol' DBA tomorrow. 😛 Thanks again for the code, Magoo.
--Jeff Moden
Change is inevitable... Change for the better is not.