Power and Deception of CTEs

  • Comments posted to this topic are about the item Power and Deception of CTEs

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

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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

  • 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

  • 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

  • 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

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

  • 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!!!

  • 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

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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. 😀

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

  • 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. 😀

    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.

Viewing 15 posts - 1 through 15 (of 27 total)

You must be logged in to reply to this topic. Login to reply