Link to home
Start Free TrialLog in
Avatar of kowsika devi
kowsika devi

asked on

sql server 2012

hi,
Is adviciable to use nolock function in select query?

what is the diff of nolock and with (nolock)
Avatar of ste5an
ste5an
Flag of Germany image

You should use NOLOCK only when it is unimportant whether the results are correct or not.

Cause NOLOCK can lead to return not (longer) existing data or to not return existing data. And there is no way for you to be sure..
Is adviciable to use nolock function in select query?
it depends.

WITH(NOLOCK)
It is equal to using READ UNCOMMITED as a transaction isolation level. So, you stand the risk of reading an uncommitted row that is subsequently rolled back, i.e. data that never made it into the database. So, while it can prevent reads being deadlocked by other operations, it comes with a risk. Basically you can get dirty reads. REF WITH(NOLOCK) - Does not improve performance. WITH(NOLOCK) is NOT by default.

Read more from -
https://www.mssqltips.com/sqlservertip/2470/understanding-the-sql-server-nolock-hint/
http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/

With lock is by default and you do not have to specify anything for this.
Avatar of kowsika devi
kowsika devi

ASKER

to avoid deadlock we can use otherwise it should not use is it correct jj
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks jj
Note to self:  Make one of those 'Worlds's Most Interesting Man' memes where the top says something like 'I don't always SELECT large sets in production databases', and the bottom says 'But when I do I use WITH(NOLOCK) and SET TRANSACTION LEVEL LOW'.
Na, I only use NOLOCK to make queries faster.. ya 'now, no locking and that overhead transaction isolation voodoo.. :)

You won't believe how often I've seen NOLOCK used as "improved query performance"..