<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL Server NOLOCK Hint &amp;amp; other poor ideas.</title><link>http://blogs.msdn.com/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx</link><description>Frequently I see production code, created by professional development teams, peppered with NOLOCK &amp;amp; other TSQL Hints. While totally understandable, as it is a common recommendation by many internet posts &amp;amp; often found in their sample code, this</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Topics about Books  &amp;raquo; SQL Server NOLOCK Hint &amp;#38; other poor ideas.</title><link>http://blogs.msdn.com/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx#9532771</link><pubDate>Mon, 06 Apr 2009 04:11:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9532771</guid><dc:creator>Topics about Books  &amp;raquo; SQL Server NOLOCK Hint &amp;#38; other poor ideas.</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://books.linkablez.info/2009/04/05/sql-server-nolock-hint-other-poor-ideas/"&gt;http://books.linkablez.info/2009/04/05/sql-server-nolock-hint-other-poor-ideas/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: SQL Server NOLOCK Hint &amp; other poor ideas.</title><link>http://blogs.msdn.com/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx#9533035</link><pubDate>Mon, 06 Apr 2009 06:57:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9533035</guid><dc:creator>Justin A</dc:creator><description>&lt;p&gt;Great post David :) NOLOCKS (as a solution) have always been an embarassing way to 'solve' a deadlock problem .. which as you said, is just hiding the real problem. Time to read those links :)&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt; Ensure your Many to Many tables use both foreign keys as a the unique composite key (Primary key), ie: an Identity Column as a Primary Key is not appropriate for these tables.&lt;/p&gt;
&lt;p&gt;Can u please elaborate on this? maybe make a really quick blog post on this? I've always handled many-to-many tables like this.&lt;/p&gt;
&lt;p&gt;Id INT PK Identity NOT NULL&lt;/p&gt;
&lt;p&gt;TableA_Id INT FK NOT NULL&lt;/p&gt;
&lt;p&gt;TableB_Id INT FK NOT NULL&lt;/p&gt;
&lt;p&gt;I'm very curious to the reasons to making the two FK's the composite PK.&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
</description></item><item><title>re: SQL Server NOLOCK Hint &amp; other poor ideas.</title><link>http://blogs.msdn.com/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx#9535136</link><pubDate>Tue, 07 Apr 2009 07:09:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9535136</guid><dc:creator>Phaneendra</dc:creator><description>&lt;p&gt;A very good post Dave. Having said this, I have a scenario as mentioned below:&lt;/p&gt;
&lt;p&gt;Consider an order processing system, which has processing of an order as a transaction. The transaction is implemented at DAL layer. Since every transaction will have a status, we need to fetch the status id from status master table and associate it with the transaction records while processing the order. In such case, if we dont place nolock, then the status table gets locked until the transaction is committed and subsequently might lead to deadlock scenario when there is more load on the system. &lt;/p&gt;
&lt;p&gt;I am curious to know if there is any alternative for this scenario with out using nolock hint.&lt;/p&gt;
&lt;p&gt;Thanks in advance&lt;/p&gt;
&lt;p&gt;Phani &lt;/p&gt;
</description></item><item><title>re: SQL Server NOLOCK Hint &amp; other poor ideas.</title><link>http://blogs.msdn.com/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx#9535439</link><pubDate>Tue, 07 Apr 2009 09:55:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9535439</guid><dc:creator>Chris Leonard</dc:creator><description>&lt;p&gt;Good, solid basics. &amp;nbsp;But the flip side is that telling a good SQL Server programmer not to hint is a lot like telling a C++ or C# programmer not to debug, and certainly to never use directives.&lt;/p&gt;
&lt;p&gt;The fact of the matter is that the optimizer makes little mistakes all the time (on purpose, in fact, so it doesn't have to spend a huge amount of time optimizing) but some of its mistakes are HUGE. &amp;nbsp;A good SQL developer, who understands the shape and distribution of their data, can frequently fix things. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;One topic I'd like to see more good database blogs write about is plan stability. &amp;nbsp;The single worst thing, other than something that can never be done efficiently in production, is something that occasionally goes inefficient in production. &amp;nbsp;In these cases, when time is tight and the demand to fix something is immediate, a simple hint or two can often straighten out the optimizer and cause a plan to be optimal (or darned close) and absolutely stable. &amp;nbsp;This is infinitely (and I do mean infinitely) preferable to a plan that is absolutely optimal 99% of the time it compiles but terrible 1% of the time. &amp;nbsp;Plan stability should be a top-level priority of any very good SQL developer.&lt;/p&gt;
&lt;p&gt;Sometimes I think our hint-phobia is rooted in a belief that we actually write code in the database. &amp;nbsp;We do not; SQL issues specifications that the optimizer turns into code, and sometimes a hint can make that specification more explicit and complete.&lt;/p&gt;
&lt;p&gt;I know this kind of thing makes the purists gag, but I'm a purist too. &amp;nbsp;Did all the PhD work, relational algebra, relational calculus - the whole nine yards. &amp;nbsp;And I did great at it. &amp;nbsp;But I still don't see why we would ever want to take a tool, like hints, away from programmers, instead of teaching them to use them well.&lt;/p&gt;
&lt;p&gt;So I like your post, but I especially like the energy you put into trying to develop a way to use hints wisely. &amp;nbsp;I'm not so sure we need so many posts, however, telling us to use them &amp;quot;almost never.&amp;quot; &amp;nbsp;A lot of high-profile companies would go out of business without them, because the optimizer is not yet good enough - and as we continue to add features to the kernel, the optimizer probably will never be good enough.&lt;/p&gt;
&lt;p&gt;Just food for thought / my 0.02.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Chris&lt;/p&gt;
</description></item><item><title>re: SQL Server NOLOCK Hint &amp; other poor ideas.</title><link>http://blogs.msdn.com/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx#9586060</link><pubDate>Mon, 04 May 2009 19:29:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9586060</guid><dc:creator>Tony Moe</dc:creator><description>&lt;p&gt;Great post Dave. &lt;/p&gt;
&lt;p&gt;However, I have to comment on a few points:&lt;/p&gt;
&lt;p&gt;1. Starting SQL Server 2005 with the introduction the snapshot isolation, there is no justification whatsoever to use NOLOCK. &lt;/p&gt;
&lt;p&gt;However, before that, NOLOCK was the only viable solution. A SELECT without NOLOCK can be blocked forever if the table being read is constantly being updated.&lt;/p&gt;
&lt;p&gt;READPAST is worse than NOLOCK because it skips the locked data.&lt;/p&gt;
&lt;p&gt;2. Blindly adding NOLOCK to all SELECT statements is a very bad practice. If a result of the SELECT is being used in a sebsequent update, UPDLOCK should be used.&lt;/p&gt;
&lt;p&gt;3. My experience with using SQL Server without query hint has been very discouraging. I can hardly imagine a multi-user real world application that can run without ROWLOCK! &lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
</description></item></channel></rss>