<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.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>Search results matching tags 'Database Administration' and 'Deadlock'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Database+Administration,Deadlock&amp;orTags=0</link><description>Search results matching tags 'Database Administration' and 'Deadlock'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Clarification on SQL Server Deadlocks being Unavoidable</title><link>http://sqlblog.com/blogs/jonathan_kehayias/archive/2009/04/30/clarification-on-sql-server-deadlocks-being-unavoidable.aspx</link><pubDate>Thu, 30 Apr 2009 09:43:36 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:13664</guid><dc:creator>jmkehayias</dc:creator><description>&lt;p&gt;While running through some of my personal emails tonight, I happened across two different questions regarding some statements I have made on forums post in the past, as well as in one of my &lt;a href="http://www.sqlservercentral.com/articles/deadlocks/65614/" target="_blank"&gt;articles on SQL Server Central&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;Even with &amp;quot;proper design&amp;quot; it is still possible to have deadlocking occur which is why it is crucial for applications to properly handle 1205 errors generated by the database engine. If a deadlock occurs, a properly built application should log the occurrence, but also resubmit the deadlocked transaction as a part of handling the error.&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;For whatever reason, this statement seems to stop being read at:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;Even with &amp;quot;proper design&amp;quot; it is still possible to have deadlocking occur&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;and it is being taken to mean that deadlocks in SQL Server are unavoidable.&amp;#160; This is not what this statement is intending to say by far, and I am a firm believer that deadlocks are generally caused by a fundamental design problem that can be fixed.&amp;#160; The real problem is that sometimes, fixing the design problem is very complex and could mean restructuring the database, refactoring code, and/or other tasks that are not necessarily feasible to do at the moment.&amp;#160; If you find yourself in one of these major redesign scenarios, solving the deadlock immediately may not actually be possible.&lt;/p&gt;  &lt;p&gt;Many thanks go out to &lt;a href="http://sqlblog.com/blogs/louis_davidson/default.aspx" target="_blank"&gt;Louis Davidson&lt;/a&gt; who taught me this, or at least made me think about it when I reply/write about deadlocks online, but properly designed applications should have exception handling built into their data access layer which brings us to the remainder of the quote from the article that seems to never get read:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;which is why it is crucial for applications to properly handle 1205 errors generated by the database engine. If a deadlock occurs, a properly built application should log the occurrence, but also resubmit the deadlocked transaction as a part of handling the error.&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Handling exceptions and errors seems to be second place in development these days, at least based on the questions that get asked on the forums at times.&amp;#160; Having written my own applications and tools in the past year, I am not exempt from this problem either.&amp;#160; I have only recently begun to put exception management at the front of my application development, and in a hurry, I often still don't do it until something blows up the first time, and I have to go back and correct my shortcoming.&amp;#160; The entire point of these two statements is that the application should properly handle the deadlock exception, and attempt to resubmit the transaction a second time before raising a hard error back to the user, and it should then log the error in some manner for follow up or tracking.&lt;/p&gt;  &lt;p&gt;Deadlocking seems to be becoming more prevalent based on the trends I see in questions on the forums, which may be partially related to the increasing speed and performance of server hardware, but most often, it is the result of bad design. &lt;/p&gt;</description></item><item><title>The Anatomy of a Deadlock</title><link>http://sqlblog.com/blogs/jonathan_kehayias/archive/2008/07/30/the-anatomy-of-a-deadlock.aspx</link><pubDate>Wed, 30 Jul 2008 16:50:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:13974</guid><dc:creator>jmkehayias</dc:creator><description>&lt;p&gt;A common question of the forums is "How do I stop these deadlocks?"&amp;nbsp; I wrote a small reference article on the SQL Examples Site for the forums for how to trap deadlocks using trace flags and Madhu Nair wrote an article for how to trap them with SQL Profiler:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://code.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=Troubleshoot%20Deadlocking%20in%20SQL%20Server&amp;amp;referringTitle=Home"&gt;Troubleshoot Deadlocking in SQL Server&lt;/a&gt;.     &lt;br&gt;&lt;a href="http://code.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=Troubleshoot%20Deadlocking%20in%20SQL%20Server%202005%20using%20Profiler&amp;amp;referringTitle=Home"&gt;Troubleshoot Deadlocking in SQL Server 2005 using Profiler&lt;/a&gt;.&lt;/p&gt;      &lt;p&gt;Most people seem to understand a UPDATE/INSERT, UPDATE/DELETE, INSERT/INSERT, DELETE/DELETE deadlocks pretty well, and the books online cover them pretty good.&amp;nbsp; However, commonly I see where deadlocks occur with a INSERT/SELECT, UPDATE/SELECT, DELETE/SELECT process that is a bit more complex.&amp;nbsp; The following image should provide a bit more explanation as to what is happening with this kind of deadlock.&lt;/p&gt;&lt;p&gt;&lt;img src="http://lh4.ggpht.com/jmkehayias/SJC_Z7xgutI/AAAAAAAAACk/rHMDX0Pvj64/image%5B4%5D.png" width="534" height="450"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;This is a specific kind of deadlock and is caused by a nonclustered index that forces a key lookup to the clustered index.&amp;nbsp; To demonstrate this kind of structure consider the following:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;use &lt;/span&gt;tempdb&lt;br&gt;go&lt;br&gt;&lt;span style="color:blue;"&gt;create table &lt;/span&gt;KeyLookupDeadlock&lt;br&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;rowid &lt;span style="color:blue;"&gt;int identity primary key&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;br&gt; &lt;/span&gt;firstname &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;30&lt;span style="color:gray;"&gt;),&lt;br&gt; &lt;/span&gt;lastname &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;30&lt;span style="color:gray;"&gt;),&lt;br&gt; &lt;/span&gt;dateofbirth &lt;span style="color:blue;"&gt;datetime&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;br&gt; &lt;/span&gt;favoritecolor &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;10&lt;span style="color:gray;"&gt;))&lt;br&gt;&lt;/span&gt;go&lt;br&gt;&lt;span style="color:blue;"&gt;create nonclustered index &lt;/span&gt;ix_KeyLookupDeadlock_firstname &lt;br&gt;&lt;span style="color:blue;"&gt;on &lt;/span&gt;KeyLookupDeadLock &lt;span style="color:gray;"&gt;(&lt;/span&gt;firstname&lt;span style="color:gray;"&gt;)&lt;br&gt;&lt;/span&gt;go&lt;br&gt;&lt;span style="color:blue;"&gt;insert into &lt;/span&gt;keylookupdeadlock &lt;span style="color:blue;"&gt;select &lt;/span&gt;&lt;span style="color:red;"&gt;'jon'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'kehayias'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'07/29/2008'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'blue'&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;insert into &lt;/span&gt;keylookupdeadlock &lt;span style="color:blue;"&gt;select &lt;/span&gt;&lt;span style="color:red;"&gt;'mickey'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'mouse'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'07/28/2008'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'green'&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;insert into &lt;/span&gt;keylookupdeadlock &lt;span style="color:blue;"&gt;select &lt;/span&gt;&lt;span style="color:red;"&gt;'minnie'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'mouse'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'07/24/2008'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'yellow'&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;insert into &lt;/span&gt;keylookupdeadlock &lt;span style="color:blue;"&gt;select &lt;/span&gt;&lt;span style="color:red;"&gt;'donald'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'duck'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'07/23/2008'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'fusia'&lt;br&gt;&lt;/span&gt;go 150000&lt;br&gt;&lt;span style="color:blue;"&gt;insert into &lt;/span&gt;keylookupdeadlock &lt;span style="color:blue;"&gt;select &lt;/span&gt;&lt;span style="color:red;"&gt;'jim'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'fisher'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'07/28/2008'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'red'&lt;br&gt;&lt;/span&gt;go&lt;br&gt;&lt;span style="color:blue;"&gt;alter index &lt;/span&gt;&lt;span style="color:gray;"&gt;all &lt;/span&gt;&lt;span style="color:blue;"&gt;on &lt;/span&gt;keylookupdeadlock &lt;span style="color:blue;"&gt;rebuild&lt;br&gt;&lt;/span&gt;go&lt;/pre&gt;

  &lt;/blockquote&gt;This will create a table that has enough rows on most systems to cause a key lookup for the following query:

&lt;br&gt;

&lt;blockquote&gt;
  &lt;span style="color:blue;"&gt;select &lt;/span&gt;firstname&lt;span style="color:gray;"&gt;, &lt;/span&gt;lastname&lt;span style="color:gray;"&gt;, &lt;/span&gt;dateofbirth&lt;span style="color:gray;"&gt;, &lt;/span&gt;favoritecolor&lt;span style="color:blue;"&gt;&lt;br&gt;from &lt;/span&gt;keylookupdeadlock&lt;span style="color:blue;"&gt;&lt;br&gt;where &lt;/span&gt;firstname &lt;span style="color:gray;"&gt;= &lt;/span&gt;&lt;span style="color:red;"&gt;'jim'&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote&gt;

  &lt;br&gt;&lt;/blockquote&gt;



&lt;p&gt;&lt;a href="http://lh4.ggpht.com/jmkehayias/SJC_afZq8AI/AAAAAAAAACs/xolrutB4oE4/image%5B8%5D.png"&gt;&lt;img src="http://lh6.ggpht.com/jmkehayias/SJC_arPjnfI/AAAAAAAAACw/cy3e1AB5mN4/image_thumb%5B4%5D.png" style="border-width:0px;" alt="image" border="0" width="812" height="334"&gt;&lt;/a&gt;&lt;/p&gt;Keeping in mind that this is an overly simplistic example, what will happen to cause the deadlock is that two SPIDS will start within microseconds of each other, one issuing the SELECT, and the other attempting to INSERT a new Row.&amp;nbsp; The SELECT immediately takes a shared lock on the NonClustered Index and the INSERT immediately takes a Exclusive Lock on the Clustered Index.&amp;nbsp; For the SELECT to complete, it will need to take a shared lock on the clustered index to get the additional columns in the SELECT list, but it will be blocked.&amp;nbsp; At the same time, to complete the INSERT operation will require an Exclusive lock on the nonclustered indexes on the table, which is incompatible with the existing shared lock held by the SELECT operation, so both processes will be blocked waiting on the other to release its lock.&amp;nbsp; This is where the deadlock occurs.



&lt;p&gt;To resolve this, the nonclustered index can be dropped and replaced with a covering index that uses the INCLUDE option to cover the query completely:&lt;/p&gt;





&lt;blockquote&gt;
  &lt;span style="color:blue;"&gt;create nonclustered index &lt;/span&gt;ix_KeyLookupDeadlock_firstname_included &lt;span style="color:blue;"&gt;&lt;br&gt;on &lt;/span&gt;KeyLookupDeadLock &lt;span style="color:gray;"&gt;(&lt;/span&gt;firstname)&lt;span style="color:blue;"&gt;&lt;br&gt;include &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;lastname&lt;span style="color:gray;"&gt;, &lt;/span&gt;dateofbirth&lt;span style="color:gray;"&gt;, &lt;/span&gt;favoritecolor&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;br&gt;&lt;/blockquote&gt;



&lt;p&gt;The resulting execution plan shows that the Key Lookup no longer happens, which will revent the deadlock from occuring:&lt;/p&gt;&lt;p&gt;&lt;img src="http://lh5.ggpht.com/jmkehayias/SJC_akIuvUI/AAAAAAAAAC0/PcMf2yxNz34/image%5B12%5D.png" width="640" height="261"&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;For further information on Lock compatibilities see:&lt;/p&gt;



&lt;h4&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms186396.aspx"&gt;Lock Compatibility (Database Engine)&lt;/a&gt;&lt;/h4&gt;</description></item></channel></rss>