This has distorted me a bit, and I hope one of the SQL Server experts can shed some light on it.
The question arises:
When you index a SQL Server column containing a UDT (CLR type), how does SQL Server determine which index operation to perform for a given query?
In particular, I am thinking of a hierarchyid type (AKA SqlHierarchyID ). The way Microsoft recommends using it β and the way I use it β is:
Create an index in the hierarchyid column itself (call it ID ). This allows you to search by depth, so when you write WHERE ID.IsDescendantOf(@ParentID) = 1 , it can do an index search.
Create a constant computed Level column and create an index on (Level, ID) . This allows you to do a breadth-first search, so when you write WHERE ID.GetAncestor(1) = @ParentID , it can search for the index (at the second index) for that expression.
But what I do not understand is how is this possible? . This seems to violate the usual rules of the query plan - the calls to GetAncestor and IsDescendantOf do not look impregnable, so this should lead to a full index scan, but this is not the case. Not that I complained, obviously, but I'm trying to figure out if it is possible to reproduce this functionality on my own UDTs.
Is hierarchyid just a βmagicβ type that SQL Server has a special awareness of and automatically changes the execution plan if it finds a specific combination of query elements and indexes? Or is the CLR type SqlHierarchyID just defining special attributes / methods (similar to how IsDeterministic works for constant computed columns) that are understood by the SQL Server engine?
I can not find any information about this. All I could find was a paragraph that stated that the IsByteOrdered property allows you to do things like indexes and constraint checks, guaranteeing one unique view for each instance; while this is somewhat interesting, it does not explain how SQL Server can perform searches using specific instance methods.
So again, the question is: how do index operations for hierarchyid types work, and is it possible to get the same behavior in the new UDT?
source share