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

Power and Deception of CTEs Expand / Collapse
Author
Message
Posted Monday, December 1, 2008 12:42 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
kevriley (12/1/2008)
Jeff- sorry the title didn't meet expectations - I was just trying to make it more exciting!


Absolutely no problem... like I said, good article and it does stress the importance of not overlooking something not so obvious to some. The devil's in the details and your good article brought that to light. Thanks again for taking the time to write it.


--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 #611613
Posted Monday, December 1, 2008 12:43 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
Ninja's_RGR'us (12/1/2008)
Steve Jones - Editor (12/1/2008)


Jeff, it's not often someone is going to teach you something about T-SQL.


Still rolling on the floor on that one... can't be more true than that!!!


Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D


--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 #611615
Posted Monday, December 1, 2008 12:46 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
G Bryant McClellan (12/1/2008)
Jeff Moden (12/1/2008)
Good article... but, because of the title, I was actually expecting to see something about a problem or technique with CTE's that couldn't be done using other forms of code. The article is really about how an index can help any query be it a CTE, Derived Table, View, etc.


Jeff,

I was also thrown by the title although 'deception' gave me a clue.

On the other hand, thanks to Kev for pointing out what should have been obvious...the use of indexes on columns commonly used in WHERE and ORDER BY clauses. I must admit that it gave me ideas on some inherited problem code that is built with CTE for no reason I can discern. The CTE is (to me) no gain over standard SQL but potentially a gauze curtain covering up an underlying indexing issue. And I would venture that it was authored by a procedural programmer...


For me, the only gain that usually comes from a CTE (one or more) is that it makes the programming read a little better in a "Top Down" fashion instead of having to read "Bottom Up" with derived tables. Some like the ability to use recurrsive CTE's, but I've found that those are as bad or worse than cursors.


--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 #611617
Posted Monday, December 1, 2008 1:01 PM


SSC-Dedicated

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

Group: Administrators
Last Login: Today @ 12:21 PM
Points: 33,202, Visits: 15,350
I'm wary of recursive CTEs as well. And concerned that because something appears clearer, you use it instead of something better. Another thread had a discussion on a left join v exists and how one is clearer to read, but not necessarily better.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #611625
Posted Monday, December 1, 2008 1:02 PM


SSC-Dedicated

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

Group: Administrators
Last Login: Today @ 12:21 PM
Points: 33,202, Visits: 15,350
Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D


I'd bet it's more often how not to do something than the other way around.

Hopefully we'll get you up there at PASS next year doing some of the teaching.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #611626
Posted Monday, December 1, 2008 1:23 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 4:00 AM
Points: 21,397, Visits: 9,612
Steve Jones - Editor (12/1/2008)
Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D


I'd bet it's more often how not to do something than the other way around.

Hopefully we'll get you up there at PASS next year doing some of the teaching.



I second... let me go muy my tickets now!!!
Post #611644
Posted Monday, December 1, 2008 11:26 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
Ninja's_RGR'us (12/1/2008)
Steve Jones - Editor (12/1/2008)
Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D


I'd bet it's more often how not to do something than the other way around.

Hopefully we'll get you up there at PASS next year doing some of the teaching.



I second... let me go muy my tickets now!!!


Awesome compliment's guys... I'm humbled. Thanks. I'd have to think of something I haven't already beat to death on the forums, though... heh... not much left.


--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 #611831
Posted Tuesday, December 2, 2008 4:03 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 4:00 AM
Points: 21,397, Visits: 9,612
If it's worth beating to death on the forums, it means that th ecommunity needs to hear it... people wanting to hear what you have to say or needing to hear it will attend... you don't have to try to please everybody out there, it's not possible (God knows I tried that one!).
Post #611937
Posted Tuesday, December 2, 2008 4:10 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, February 24, 2010 4:10 AM
Points: 1,553, Visits: 2,232
Good point Steve, I was asking just yesterday about the left join vs NOT Exists, and well Gail gave me a very good visible answer and well ... like you say just cause it looks good doesn't mean it is good

----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley


Posting Best Practices
Numbers / Tally Tables

SQL-4-Life
Post #611940
Posted Tuesday, December 2, 2008 4:15 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:30 AM
Points: 5,344, Visits: 1,388
Nice article ...


Post #611941
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse