Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Hierarchy Structure SQL Query Rate Topic Display Mode Topic Options
Author
 Message
 Posted Saturday, May 26, 2012 1:50 PM
 SSCrazy Eights Group: General Forum Members Last Login: Tuesday, October 25, 2016 7:17 AM Points: 9,298, Visits: 9,517
 Jeff Moden (5/24/2012)dwain.c (5/24/2012)Jeff Moden (10/24/2010)For the simple task of sorting in the correct order, we'll use the "Hierarchical Path" method. To build it, we'll use a "Recursive CTE". Look that up in Books Online for a better understanding of them. I will tell you right now that they are NOT a panacea. Hierarchies are one of the few places where I don't mind the use of recursive CTE's because they're normally RBAR code. In the example that follows, the recursive CTE is "layered" or (as Celko calls it), "lasagna" code. It processes a full set (one full level) of information for each iteration which makes it non-RBAR code. It's still a bit slower than a more lengthy method but it takes a huge hierarchy to really appreciate the difference so the recursive CTE is usually good enough.Oh my! No fair! I love posting recursive CTE solutions and you beat me to it.Only by a year and eight months. (Just having a little fun here).Shifting gears, Dwaine, all is not lost here. If you could figure out a way to make this kind of "lasagne" recursive CTE faster, I know a lot of people (especially me) that would greatly appreciate it. The only other way that I know to build the Hierarchical path is with a slightly faster While Loop and even though it's not a RBAR While Loop, it still doesn't sit so well with me that I have to use one here.Jeff, could you post this as a challenge, along with a test set? I'd love to take a whack at it. -- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoungProactive Performance Solutions, Inc. "Performance is our middle name."
Post #1307002
 Posted Saturday, May 26, 2012 2:06 PM
 SSC-Forever Group: General Forum Members Last Login: Today @ 10:52 PM Points: 42,083, Visits: 39,479
 You mean just as a separate post or here or did you have something else in mind, Barry? --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." Helpful Links:How to post code problemsHow to post performance problems
Post #1307003
 Posted Saturday, May 26, 2012 3:18 PM
 SSCrazy Eights Group: General Forum Members Last Login: Tuesday, October 25, 2016 7:17 AM Points: 9,298, Visits: 9,517
 Jeff Moden (5/26/2012)You mean just as a separate post or here or did you have something else in mind, Barry?Right, a separate post, with a data set (million rows, of course) and a formal statement of the challenge ("fastest execution time to produce result set like this ...".I actually have a fair start on the data set... -- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoungProactive Performance Solutions, Inc. "Performance is our middle name."
Post #1307005
 Posted Sunday, May 27, 2012 8:49 AM
 SSC-Forever Group: General Forum Members Last Login: Today @ 10:52 PM Points: 42,083, Visits: 39,479
 RBarryYoung (5/26/2012)Jeff Moden (5/26/2012)You mean just as a separate post or here or did you have something else in mind, Barry?Right, a separate post, with a data set (million rows, of course) and a formal statement of the challenge ("fastest execution time to produce result set like this ...".I actually have a fair start on the data set...Got it. I'll see what i can do. Thanks, Barry. --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." Helpful Links:How to post code problemsHow to post performance problems
Post #1307072
 Posted Monday, June 30, 2014 4:37 AM
 Forum Newbie Group: General Forum Members Last Login: Sunday, December 21, 2014 11:09 AM Points: 3, Visits: 2
Post #1587564
 Posted Monday, June 30, 2014 4:39 AM
 Forum Newbie Group: General Forum Members Last Login: Sunday, December 21, 2014 11:09 AM Points: 3, Visits: 2
 DECLARE c CURSOR READ_ONLY FAST_FORWARD FORSELECT Id FROM Tree_Table WHERE Id NOT IN (SELECT DISTINCT ParentId FROM Tree_Table WHERE ParentId IS NOT NULL) DECLARE @IdPk INTOPEN c FETCH NEXT FROM c INTO @IdPk WHILE (@@FETCH_STATUS = 0) BEGIN DECLARE @NodeValue VARCHAR(MAX) SET @NodeValue = '' WHILE(EXISTS(SELECT * FROM Tree_Table WHERE Id=@IdPk)) BEGIN SELECT @NodeValue=Name+'/'+@NodeValue, @IdPk=ParentId FROM Tree_Table WHERE id=@IdPk END PRINT 'Parent to Leaf: ' + LEFT(@NodeValue,LEN(@NodeValue)-1) FETCH NEXT FROM c INTO @IdPk ENDCLOSE cDEALLOCATE c To get hierarchy and tree view data
Post #1587567
 Posted Monday, June 30, 2014 5:35 PM
 SSC-Forever Group: General Forum Members Last Login: Today @ 10:52 PM Points: 42,083, Visits: 39,479
 tafinami (6/30/2014)DECLARE c CURSOR READ_ONLY FAST_FORWARD FORSELECT Id FROM Tree_Table WHERE Id NOT IN (SELECT DISTINCT ParentId FROM Tree_Table WHERE ParentId IS NOT NULL) DECLARE @IdPk INTOPEN c FETCH NEXT FROM c INTO @IdPk WHILE (@@FETCH_STATUS = 0) BEGIN DECLARE @NodeValue VARCHAR(MAX) SET @NodeValue = '' WHILE(EXISTS(SELECT * FROM Tree_Table WHERE Id=@IdPk)) BEGIN SELECT @NodeValue=Name+'/'+@NodeValue, @IdPk=ParentId FROM Tree_Table WHERE id=@IdPk END PRINT 'Parent to Leaf: ' + LEFT(@NodeValue,LEN(@NodeValue)-1) FETCH NEXT FROM c INTO @IdPk ENDCLOSE cDEALLOCATE c To get hierarchy and tree view data In this case, a cursor isn't the worst thing in the world. However, take a look at the following articles...http://www.sqlservercentral.com/articles/Hierarchy/94040/http://www.sqlservercentral.com/articles/T-SQL/94570/ --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." Helpful Links:How to post code problemsHow to post performance problems
Post #1587822
 Posted Monday, June 30, 2014 5:38 PM
 SSC-Forever Group: General Forum Members Last Login: Today @ 10:52 PM Points: 42,083, Visits: 39,479
 Jeff Moden (5/27/2012)RBarryYoung (5/26/2012)Jeff Moden (5/26/2012)You mean just as a separate post or here or did you have something else in mind, Barry?Right, a separate post, with a data set (million rows, of course) and a formal statement of the challenge ("fastest execution time to produce result set like this ...".I actually have a fair start on the data set...Got it. I'll see what i can do. Thanks, Barry.Wow... talking about a forgotten promise... my apologies, Barry.That, notwithstanding, please see the following article which also has a million row "clean" hierarchy generator.http://www.sqlservercentral.com/articles/Hierarchy/94040/This next one is part of the same series (2 articles in the series) and provides an alternate to all of the currently known hierarchical structures.http://www.sqlservercentral.com/articles/T-SQL/94570/ --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." Helpful Links:How to post code problemsHow to post performance problems
Post #1587823
 Posted Monday, June 30, 2014 6:20 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 Jeff Moden (6/30/2014)tafinami (6/30/2014)DECLARE c CURSOR READ_ONLY FAST_FORWARD FORSELECT Id FROM Tree_Table WHERE Id NOT IN (SELECT DISTINCT ParentId FROM Tree_Table WHERE ParentId IS NOT NULL) DECLARE @IdPk INTOPEN c FETCH NEXT FROM c INTO @IdPk WHILE (@@FETCH_STATUS = 0) BEGIN DECLARE @NodeValue VARCHAR(MAX) SET @NodeValue = '' WHILE(EXISTS(SELECT * FROM Tree_Table WHERE Id=@IdPk)) BEGIN SELECT @NodeValue=Name+'/'+@NodeValue, @IdPk=ParentId FROM Tree_Table WHERE id=@IdPk END PRINT 'Parent to Leaf: ' + LEFT(@NodeValue,LEN(@NodeValue)-1) FETCH NEXT FROM c INTO @IdPk ENDCLOSE cDEALLOCATE c To get hierarchy and tree view data In this case, a cursor isn't the worst thing in the world. However, take a look at the following articles...http://www.sqlservercentral.com/articles/Hierarchy/94040/http://www.sqlservercentral.com/articles/T-SQL/94570/Or this one, if Jeff's wonderful solutions on steroids overwhelm you:The Performance of Traversing a SQL Hierarchy My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1587832

 Permissions

 Copyright © 2002-2016 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.