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

  • Home
  • SEARCH
  • 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 561293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:26:05+00:00 2026-05-13T12:26:05+00:00

This has been bugging me for a while and I’m hoping that one of

  • 0

This has been bugging me for a while and I’m hoping that one of the SQL Server experts can shed some light on it.

The question is:

When you index a SQL Server column containing a UDT (CLR type), how does SQL Server determine what index operation to perform for a given query?

Specifically I am thinking of the hierarchyid (AKA SqlHierarchyID) type. The way Microsoft recommends that you use it – and the way I do use it – is:

  • Create an index on the hierarchyid column itself (let’s call it ID). This enables a depth-first search, so that when you write WHERE ID.IsDescendantOf(@ParentID) = 1, it can perform an index seek.

  • Create a persisted computed Level column and create an index on (Level, ID). This enables a breadth-first search, so that when you write WHERE ID.GetAncestor(1) = @ParentID, it can perform an index seek (on the second index) for this expression.

But what I don’t understand is how is this possible? It seems to violate the normal query plan rules – the calls to GetAncestor and IsDescendantOf don’t appear to be sargable, so this should result in a full index scan, but it doesn’t. Not that I am complaining, obviously, but I am trying to understand if it’s possible to replicate this functionality on my own UDTs.

Is hierarchyid simply a “magical” type that SQL Server has a special awareness of, and automatically alters the execution plan if it finds a certain combination of query elements and indexes? Or does the SqlHierarchyID CLR type simply define special attributes/methods (similar to the way IsDeterministic works for persisted computed columns) that are understood by the SQL Server engine?

I can’t seem to find any information about this. All I’ve been able to locate is a paragraph stating that the IsByteOrdered property makes things like indexes and check constraints possible by guaranteeing one unique representation per instance; while this is somewhat interesting, it doesn’t explain how SQL Server is able to perform a seek with certain instance methods.

So the question again – how do the index operations work for types like hierarchyid, and is it possible to get the same behaviour in a new UDT?

  • 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-13T12:26:05+00:00Added an answer on May 13, 2026 at 12:26 pm

    The query optimizer team is trying to handle scenarios that don’t change the order of things. For example, cast(someDateTime as date) is still sargable. I’m hoping that as time continues, they fix up a bunch of old ones, such as dateadd/datediff with a constant.

    So… handling Ancestor is effectively like using the LIKE operator with the start of a string. It doesn’t change the order, and you can still get away with stuff.

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

Sidebar

Related Questions

No related questions found

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.