Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»

Hierarchies in SQL Expand / Collapse
Author
Message
Posted Friday, December 10, 2010 12:30 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
Craig Farrell (11/19/2010)
Hey Gus,

Late to the party, I know, but you referenced this in another thread and I had a curiousity. Could you discuss the 'lazy updater' component a bit more. I'm not necessarily sure I understand how the 'temp range' vs. the 'real range' helps with update issues, since you're still locking and unlocking rows/pages/table using either column.


The idea is to use Snapshot Isolation. That will allow the asynchronous update to complete without wrecking performance while it runs (which was a flaw in my original implementation). This could also be used in a purely nested sets hierarchy, so that infrequent updates don't interfere with concurrent reads. That's what I was refering to in that other thread.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1033183
Posted Friday, December 10, 2010 12:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
autoexcrement (12/10/2010)
Fascinating article! I have a couple of naive questions about the following bit of G code:

       Adjacency (NodeID, ParentID) as -- Adjacency Query
(select 0, ID, ParentID
from dbo.HierarchyHybrid
where ID = @NodeID_in
andexists
(select*
from dbo.HierarchyHybrid h2
where h2.TopParentID = HierarchyHybrid.TopParentID
and RangeStart is null)

1. Is it supposed to have the "select 0, " or is that just a typo? It looks like the surrounding code is expecting 2 values there, not 3. If not a typo, I don't understand the "0" part.

2. Why doesn't this work?
       Adjacency (NodeID, ParentID) as -- Adjacency Query
(select 0, ID, ParentID
from dbo.HierarchyHybrid
where ID = @NodeID_in
and RangeStart is null)

Thanks, regardless of whether you have time to reply.


The 0 is meant to indicate the row that was from the original parameter value. This comes in handy if you are querying up and down the hierarchy and need to know which one you originally asked for.

The "and exists" subquery merely tests that the row requested is in a hierarchy that has a top level. The version you wrote tests that it IS the top level. There's a difference.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1033185
Posted Friday, December 10, 2010 12:38 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
Zeev Kazhdan (12/10/2010)
Interesting article, but I have a question, which hopefully will save me a lot of hours:
In Oracle 10 I do it in one select statement, using START WITH and CONNECT BY.
Now, migrating one of my customers to SQL 2008, is there anything close to it in SQL Server?

Thanks in advance


The HierarchyID data type has a lot of that kind of functionality. I built this solution in SQL 2000, and then in SQL 2005, where that wasn't available.

I've found that nested sets hierarchies still perform much better (in selecting) than the hierarchy ID data type does. Updates are still fastest in adjacency hierarchies, since they usually only involve one row. Deletion speed is fastest in nested sets, second fastest in adjacency, and slowest in hierarchy ID. Additions are fastest in either adjacency, or in a padded nested sets hierarchy (equally fast in either), except in cases where the nested sets version requires resizing or moving any ranges, and are slowest in a hierarchy ID, except when adding one row to the bottom of a chain.

So, even in SQL 2008, consider a few things before just going with the hierarchy ID data type, instead of nested sets (which is best if you can get past the issues with frequent changes), or a hybrid system like this. Adjacency should only be used if you absolutely have to. It's a performance killer except on very small data sets or very shallow hierarchies. (It's hard to argue against it on a family tree, for example, while you're building it. But for just about anything else I've run into, one of the others will be better.)


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1033187
Posted Saturday, December 11, 2010 10:51 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 2:46 PM
Points: 542, Visits: 806
Thank you for the article! We are getting started on a project requiring us to maintain hierarchies and I found the information here very useful. What's missing though is the ability to track hierarchies and their changes over time. What if I need to know Parent-Child relationships as they were at any point in time? Any recommendations?

Thank you!



Post #1033335
Posted Saturday, December 11, 2010 11:48 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 3:19 PM
Points: 157, Visits: 608
What if I need to know Parent-Child relationships as they were at any point in time?


Could you just use change data capture, or any traditional type of trigger-based auditing to track changes to your tables as needed?



"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Post #1033353
Posted Monday, December 13, 2010 6:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
mishaluba (12/11/2010)
Thank you for the article! We are getting started on a project requiring us to maintain hierarchies and I found the information here very useful. What's missing though is the ability to track hierarchies and their changes over time. What if I need to know Parent-Child relationships as they were at any point in time? Any recommendations?

Thank you!


My other main article for SQL Server Central is on audit trails and logging. It's at http://www.sqlservercentral.com/articles/Auditing/63247/, with part 2 at http://www.sqlservercentral.com/articles/Auditing/63248/.

autoexcrement (12/11/2010)
Could you just use change data capture, or any traditional type of trigger-based auditing to track changes to your tables as needed?


Keep in mind that, with change-data-capture, certain types of table DDL will be blocked, in the same manner as when you have replication on a table. Triggers or passive logging don't have that issue, if they're set up correctly.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1033765
Posted Tuesday, December 14, 2010 1:17 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 2:46 PM
Points: 542, Visits: 806
Thank you for the responses, but I was thinking more along the lines of slowly-chaning dimension, rather then audit/logging. In one of the earlier comments Jeff Storm showed a portion of his solution, which is closer to what I am after. I would like for the application to be able to "look" at the same data using different hierarchy structures (Hierarchy A, which existed at time X and Hierarchy B, which existed at time Y) and all of them have to be equally accessible in the same set of tables.

Thank you!



Post #1034684
Posted Tuesday, December 14, 2010 1:27 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 3:19 PM
Points: 157, Visits: 608
I'm the first to admit that I'm naive and uninformed, but it sounds to me as if it's basically an issue of you:

1) deciding what data you need to store, including any "related" data you may need
2) deciding what DML events should trigger your storing the old/previous values

From there it's fairly straightforward to decide on a method to copy the old data into a separate table/column/etc. No?

I'm probably missing something.



"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Post #1034690
Posted Tuesday, December 14, 2010 2:11 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
mishaluba (12/14/2010)
Thank you for the responses, but I was thinking more along the lines of slowly-chaning dimension, rather then audit/logging. In one of the earlier comments Jeff Storm showed a portion of his solution, which is closer to what I am after. I would like for the application to be able to "look" at the same data using different hierarchy structures (Hierarchy A, which existed at time X and Hierarchy B, which existed at time Y) and all of them have to be equally accessible in the same set of tables.

Thank you!


The usual solution for that is adding effective dates to the table, and using those in your queries.

That often has a significant impact on performance, and always makes coding a bit more complex, but it does work.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1034748
Posted Thursday, January 13, 2011 10:02 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, December 16, 2013 10:42 AM
Points: 96, Visits: 434
mishaluba (12/14/2010)
Thank you for the responses, but I was thinking more along the lines of slowly-chaning dimension, rather then audit/logging. In one of the earlier comments Jeff Storm showed a portion of his solution, which is closer to what I am after. I would like for the application to be able to "look" at the same data using different hierarchy structures (Hierarchy A, which existed at time X and Hierarchy B, which existed at time Y) and all of them have to be equally accessible in the same set of tables.

Thank you!


I actually just got done building this exact solution yesterday. I have a parent child relationship in our customer (accounts) table, and it's using SCD. I wish I had time to explain how I solved this. ...

It's going to have to wait until I can make a blog on it. Maybe I can do that this weekend. Right now I don't have time to explain it.

I'll try and post back here if I can blog on it this weekend...


-------------------------------------------------------------------------------------------------
My SQL Server Blog
Post #1047419
« Prev Topic | Next Topic »

Add to briefcase «««12345»»

Permissions Expand / Collapse