<?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>Deadlock Troubleshooting, Part 2</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/13/deadlock-troubleshooting_2C00_-part-2.aspx</link><description>In this post I’ll look at an actual deadlock, then troubleshoot it using the steps I described in Deadlock Troubleshooting, Part 1 so you can see them in action. This is a simplified version of a deadlock scenario that an internal customer here at Microsoft</description><dc:language>en</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Deadlock Troubleshooting, Part 2</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/13/deadlock-troubleshooting_2C00_-part-2.aspx#9932295</link><pubDate>Fri, 04 Dec 2009 00:44:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9932295</guid><dc:creator>bartduncan</dc:creator><description>&lt;p&gt;@stephen_or_doug, &lt;/p&gt;
&lt;p&gt;First, apologies for the delayed response. &amp;nbsp;If there is an index on column [c1], then SQL will use a very selective index seek and only the rows where [c1]=@p1. &amp;nbsp;But if there is no index that has [c1] as the leading column, SQL will need to scan the table or an index, and it will lock (and hold the lock on) every row that it scans. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;USE tempdb;&lt;/p&gt;
&lt;p&gt;GO&lt;/p&gt;
&lt;p&gt;DROP TABLE t1;&lt;/p&gt;
&lt;p&gt;GO&lt;/p&gt;
&lt;p&gt;CREATE TABLE t1 (c1 int PRIMARY KEY, c2 int);&lt;/p&gt;
&lt;p&gt;INSERT INTO t1 SELECT [object_id], [object_id] FROM master.sys.objects;&lt;/p&gt;
&lt;p&gt;GO&lt;/p&gt;
&lt;p&gt;BEGIN TRAN;&lt;/p&gt;
&lt;p&gt;SELECT OBJECT_ID ('t1')&lt;/p&gt;
&lt;p&gt;SELECT COUNT(*) FROM t1 WITH (ROWLOCK, UPDLOCK, HOLDLOCK) WHERE c2 = 5;&lt;/p&gt;
&lt;p&gt;EXEC sp_lock;&lt;/p&gt;
&lt;p&gt;ROLLBACK;&lt;/p&gt;
&lt;p&gt;GO&lt;/p&gt;
&lt;p&gt;BEGIN TRAN;&lt;/p&gt;
&lt;p&gt;SELECT COUNT(*) FROM t1 WITH (ROWLOCK, UPDLOCK, HOLDLOCK) WHERE c1 = 5;&lt;/p&gt;
&lt;p&gt;EXEC sp_lock;&lt;/p&gt;
&lt;p&gt;ROLLBACK;&lt;/p&gt;
&lt;p&gt;GO&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9932295" width="1" height="1"&gt;</description></item><item><title>re: Deadlock Troubleshooting, Part 2</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/13/deadlock-troubleshooting_2C00_-part-2.aspx#9685354</link><pubDate>Tue, 02 Jun 2009 16:20:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9685354</guid><dc:creator>stephen_or_doug</dc:creator><description>&lt;p&gt;(Sorry if I seem to have posted this twice - forgot to log in the first time!)&lt;/p&gt;
&lt;p&gt;This is a really useful post. One day the documentation that ships with the SQL server product will be equally clear...&lt;/p&gt;
&lt;p&gt;You mention the option of forcing one of the transactions to block early on the process, by doing something like:&lt;/p&gt;
&lt;p&gt;SELECT @x = COUNT(*) FROM t1 WITH (HOLDLOCK, UPDLOCK) WHERE c1 = @p1&lt;/p&gt;
&lt;p&gt;Would this in general:&lt;/p&gt;
&lt;p&gt;a) lock a single set of rows (i.e. the ones where C1=@p1) in the index cidx? &lt;/p&gt;
&lt;p&gt;b) effectively place a index wide lock?&lt;/p&gt;
&lt;p&gt;If it is a), then the issue I have is that the two processes are independent, (different users logged into different clients!) so the runtime values of @p1 for the two procedures are more than likely to be completely different - in fact the values apply to different columns in the table, so there is no relationship between the parameters for the procedures at all.&lt;/p&gt;
&lt;p&gt;In addition, the count(*) is likely to be expensive in some cases - what if there are millions of rows?. If the locking is an index wide lock, would this lock still be held, even if the value for @p1 was chosen such that there we no rows at all where c1=@p1?&lt;/p&gt;
&lt;p&gt;These may be silly questions !&lt;/p&gt;
&lt;p&gt;This is such black art stuff.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9685354" width="1" height="1"&gt;</description></item><item><title>re: Deadlock Troubleshooting, Part 2</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/13/deadlock-troubleshooting_2C00_-part-2.aspx#9461054</link><pubDate>Fri, 06 Mar 2009 02:56:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9461054</guid><dc:creator>Sarabjeet singh Arors</dc:creator><description>&lt;p&gt;Bart Sir, You are great, and I also worked for Microsoft in old willows building 10525.This was the great help for every Sql lover guys.&lt;/p&gt;
&lt;p&gt;Thanks and Keep Posted!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9461054" width="1" height="1"&gt;</description></item><item><title>Bart Duncan's SQL Weblog : Deadlock Troubleshooting, Part 3</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/13/deadlock-troubleshooting_2C00_-part-2.aspx#2361531</link><pubDate>Tue, 01 May 2007 22:08:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2361531</guid><dc:creator>Bart Duncan's SQL Weblog : Deadlock Troubleshooting, Part 3</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/bartd/archive/2006/09/25/deadlock-troubleshooting-part-3.aspx"&gt;http://blogs.msdn.com/bartd/archive/2006/09/25/deadlock-troubleshooting-part-3.aspx&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2361531" width="1" height="1"&gt;</description></item></channel></rss>