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


With Nolock


With Nolock

Author
Message
AMJ-458987
AMJ-458987
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 266
Hi All,

I am trying these queries -

SELECT TOP 10 * FROM Table1
select top 10 * from Table1 WITH (NOLOCK)

Both the queries will give different result set.
But when i used order by with them the result set will be the same.
Is this related to Isolation level in SQL Server or anything else?

I am very much confused.

Waiting for reply.

MJCool
Garadin
Garadin
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2691 Visits: 4107
(NOLOCK) allows dirty reads. Basically, there is a chance that if you are reading data out of the table while it is in the process of being updated, you could read the wrong data. You can also read data that has been modified by transactions that have not been committed yet as well as a slew of other problems.

Best practice is not to use NOLOCK unless you are reading from tables that really don't change (such as a table containing states) or from a data warehouse type DB that is not constantly updated.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23307 Visits: 9730
In the case of data that doesn't change, that's better off in a read-only database or read-only file. SQL Server won't even attempt to take locks on read-only data, and thus avoids the processing overhead involved. The performance boost is minor, but in a busy system it can make a difference.

With(nolock) has some uses very occassionally. Inappropriate use of it is the single most common error I've ever seen in database devs. Most don't understand that it can result in all kinds of junk data being used in important business processes.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
AMJ-458987
AMJ-458987
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 266
It means what i am getting dirty or uncommitted records from second query (select top 10 * from Table1 With(NOLOCK).
Actually i am confused because of order by clause. When i use order by both the queries will give me same result set.

Reason why i am stuck in this situation is that we are improving the performance of our application using WITH (NOLOCK). During this process i got two different result set from the similar queries .

My question is that will it affect my work or not? If i get two different result sets.
Or it is better not to use WITH (NOLOCK) in select statement.

MJCool
Dave Ballantyne
Dave Ballantyne
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2872 Visits: 8370
AMJ-458987 (10/21/2009)When i use order by both the queries will give me same result set.


Probably the rows in the dirty read wont be in the top 10 of your ordered results.
As you have discovered nolock can be a very dangerous beast.

Take a look at this link
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/10/1280.aspx



Clear Sky SQL
My Blog
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23307 Visits: 9730
Using With(Nolock) can improve performance, at the cost of reliability and integrity.

The question to ask on that one is: If you were a manager, and had an employee who was really, really fast at his job, but who lied to you, broke things, and sabotaged the work of his co-workers, would you keep him around? If the answer is yes, then with(nolock) is for you.

The thing you're seeing with Top without an Order By is that SQL Server (and any normal database) can give you whatever it wants in that case. What you're doing is telling it, "give me the most convenient 10 rows". Since one query honors locks and the other doesn't, they're going to have different definitions of "most convenient". That's not so much an issue with "with(nolock)" as it is an issue with unordered Top commands. You never know what you're going to get with one of those.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
ranjitrjha
ranjitrjha
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 43
Mithilesh,
I tried a similar query. I do agree that NOLOCK gives uncommited data but very strangely when I was trying the queries, there was no other user/activity...so i dont expect any thing uncommited. Can anybody tell the reason for different result set when there is no update/lock on table.
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23307 Visits: 9730
ranjitrjha (10/22/2009)
Mithilesh,
I tried a similar query. I do agree that NOLOCK gives uncommited data but very strangely when I was trying the queries, there was no other user/activity...so i dont expect any thing uncommited. Can anybody tell the reason for different result set when there is no update/lock on table.


Because an unordered Top statement gives you whatever comes up. It doesn't have consistency, because it's not required to.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
ranjitrjha
ranjitrjha
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 43
GSquared,
Your explanation makes sense but when i am running
select top 10 * from table1
select top 10 * from table1
then for both queries, result set is same.

It's only when I put a nolock hint as in queries below, the result set differs.
select top 10 * from table1
select top 10 * from table1 with (nolock)
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23307 Visits: 9730
That's simply due to the fact that the software is essentially lazy. The first query, run multiple times, is likely to pull the same data, because both the plan and the data are cached. Even if you clear the cache, you're likely to get the same data on multiple runs of the same query, because of the physical implementation of the database and the fact that the devs didn't add in random number generators just for the heck of it.

The point is that you are likely to get the same data. You aren't guaranteed to get the same data.

Thus, a minor, seemingly immaterial change in the query, is likely to get different results. Again, it might, or it might not.

The point is that you don't know what you'll get. It might give you the same results 10,000 times in a row, and then on the 10,001st, give you something different. No way to know.

Since most businesses don't want to get that kind of unpredictability out of their data, it's better to force the issue. That means ordering your query if you want the top X rows. It means avoiding "nolock" unless you have a real business reason to allow dirty reads. Has a lot of other ramifications, but those are definitely two of them.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
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