When you ask for a new HierarchyID between two others, the result gets progressively longer. For example, between 2/5.6 and 2/5.7 there’s only 2/5.6.1 and other 4 component paths. The HierarchyID data type is limited to 800 some bytes, so you can’t repeat this forever. Then again, integer types are also limited, but it isn’t a problem in practice. Should I periodically defragment my table so that height doesn’t grow unbounded?
When you ask for a new HierarchyID between two others, the result gets progressively
Share
It’s considered a “best practice” with the
hierarchyidto “append” new IDs so that you don’t use those in-between states (such as/2/5.6/) at all. If yourhierarchyidis a clustered primary key then that’s bad for performance, it will cause page splits similar to the way auniqueidentifierwill.If you generate sequential children, it’s highly unlikely that you’d ever need to worry about running out; you can have literally millions of children for each parent.
Here is an example of how you’re expected to generate
hierarchyidvalues:If you generate the ids this way, rest assured you’ll never have to worry about running out.
For curiosity’s sake, I ran a quick test to find out for sure how deep you can go. Here’s a test script:
You can see it error out at a point-nesting level of 1426, so that’s your worst-case limit for how many “in-between” nodes you can create, worst case meaning that every single insertion goes in between the two most deeply-nested nodes.
As I mentioned in the comments, it’s pretty hard to hit this limit, but that still doesn’t make it a good idea to try. The actual byte length gets longer as longer as you use up more and more “points”, which degrades performance. If the
hierarchyidis your clustered index, this will kill performance by page splits. If you’re trying to rank nodes by parent then use a ranking column instead; it’s easier and more efficient to sort from a laterSELECTthan it is to do during yourINSERTwhere you have to worry about transaction isolation and other such headaches.