473,756 Members | 6,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to use the NOLOCK hint.

Background:
I am currently working on a mission critical web based application
that is accessed 24 hours a day by users from just about every time
zone. We use MS SQL Server as our database and we have lots of
problems with time-outs. We used to have lots of problems with locks
until my management decided that we would use the WITH (NOLOCK) hint
on EVERY select statement and WITH (ROWLOCK) on EVERY update
statement. I have argued since the beginning that the NOLOCK hint
should be the exception and not the rule. Meanwhile we continue to
have problems related to time-outs.

Problem:
I'm the one that they call when there are time-out errors.

I am a programmer first and a DBA when I have to be. I'd really like
to hear from some of you who are the opposite. I realize that there
are many factors that contribute to slow response from a database
server (indexes, RAM, disk speed, etc.), but what I really need to
hear from an expert is whether or not using NOLOCK on **EVERY** query
in a 30GB database that has 344 tables is a bad idea.

Thanks in advance,

Stephen McMahon
sm******@gmail. com
Jul 20 '05 #1
2 25437
Stephen,

With NOLOCK hint, the transaction isolation level for the SELECT statement
is READ UNCOMMITTED. This means that the query may see dirty and
inconsistent data. So in general this is not a good idea to apply as a
rule. Even if this dirty read behavior is OK for your mission critical web
based application, NOLOCK scan can cause 601 error which will terminate the
query due to data movement as a result of lack of locking protection.

--
Gang He
Software Design Engineer
Microsoft SQL Server Storage Engine

This posting is provided "AS IS" with no warranties, and confers no rights.
"Stephen McMahon" <sl*******@gmai l.com> wrote in message
news:ab******** *************** **@posting.goog le.com...
Background:
I am currently working on a mission critical web based application
that is accessed 24 hours a day by users from just about every time
zone. We use MS SQL Server as our database and we have lots of
problems with time-outs. We used to have lots of problems with locks
until my management decided that we would use the WITH (NOLOCK) hint
on EVERY select statement and WITH (ROWLOCK) on EVERY update
statement. I have argued since the beginning that the NOLOCK hint
should be the exception and not the rule. Meanwhile we continue to
have problems related to time-outs.

Problem:
I'm the one that they call when there are time-out errors.

I am a programmer first and a DBA when I have to be. I'd really like
to hear from some of you who are the opposite. I realize that there
are many factors that contribute to slow response from a database
server (indexes, RAM, disk speed, etc.), but what I really need to
hear from an expert is whether or not using NOLOCK on **EVERY** query
in a 30GB database that has 344 tables is a bad idea.

Thanks in advance,

Stephen McMahon
sm******@gmail. com

Jul 20 '05 #2
Stephen McMahon (sl*******@gmai l.com) writes:
I am currently working on a mission critical web based application
that is accessed 24 hours a day by users from just about every time
zone. We use MS SQL Server as our database and we have lots of
problems with time-outs. We used to have lots of problems with locks
until my management decided that we would use the WITH (NOLOCK) hint
on EVERY select statement and WITH (ROWLOCK) on EVERY update
statement. I have argued since the beginning that the NOLOCK hint
should be the exception and not the rule. Meanwhile we continue to
have problems related to time-outs.

Problem:
I'm the one that they call when there are time-out errors.

I am a programmer first and a DBA when I have to be. I'd really like
to hear from some of you who are the opposite. I realize that there
are many factors that contribute to slow response from a database
server (indexes, RAM, disk speed, etc.), but what I really need to
hear from an expert is whether or not using NOLOCK on **EVERY** query
in a 30GB database that has 344 tables is a bad idea.


Always using NOLOCK does certainly come with some risks. But what the
risks are depends on how the system is coded. And what purpose the
system has.

But say that you system maintain accounts, and there is a process that
performs some reallocation of assets, and it happens that new assets
are inserted before old are deleted. All this happens in one transaction.
Now we have a stored procedure that is to present the total value of
that account, which is called, because the account owner logs through
the web to look at his account. The procedure runs with NOLOCK, and the
account owner now finds that his assets has doubled. (And it is really
better if old are deleted first. The account owner now finds that he
has nothing!)

For a system which is more OLAP than OLTP, it may be more permissible to
use NOLOCK, since the users may be looking at overall trends and not
individual data points.

Then there are other ugly things that can happen with NOLOCK if you
come in the middle of a page split, or a page being moved because
a colunm in the clustered index has been updated. That can lead to
the NOLOCK query failing with a seemingly serious error.

To take this path requires very thorough knowledge of the ramifications.
What yuo really should do is trace slow queries, improve indexing etc.
If you have bad query plans, you still have bad query plans with NOLOCK.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
19123
by: AKS | last post by:
I am getting lot of deadlocks in my application. As it is very complex ti avoid deadlocks at this stage of application we have done few steps to lessen the impact. We have added retries after deadlock is capturted. We have added select * from TABLE with (nolock) wherever possible. But interestingly second step is not working. I have few simple select statements where i am using nolock criteria still I am getting deadlock victim error....
6
7013
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: http://groups-beta.google.com/group/alt.comp.lang.learn.c-c++/browse_thread/thread/fb75b00f73e979db/018b8d0eadb38dbf?q=%22STL+insert+with+hint%22+%22Mark+P%22&rnum=1&hl=en#018b8d0eadb38dbf I quoted the SGI STL docs describing a.insert(p, t), where p is the hint iterator and t is the inserted object: "Insert with hint is logarithmic in general, but it is amortized...
4
14884
by: Eddie | last post by:
I wondering which one of the following I should use to get the best performance. 1. "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED" OR 2. "WITH (NOLOCK)" I notice that when I use the #1 "SET TRANSACTION..." it sets a lock Mode type of "Sch-S" (Schema stability Lock) which described by SQL Books Online as "Schema stability (Sch-S) locks do not block any transactional locks, including exclusive (X) locks"
6
8523
by: laurenq uantrell | last post by:
Is it possible to use With (NOLOCK) and With (READPAST) in the same SELECT query and what whould be the syntax? @param int SELECT myRow FROM dbo.myTable WITH (NOLOCK) WHERE
2
7202
by: Roger Gordon | last post by:
Hi, When we use WITH (NOLOCK) at the end of table list in a SELECT query, a Cartesian join is returned (i.e. approximately 6,500 records instead of 57, which is distressing.
4
18832
by: Robert | last post by:
Greetings I am assisting a developer with an Access application performance problem and an ODBC timeout. In a nutshell they have a combo box with a drop down that queries a lookup table on a SQL Server database over ODBC and returns 30,000+ records each time it is accessed. By using a stored procedure to view the locks on the tables in the DB we suspect that a locking issue or a dead lock occurs which is causing the timeout. Once the user...
3
3456
by: Rycho | last post by:
Hi, Is there a way to catch every select comming to sql and change it to select with nolock? or how to make database READ UNCOMMITTED permanent? any ideas? Richard
1
4880
by: blechner | last post by:
Is there a with (nolock) equivalent when writing queries in Access 2003?
5
2931
by: bmm | last post by:
I have a Store Procedure on a Sql Server 2000 Where I use the Table Hint "NoLock" on all selects. One of my clients (OleDbConnection from C#) doesn't get the same Result Set as the others. The result Set should have 31 rows but this client only gets 5! When I remove all the "NoLocks" everything works fine. How can that be?
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.