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

CTE - why use, and NOLOCK Expand / Collapse
Author
Message
Posted Friday, May 30, 2014 9:47 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 6:02 AM
Points: 1,116, Visits: 2,387
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.
Post #1576167
Posted Friday, May 30, 2014 11:01 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:11 AM
Points: 17,823, Visits: 15,754
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
Post #1576203
Posted Friday, May 30, 2014 11:11 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 6:44 AM
Points: 40,207, Visits: 36,616
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 2008, MVP
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

Post #1576209
Posted Friday, May 30, 2014 12:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,085, Visits: 12,551
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1576241
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse