CTE - why use, and NOLOCK

  • I work for a major transportation company and I write SSRS reports. We use MS SQL 2005 (I know old) but that's another thing. And I have been doing this for a few years now. I have been reading the fourms a while too and I have 2 comments or questions.

    1. Why use CTE? I see a number of times when people answer others questions that they use CTE's. I understand for recursive code issues. But for most other code is it not better to use CREATE TABLE and then actually define the data types? Is this not better for the SQL engine to know what the table is then having a CTE and not knowing what data types it uses?

    2. Yes, I know by now NOLOCK is bad. (I have seen all you guys say that, the top people in these forums, you know who you are.) But here we are forced to use it. I am told that without it, it caused many issues with our transactions tables. Our tables get updated 100's of times per hour. I know this is being deprecated. And I am not a database admin. So I am not sure what to tell management with this issue.

    Anyway what are your thoughts.

  • ON the topic of the NOLOCK issue, the most common reason people use it is because they have poor performance. The database design could be part of that, the code could be part of that, the indexes could be part of that.

    Rather than implement the time to fix some of those performance issues, they use NOLOCK to bandaid the issue. Often times NOLOCK creates an illusion of the infamous "turbo" button. Fixing the code, indexes or DB design could often alleviate those problems.

    A secondary issue seems that your OLTP database is also serving up your reporting/warehouse requirements. This is another common reason why NOLOCK gets implemented. Even in this situation, performance tuning of the code, indexes, database et al would help to alleviate much of the performance issues and thus eliminate most reasons for using NOLOCK.

    As for the CTEs, it is something that should be tested cases by case. Sometimes it makes more sense and provides better performance to use a temp table and sometimes it is better to use a cte.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Michael_Garrison (5/30/2014)


    1. Why use CTE? I see a number of times when people answer others questions that they use CTE's. I understand for recursive code issues. But for most other code is it not better to use CREATE TABLE and then actually define the data types? Is this not better for the SQL engine to know what the table is then having a CTE and not knowing what data types it uses?

    A CTE is nothing more than a named subquery. It is not a table. There's no persistent storage. When you run the query, the performance will be just the same as if you'd used a normal subquery.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Michael_Garrison (5/30/2014)


    2. Yes, I know by now NOLOCK is bad. (I have seen all you guys say that, the top people in these forums, you know who you are.) But here we are forced to use it. I am told that without it, it caused many issues with our transactions tables. Our tables get updated 100's of times per hour. I know this is being deprecated. And I am not a database admin. So I am not sure what to tell management with this issue.

    It somewhat boils down to the reasoning behind why this hint has been mandated. If this is reporting and pinpoint accuracy is not important, then it possibly ok. I would prefer to move the data away from the OLTP database to a warehouse so you can do stuff with the data without impacting the production environment of the application. I would prefer to use read uncommitted instead of individual hints but the result is the same.

    If you are using NOLOCK in your production environment this is a much more serious issue. Making decisions for an application (especially if it involves finances or money) is a very bad place to implement NOLOCK. Worse yet is when management makes a blanket mandate that all queries use that hint. This is a VERY bad situation and one that needs to be addressed sooner rather than later.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 4 posts - 1 through 3 (of 3 total)

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