David Foderick's Blog - OnMaterialize()

Beware of NOLOCK hint

At many of the companies where I consult, use of the NOLOCK hint is ubiquitous.

In the normal situation , without NOLOCK, SQL Server places a Read lock on the rows that are read when you issue a SELECT statement. (These read locks are only held for the duration of the select statement.) Many teams have developed the habit of using the NOLOCK hint on all select statements. The NOLOCK hint tells SQL Server not to issue these read locks thus relieving SQL Server from having to administer these locks. The NOLOCK hint will look like this:

SELECT * FROM Customer WITH (NOLOCK) WHERE CustomerID=1

So far, so good. No Read locks, less overhead, more efficient server. Now here's the problem that I think most people do not realize: NOLOCK is equivalent to READUNCOMMITTED. That means that you are potentially reading dirty data! Read Uncommitted is the lowest isolation level and permits the connection to "read through" any exclusive locks held  by another transaction and read the other transaction's uncommitted data. If you use NOLOCK, you may be reading records that may never get committed to the database or you could be reading inconsistent data. This may or may not be appropriate for your application.

I suspect what is happening is that teams are running into blocking situations and instead of finding and curing the source of the blockage they simply put in a NOLOCK hint, unaware of its potentially dangerous behavior. If you don't care about data integrity, then continue to use NOLOCK. If you do care about the integrity of your application, find and fix the blockage at its source and don't sweep the problem under the rug with the promiscuous use of NOLOCK.

Comments

Anisha said:

Kewl you should come up with that. Excleelnt!

# May 29, 2011 7:09 AM

Honeysuckle said:

In awe of that answer! Relaly cool!

# May 30, 2011 11:17 AM

Michigan No Fault Insurance said:

advertising and *********** with Adwords. Well I’m including this RSS to my email and could look out for much extra of your respective interesting content. Make sure you update this again soon..  <a href="michigancarautoinsurance.com/">Michigan No Fault Insurance</a>

# July 29, 2011 11:30 AM

cheapest car insurance rates said:

hey there and thank you on your information – I have definitely picked up anything new from right here. I did alternatively expertise some technical points the usage of this website, since I skilled to reload the website lots of occasions previous to I may get it to load correctly. I have been brooding about if your web hosting is OK? Not that I am complaining, but slow loading cases times will often impact your placement in google and can damage your quality rating if ads and ***********  <a href="topautoinsurancerates.net/">car insurance rates</a>

# October 18, 2011 11:13 AM

buy writing paper said:

Students in the whole world remember that the persuasive essay writing service will supply them with the do my essay request. Hence, it’s easy to buy papers online.

# October 25, 2011 2:19 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)