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


WITH (NOLOCK)


WITH (NOLOCK)

Author
Message
pkrieg
pkrieg
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 1

We are having some locking problems with our database and are planning to implement the WITH (NOLOCK) to help with this problem.

We have some questions that we would appreciate your help with:

1) Is the default on 'SELECT * FROM x' to lock each row?

2) Should all SELECTS have WITH (NOLOCK)?

3) Should all INNER JOINS have WITH (NOLOCK) if they are not part of an UPDATE?

4) Will performance improve using WITH (NOLOCK)s?

5) Is the default on 'UPDATE...' to lock each row (ROWLOCK)?

6) Will performance improve using WITH (ROWLOCK)?

7) Can we or should we include WITH (NOLOCK) on temp files?

Thanks in advance.

Phil





Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84555 Visits: 41064
1) Is the default on 'SELECT * FROM x' to lock each row?
I don't believe so but could be wrong.
2) Should all SELECTS have WITH (NOLOCK)?
Only if it's OK to do a "dirty read".
3) Should all INNER JOINS have WITH (NOLOCK) if they are not part of an UPDATE?
Yes, if it's OK to do a "dirty read".
4) Will performance improve using WITH (NOLOCK)s?
Yes, if locks are a problem. NOLOCK allows dirty reads past most locks.
5) Is the default on 'UPDATE...' to lock each row (ROWLOCK)?
In order to do an UPDATE on a row, the row must be locked even for the briefest of perios.
6) Will performance improve using WITH (ROWLOCK)?
No. Only INSERT, UPDATE, and DELETE require row locks and they pretty well do it on their own.
7) Can we or should we include WITH (NOLOCK) on temp files?
Nothing will be gained by NOLOCKing temp files because temp files can only be accessed by the connection that created them.

Suggestion... adding WITH (NOLOCK) or just (NOLOCK) to each table name is a real pain and you should be careful not to NOLOCK the target table of an INSERT, UPDATE, or DELETE. An easier solution might be to use SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED at the beginning of a query which is the same as using WITH (NOLOCK) on all tables involved in a SELECT statement or used as inputs to INSERT, UPDATE, or DELETE. If you have a particular case where dirty reads should not be allowed, then use WITH (ROWLOCK) only on those tables where the dirty read should not be allowed.



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
pkrieg
pkrieg
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 1

Thanks for your reply Jeff. It confirmed my 'book' findings.

Phil





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