June 4, 2013 at 11:44 am
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?
June 4, 2013 at 11:51 am
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
June 4, 2013 at 12:06 pm
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/[/url]
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 11, 2013 at 2:50 am
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.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply