Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

NOLOCK is giving different result sets.Why? Expand / Collapse
Author
Message
Posted Tuesday, June 4, 2013 11:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 4, 2013 11:26 AM
Points: 1, Visits: 0
I have 2 select statements. One with NOLOCK hint and other without NOLOCK hint. I am getting different result sets. Rows are same but there order is different. Why?
Post #1459887
Posted Tuesday, June 4, 2013 11:51 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:09 AM
Points: 14,215, Visits: 37,154
sachin6139 (6/4/2013)
I have 2 select statements. One with NOLOCK hint and other without NOLOCK hint. I am getting different result sets. Rows are same but there order is different. Why?


SQLServer never, ever guarantees that results will be in any order unless an explicit ORDER BY is added to a query.
If order is important, you have to tell SQL to use an Order By.

the engine will use various indexes to get the data in what it determines is the fastest way possible; because the two queries are not the same, the apparent order is not duplicated between the two.

even the same query repeated multiple times can be affected by parallelism, SET options, statsitics being updated and much much more.


Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1459889
Posted Tuesday, June 4, 2013 12:06 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:14 AM
Points: 15,571, Visits: 15,726
In addition to Lowell's excellent explanation I would add that you need to be VERY careful about NOLOCK. Given your question I am assuming you are testing because you have heard that using NOLOCK will make your queries faster. While this is possible, you can also get duplicate and/or missing rows. This behavior is not consistent and nearly impossible to reproduce.

http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/

http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1459893
Posted Tuesday, June 11, 2013 2:50 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:55 AM
Points: 1,125, Visits: 3,397
sachin6139 (6/4/2013)
I have 2 select statements. One with NOLOCK hint and other without NOLOCK hint. I am getting different result sets. Rows are same but there order is different. Why?


The order changes due to the way SQL Server reads pages with and without NOLOCK. This article will explain why:

http://thesqlguy.blogspot.com/2013/02/nolock.html

The article also explains why you shouldn't use NOLOCK, but that has already been covered above.




The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1461933
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse