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 Sunday, November 30, 2008 4:41 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 2:35 AM
Points: 2,716, Visits: 2,471
Comments posted to this topic are about the item Power and Deception of CTEs
Post #610853
Posted Monday, December 1, 2008 2:51 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, May 16, 2014 7:20 PM
Points: 63, Visits: 470
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.."
Post #611093
Posted Monday, December 1, 2008 5:40 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 8:38 PM
Points: 35,371, Visits: 31,912
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."

(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 #611226
Posted Monday, December 1, 2008 5:49 AM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Monday, September 9, 2013 8:32 AM
Points: 108, 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
Post #611232
Posted Monday, December 1, 2008 5:54 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 9:21 AM
Points: 847, Visits: 476
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
Post #611237
Posted Monday, December 1, 2008 5:54 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 9:21 AM
Points: 847, Visits: 476
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
Post #611238
Posted Monday, December 1, 2008 7:10 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
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
Post #611311
Posted Monday, December 1, 2008 7:58 AM


SSC-Dedicated

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

Group: Administrators
Last Login: 2 days ago @ 12:34 PM
Points: 31,181, Visits: 15,626
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
Post #611357
Posted Monday, December 1, 2008 8:12 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 4:15 AM
Points: 20,581, Visits: 9,619
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!!!
Post #611374
Posted Monday, December 1, 2008 8:57 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 2:35 AM
Points: 2,716, Visits: 2,471
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

Post #611402
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse