sql server 2012

kowsika devi
kowsika devi used Ask the Experts™
on
hi,
Is adviciable to use nolock function in select query?

what is the diff of nolock and with (nolock)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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..
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
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.

Author

Commented:
to avoid deadlock we can use otherwise it should not use is it correct jj
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Database Expert
Awarded 2016
Top Expert 2016
Commented:
>>to avoid deadlock we can use otherwise it should not use is it correct jj
When you use NOLOCK  there is no guarantee that a deadlock will not occur. You need to optimize your query.

Author

Commented:
thanks jj
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
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'.
ste5anSenior Developer

Commented:
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"..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial