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

with nolock in sql server please advice Expand / Collapse
Author
Message
Posted Sunday, January 27, 2008 7:21 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 20, 2010 12:29 AM
Points: 56, Visits: 106



I have read lots of article about with nolock, and have seen many projects using with nolock option.

Please advice me if i am right.

I have read lots of article about with nolock, and have seen many projects using with nolock option.

Please advice me if i am right.

1.Normal select queries can use with no lock
2. Batch jobs should not use with no lock.


please guide me, am i right.

Thanks
Post #448033
Posted Sunday, January 27, 2008 10:45 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Friday, April 5, 2013 4:43 PM
Points: 1,473, Visits: 1,314
Here is my input.
I do not think we have a clear cut on your questions.
Generally speaking, when there are many users and objects are huge, we may need to use NO_LOCK. Our decision is made based on our the above factors and the torlerance of our users.

NO_LOCK may cause insistencies if we abuse it, however.
Post #448067
Posted Sunday, January 27, 2008 11:13 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 @ 8:36 AM
Points: 40,615, Visits: 37,080
No lock's not something you should be using all the time. It's for when you have concurrency issues and you don't mind the chance of inaccurate data.

Selects can be run with no lock, however all forms of data modification will take locks, even if you specify the nolock hint.



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 #448073
Posted Monday, January 28, 2008 1:31 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 30, 2014 10:08 PM
Points: 1,038, Visits: 444
And, in a really busy environment, your No Lock query could say there's table corruption when there really isn't. I cannot remember the exact cause but it's to do with the nolock allowing another query to change the linked pages, etc and then when the nolock query comes across that bit it doesn't necessarily see the change straight away.

Run your DB with nolock to see how it goes. If need be you should probably change the transaction isolation level rather than explicitly specifying locking hints.



Post #448154
Posted Monday, January 28, 2008 2:44 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 2,900, Visits: 3,301
If you are doing aggregation queries, where the consistency of the result is not critical, you should make NOLOCK your default choice. Also make NOLOCK your default choice when querying static data, as there is no need to get SQL to do more work then required. This often means that data warehouse (BI) queries are often a good candidate for NOLOCK.
If you are doing queries against operational data (e.g. checking quantity in stock before promising delivery) then you need the protection of locking, otherwise you may make the wrong business decision. This means that OLTP type queries normally do not have NOLOCK or the application uses some form of optimistic update logic to handle the integrity issues. Even with OLTP applications, static data queries can use NOLOCK.


Original author: SQL Server FineBuild 1-click install and best practice configuration of SQL Server 2014, 2012, 2008 R2, 2008 and 2005. 18 October 2014: now over 31,000 downloads.
Disclaimer: All information provided is a personal opinion that may not match reality.
Concept: "Pizza Apartheid" - the discrimination that separates those who earn enough in one day to buy a pizza if they want one, from those who can not.
Post #448168
Posted Monday, January 28, 2008 1:35 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, October 29, 2009 2:34 PM
Points: 56, Visits: 77
Be aware that you can get duplicate data back in a query using NOLOCK.

Itzik Ben Gan demoed this and you can find a code example here.

http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/16/1345.aspx

--Dave



Post #448557
Posted Tuesday, January 29, 2008 12:05 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, June 3, 2013 9:53 PM
Points: 433, Visits: 619
You've got to watch those hints and use them sparingly, in SQL 2005 there are better alternatives than NOLOCK in T-SQL. Have you looked into snapshots?

One gotcha to avoid, SQL 2008 and beyond "Specifying NOLOCK or READUNCOMMITTED in the FROM clause of an UPDATE or DELETE statement when applied to the target table of the statement." will no longer work.

Joe



Post #448701
Posted Tuesday, March 31, 2009 12:30 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 4, 2009 1:33 PM
Points: 7, Visits: 42
Does the WITH (NOLOCK) option work with sql server 2005 ?
Or is it depricated ?

I was facing some serious deadlock issues in a high transaction table, i was not bothered about inaccurate data, i added nolock to a select statement.

Even after i added nolock directive to my select, this select statement becomes a deadlock victim at times.

I do not understand why and how can this happen? ( a select with nolock is not locking anything at all, how does it participate in a dead lock situation ? )

can someone throw some light on this please ?

fyi. there could be other transactions that update the data that the select with nolock transaction is trying to select.

Thanks in adv.
appreciate your help.
Post #687320
Posted Tuesday, March 31, 2009 3:00 PM


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 @ 8:36 AM
Points: 40,615, Visits: 37,080
Santa Ana (3/31/2009)
Does the WITH (NOLOCK) option work with sql server 2005 ?

Yes

I do not understand why and how can this happen? ( a select with nolock is not locking anything at all, how does it participate in a dead lock situation ? )


You're going to have to show us the code to be sure, but I have before seen deadlocks resulting from schema locks as a result if enabling and disabling triggers and even selects with a nolock have to respect a Sch-M lock.



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 #687485
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse