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


CTE - why use, and NOLOCK


CTE - why use, and NOLOCK

Author
Message
Michael_Garrison
Michael_Garrison
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1326 Visits: 2754
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.
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31992 Visits: 18551
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

GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86598 Visits: 45246
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


Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

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

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