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 «««1234»»

CTE Performance Expand / Collapse
Author
Message
Posted Friday, March 14, 2008 7:00 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
Joe, I haven't been able to work out an efficient way to use your nested sets structure on dynamic hierarchies. On relatively static data, it works great, but I have trouble with it as soon as I need to move levels around, or expand the hierarchy, or anything else other than adding to the "right side" of the hierarchy.

For example, in a managers to employees hierarchy, if a department is moved to be under a different manager, the nested sets model pretty much forces a complete rebuild of the whole hierarchy, so far as I can tell. A standard hierarchy table (ID and ParentID), allows the same update by changing a single value in a single row.

I could be missing something on this (probably am), but I just can't get my head around using the nested sets for data that requires any sort of frequent updates.

I've tested it, and it does work really, really well for selects. But it adds so much complexity and so many referential integrity issues to updates and inserts, that I just can't see how to use it.

Can you point me to something that helps in that regard?

Or is it strictly meant for static/nearly static data?


- 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 #469353
Posted Friday, March 14, 2008 2:37 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: Friday, September 26, 2014 6:54 AM
Points: 531, Visits: 432
I found your loop and CTE are not equivalent. In the loop, you have the code

WHERE ...
and c2.structureid not in
(select structureid
from @hierarchy)

which slows it down.



Post #469713
Posted Monday, March 17, 2008 9:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
peterhe (3/14/2008)
I found your loop and CTE are not equivalent. In the loop, you have the code

WHERE ...
and c2.structureid not in
(select structureid
from @hierarchy)

which slows it down.


That step is necessary in the loop, and not in the CTE. Otherwise, a repeating hierarchy level causes an error in the loop.

For example, insert into a hierarchy table:

ID 1, Parent 2
ID 2, Parent 1

or

ID 1, Parent 1

In either case, the loop without the Not In (or a left join), will cause an error as it hits the max recursion level. The CTE doesn't have that problem and doesn't need the code.

For an example of how this can happen:

Joe is a manager of a sales department
Joe is also a salesman in that department

In this case, it is necessary for Joe to be listed as the manager, since that determines his "sales manager" commission, and it is necessary for him to be listed as a salesperson, since that determines his direct sales commissions, and his own sales count for calculating his manager commission. Separating him into two entities with two separate IDs would necessitate giving him two separate paychecks, and would thus mess up income tax, Social Security, Medicare, 401(k), etc., calculations.

A loop without controls for that kind of thing will go into an infinite loop and will error out. (Yes, I know, real life isn't as neat and tidy like we'd like it to be.)

I've tested the above scenario in CTEs, and they don't error out and do return the correct hierarchy.

If you can guarantee that the hierarchy will never include recursive elements, then you don't need that step in the loop.

The loop without that step, on data pre-tested for non-recursion, is still slower than the CTE on the same data, in my tests.


- 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 #470319
Posted Thursday, June 12, 2008 6:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, March 6, 2010 11:15 AM
Points: 25, Visits: 119
help please.

I have the following situation:

I want to populate two related tables

Person (PersonID, PersonName, PersonLName)
PersonAddress(PersonAddressID, PersonID, PersonAddr, PersonCity, PersonState, PersonZip, County)

from a table which contains all the data needed

MasterList(PersonName, PersonLName, PersonAddr, PersonCity, PersonState, Personzip, County)

The thing is I am not doing this sequentially, I want to select every 7th person starting from the person in the MasterList table at position 3.

For example:
I have 100 people in the master list

Lets say that I want 30 people to be put into the Persons table and into thier associated address tables.

I want to be able to increment through the MasterList table starting at the person from position 3, and insert every 7th person thereafter into the tables.


If I get to the end of the table and I have not reached my 30 people, I need to loop back around starting at a new position - 2 and then repeat selecting every 7th person from then on out.


Important things:

1. PersonID is an identity, and an auto - incremented value, but within this loop, when I create my entry for Person, I want to turn around and create an entry for PersonAddress, using the newly created PersonID identity value as a foreign key inside of PersonAddress.

How should I write a cte to be able to acomplish this ? Thanks for any assistance provided
Post #516343
Posted Thursday, June 12, 2008 7:29 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 35,348, Visits: 31,886
This is probably the wrong place to ask for such help... and we'd really need to know which version of SQL Server you're using. It would probably be better if you started a thread in the correct forum for this.

--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #516354
Posted Tuesday, December 30, 2008 12:43 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 9:24 PM
Points: 17,807, Visits: 15,728
I have to agree with Jason Adams on this topic. This article seems to miss one of the most important aspects of DBA work - proper indexing. I currently have the need in an environment to create trees recursively 700+ levels deep and with several million nodes. To perform this using a loop takes 10-15 times longer than to use CTEs. That is using the same indexing between the two sets of coding philosophies. I have seen the rare occasion where a CTE does not outperform the loop by that much, but have yet to see a scenario where the CTE is outperformed by a loop.

BTW, the loops had been optimized numerous times to gain every ms possible out of them - introduce the CTE's and we are leaps and bounds beyond historic SQL performance levels.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #627555
Posted Tuesday, December 30, 2008 1:46 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:14 AM
Points: 9,294, Visits: 9,483
jason brimhall (12/30/2008)
I have to agree with Jason Adams on this topic. This article seems to miss one of the most important aspects of DBA work - proper indexing. I currently have the need in an environment to create trees recursively 700+ levels deep and with several million nodes. To perform this using a loop takes 10-15 times longer than to use CTEs. That is using the same indexing between the two sets of coding philosophies. I have seen the rare occasion where a CTE does not outperform the loop by that much, but have yet to see a scenario where the CTE is outperformed by a loop.

BTW, the loops had been optimized numerous times to gain every ms possible out of them - introduce the CTE's and we are leaps and bounds beyond historic SQL performance levels.

OK, NOW I'm curious: What kind of hierarchy goes that deep?


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #627615
Posted Tuesday, December 30, 2008 2:50 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 9:24 PM
Points: 17,807, Visits: 15,728
Binary Trees in the MLM industry.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #627682
Posted Tuesday, December 30, 2008 3:17 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:14 AM
Points: 9,294, Visits: 9,483
What is the MLM industry and why would they need hierarchies that deep?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #627699
Posted Tuesday, December 30, 2008 3:25 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:07 PM
Points: 7,105, Visits: 15,433
MLM=multi-level marketing.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #627702
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse