SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Power and Deception of CTEs


Power and Deception of CTEs

Author
Message
kevriley
kevriley
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5542 Visits: 2646
Comments posted to this topic are about the item Power and Deception of CTEs
katesl
katesl
Say Hey Kid
Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)

Group: General Forum Members
Points: 687 Visits: 476
The CTE is the way programmers get around not having database permission to create a view. Application programmers and "architects" who think "who needs a DBA?" write queries from the point of view that the database is a box that holds the records of interest to them. The point of view of the DBA is to create views that are generally useful in the business model.

_________________
"Look, those sheep have been shorn."
data analyst replies, "On the sides that we can see.."
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)

Group: General Forum Members
Points: 382120 Visits: 43008
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 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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Roger L Reid
Roger L Reid
SSC Eights!
SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)

Group: General Forum Members
Points: 868 Visits: 166
Kevin, while well understanding that the client just wants it to work, I'm going to play devil's advocate on this and argue who's got the "procedural" mindset.

If we were talking about a relational database (vs a SQL database), there would no distinction between getting the needed values from a CTE or a "table", or a "view". In all cases, you're just asking for the variable with the values of your query.

Procedural programmers actually don't expect to get the value assigned at "declaration", but they do expect variables to hold onto values. This is reasonable.

More to the point, though, the programmer shouldn't have to think about the mechanism anyway. The programmer shouldn't be aware of a difference between a CTE or a "table" or a "view" - that's the whole point to relational databases.

The vendors however have done their best to make it look like an ISAM system, and now the so-called "object oriented" crowd has demoted it to a "persistence store" - even to the point of undoing most of the advantages of relational databases.

Solving your client's issue means dealing with the system as it exists, and you did that, and got them going. But I think you might be a little quick to attribute the flaw to "procedural thinking". It's in there, of course - but SQL and the database vendors don't make it easy to approach this right.

That's just arguing from the other side - your points are valid and well taken and it's an interesting real-life article.
roger reid

Roger L Reid
Bryant McClellan
Bryant McClellan
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1897 Visits: 550
katesl (12/1/2008)
The CTE is the way programmers get around not having database permission to create a view. Application programmers and "architects" who think "who needs a DBA?" write queries from the point of view that the database is a box that holds the records of interest to them. The point of view of the DBA is to create views that are generally useful in the business model.


Lets be careful how we define application programmers. I have been developing in SQL Server since version 4. I am acknowledged by my DBAs as having considerably more SQL application development and tuning experience than they do, primarily because my background before SQL Server was in a declarative language and because I program SQL applications daily. Be that as it may, I still consider myself an application developer, even though my language of choice is SQL rather than C#.

------------
Buy the ticket, take the ride. -- Hunter S. Thompson
Bryant McClellan
Bryant McClellan
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1897 Visits: 550
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...

------------
Buy the ticket, take the ride. -- Hunter S. Thompson
GSquared
GSquared
SSC Guru
SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)

Group: General Forum Members
Points: 106157 Visits: 9730
Good article. Most devs (database or otherwise) don't understand enough about the query optimizer and how it works with indexes, and your article has a good example of that.

- 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
Steve Jones
Steve Jones
SSC Guru
SSC Guru (250K reputation)SSC Guru (250K reputation)SSC Guru (250K reputation)SSC Guru (250K reputation)SSC Guru (250K reputation)SSC Guru (250K reputation)SSC Guru (250K reputation)SSC Guru (250K reputation)

Group: Administrators
Points: 250374 Visits: 19813
I thought it was an interesting story about how easy it is to overlook something. It's good to be reminded regularly that indexes matter and that new features, like CTEs, don't necessarily change the way T-SQL fundamentally works.

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

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)

Group: General Forum Members
Points: 117218 Visits: 9672
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!!!
kevriley
kevriley
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5542 Visits: 2646
Hi all,

thanks for all your comments, very encouraging!

Jeff- sorry the title didn't meet expectations - I was just trying to make it more exciting!

Roger - I take all your points on board too - it's always good to hear both sides of any argument.

All of my SSC contributions, whether articles, QOTDs or scripts are borne out of real-life experiences, and I just hope that they help anyone, experienced or newbie, in this strange land called SQL Server that we all play in.


Kev
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search