January 9, 2014 at 3:31 am
Hi All,
can any one clarify the difference between NOLOCK and WITH(NOLOCK) in sql server 2008 ?
which one is the best practice to use ?
Thanks
Bhanu
January 9, 2014 at 3:51 am
kbhanu15 (1/9/2014)
Hi All,can any one clarify the difference between NOLOCK and WITH(NOLOCK) in sql server 2008 ?
Thanks
Bhanu
There is no diference between NOLOCK & WITH (NOLOCK). They both execute the query in "Read Uncommited" Isolation level.
which one is the best practice to use ?
No one, as they both may provide the result set with dirty reads.
January 9, 2014 at 3:57 am
The first is deprecated, the second is not, both can result in incorrect results, duplicate rows and missing rows, so neither is the best to use. Best is to write the SQL properly without hints.
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
January 9, 2014 at 4:01 am
Thanks for you support.
it is mandatory in my query ti use(NOLOCK or WITH(NOLOCK), so i can prefer to use WITH (NOLOCK).
it is your conclusion.
January 9, 2014 at 4:25 am
Why is a hint that will sooner or later result in incorrect results 'mandatory'? Do the people you're writing that query for realise that from time to time their results will be incorrect?
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
January 9, 2014 at 7:38 am
NOLOCK is not a good habit at all. In fact query hints in general are a last resort and should only be used when you REALLY understand what they are doing. If you are unsure, avoid them.
Before you claim that NOLOCK is mandatory for your query you need to fully understand what that hint does and the serious issues it can and will bring to your system. Here are 3 very excellent articles describing what happens when you use that hint.
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/10/1280.aspx
http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx
http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/[/url]
_______________________________________________________________
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/
January 10, 2014 at 9:55 am
We have some semi-static reporting databases that run long queries. Some of them use nolock to avoid one report blocking another. In this case, the data was refreshed over night from production, and is static during the day. There is not really a concern about users getting up to the minute accurate information. Does this qualify as an acceptable use ?
January 10, 2014 at 10:19 am
No, it's an unnecessary use. If the data is static during the day then there aren't going to be any X locks around to block the reports and so adding nolock is a waste of typing
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
January 10, 2014 at 10:44 am
kbhanu15 (1/9/2014)
Hi All,can any one clarify the difference between NOLOCK and WITH(NOLOCK) in sql server 2008 ?
which one is the best practice to use ?
I've been in the situation where nolock use was mandatory. I'm pretty sure that the current supported syntax is WITH (NOLOCK).
Make sure you realise what actually happens and the ramifications of using it. In particular, knowledge of isolation modes is highly recommended when programming databases, I like the wikipedia articles on it. Nolock looks to be equal to read uncommitted.
January 10, 2014 at 1:29 pm
GilaMonster (1/10/2014)
No, it's an unnecessary use. If the data is static during the day then there aren't going to be any X locks around to block the reports and so adding nolock is a waste of typing
So am I mis-remembering situations in which long running read-only queries have caused blocking of other read-only queries ?
January 10, 2014 at 2:23 pm
In read-commited isolation level, readers don't block readers.
January 10, 2014 at 2:44 pm
homebrew01 (1/10/2014)
GilaMonster (1/10/2014)
No, it's an unnecessary use. If the data is static during the day then there aren't going to be any X locks around to block the reports and so adding nolock is a waste of typingSo am I mis-remembering situations in which long running read-only queries have caused blocking of other read-only queries ?
Read queries take shared locks (unless someone's been messing with locking hints). Shared locks never block other shared locks.
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
January 10, 2014 at 3:19 pm
homebrew01 (1/10/2014)
We have some semi-static reporting databases that run long queries. Some of them use nolock to avoid one report blocking another. In this case, the data was refreshed over night from production, and is static during the day. There is not really a concern about users getting up to the minute accurate information. Does this qualify as an acceptable use ?
The only thing you're doing here is reducing locking lag/escalation requirements, but they can't block each other.
TABLOCK would be equivalent, and safer, in a read only environment. Single lock opened, tiny overhead.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
January 13, 2014 at 10:19 am
kbhanu15 (1/9/2014)
Hi All,can any one clarify the difference between NOLOCK and WITH(NOLOCK) in sql server 2008 ?
which one is the best practice to use ?
Thanks
Bhanu
I think everyone did a good job explaining why you would want to avoid using read-uncommitted (AKA NOLOCK). That said, if NOLOCK is required (and getting the correct results when you run a query is not required) then I recommend always using: WITH (NOLOCK). Some things break when you omit the WITH keyword when using NOLOCK.
For example: this query would fail:
SELECT t1.<column>, t2.<column>
FROM <db>.<schema>.<table> t1
JOIN <linked server>.<db>.<schema>.<table> t2 (NOLOCK)
This would not fail:
SELECT t1.<column>, t2.<column>
FROM <db>.<schema>.<table> t1
JOIN <linked server>.<db>.<schema>.<table> t2 WITH (NOLOCK)
-- Itzik Ben-Gan 2001
January 13, 2014 at 12:12 pm
Alan.B (1/13/2014)
kbhanu15 (1/9/2014)
Hi All,can any one clarify the difference between NOLOCK and WITH(NOLOCK) in sql server 2008 ?
which one is the best practice to use ?
Thanks
Bhanu
I think everyone did a good job explaining why you would want to avoid using read-uncommitted (AKA NOLOCK). That said, if NOLOCK is required (and getting the correct results when you run a query is not required) then I recommend always using: WITH (NOLOCK). Some things break when you omit the WITH keyword when using NOLOCK.
For example: this query would fail:
SELECT t1.<column>, t2.<column>
FROM <db>.<schema>.<table> t1
JOIN <linked server>.<db>.<schema>.<table> t2 (NOLOCK)
This would not fail:
SELECT t1.<column>, t2.<column>
FROM <db>.<schema>.<table> t1
JOIN <linked server>.<db>.<schema>.<table> t2 WITH (NOLOCK)
I will ignore your comment about suggesting that always using NOLOCK is a good idea when integrity of the results is not important. :w00t:
The reason omitting the keyword WITH when defining a query hint is because leaving it off has been deprecated. You should ALWAYS use proper syntax in any t-sql statement and omitting WITH for a query hint is not correct syntax.
http://technet.microsoft.com/en-us/library/ms143729.aspx
To see the line in reference just search for "Specifying table hints without using the WITH keyword."
_______________________________________________________________
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 15 posts - 1 through 15 (of 21 total)
You must be logged in to reply to this topic. Login to reply