Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 796303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:37:10+00:00 2026-05-14T22:37:10+00:00

When you ask for a new HierarchyID between two others, the result gets progressively

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:37:10+00:00Added an answer on May 14, 2026 at 10:37 pm

    It’s considered a “best practice” with the hierarchyid to “append” new IDs so that you don’t use those in-between states (such as /2/5.6/) at all. If your hierarchyid is a clustered primary key then that’s bad for performance, it will cause page splits similar to the way a uniqueidentifier will.

    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 hierarchyid values:

    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    BEGIN TRANSACTION 
        UPDATE Hierarchy
        SET @LastChild = LastChild = HId.GetDescendant(LastChild, NULL)
        WHERE HId = @ParentID
    
        INSERT Hierarchy (HId, ...)
        VALUES (@LastChild, ...)
    COMMIT
    

    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:

    DECLARE
        @parent hierarchyid,
        @child hierarchyid,
        @high hierarchyid,
        @cnt int
    
    SET @parent = '/1/'
    SET @child = @parent.GetDescendant(NULL, NULL)
    SET @cnt = 0
    
    WHILE (@@ERROR = 0)
    BEGIN
        SET @cnt = @cnt + 1
        PRINT CAST(@cnt AS varchar(10)) + ': ' + @child.ToString()
        SET @high = @parent.GetDescendant(@child, @high)
        SET @child = @parent.GetDescendant(@child, @high)
    END
    

    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 hierarchyid is 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 later SELECT than it is to do during your INSERT where you have to worry about transaction isolation and other such headaches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still new to the API and I wanted to ask: Can you send
And why don't they change it? Edit: The reason ask is because I'm new
I shudder to ask, but my client might offer no other SQL (or SQL-like)
I ask this because at work I am supposed to develop a web-application that
I ask this question in anticipation as part of a project. I have experience
need ask you about some help. I have web app running in Net 2.0.
I ask because I am sending a byte stream from a C process to
I ask this, because tomorrow is my first meeting with the client, in which
I ask this question in the hope of collecting all the incompatible changes/bugfixes in
The reason I ask is that Stack Overflow has been Slashdotted , and Redditted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.