<?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>Bart Duncan's SQL Weblog</title><link>http://blogs.msdn.com/b/bartd/</link><description /><dc:language>en</dc:language><generator>Telligent Community 5.6.583.21163 (Build: 5.6.583.21163)</generator><item><title>Row Goals Gone Rogue</title><link>http://blogs.msdn.com/b/bartd/archive/2012/03/14/row-goals-gone-rogue.aspx</link><pubDate>Thu, 15 Mar 2012 00:14:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10283345</guid><dc:creator>bartduncan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10283345</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10283345</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2012/03/14/row-goals-gone-rogue.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This post discusses &amp;ldquo;&lt;strong&gt;row goals&lt;/strong&gt;&amp;ldquo;, but with a twist. The point is to illustrate how row goals can cause unnecessarily slow queries. First, run this script:&lt;/p&gt;
&lt;pre&gt;    USE tempdb
    GO
    IF OBJECT_ID ('even') IS NOT NULL DROP TABLE even;
    IF OBJECT_ID ('odd') IS NOT NULL DROP TABLE odd;
    GO
    CREATE TABLE even (c1 int, c2 CHAR(30));
    CREATE TABLE odd (c1 int, c2 CHAR(30));
    GO
    SET NOCOUNT ON;
    DECLARE @x int;
    SET @x = 1;
    BEGIN TRAN;
    WHILE (@x &amp;lt;= 10000)
    BEGIN
        INSERT INTO even (c1, c2) VALUES (@x * 2, '');
        INSERT INTO odd (c1, c2) VALUES (@x * 2 - 1, '');
        IF @x % 1000 = 0
        BEGIN
            RAISERROR ('Inserted %d rows...', 0, 1, @x) WITH NOWAIT;
            COMMIT TRAN;
            BEGIN TRAN;
        END;
        SET @x = @x + 1;
    END;
    WHILE @@TRANCOUNT &amp;gt; 0 COMMIT TRAN;
    GO&lt;/pre&gt;
&lt;p&gt;This will insert 10,000 even integers into a table named [even], and the same number of odd values into a table named [odd]. Then consider this simple query:&lt;/p&gt;
&lt;pre&gt;    -- QUERY #1
    SELECT *
    FROM even t1
    INNER JOIN even t2 ON t1.c1 = t2.c1&lt;/pre&gt;
&lt;p&gt;This joins the [even] table to itself, and will return all 10,000 rows from the table. If you run with &amp;ldquo;&lt;strong&gt;SET STATISTICS TIME ON; SET STATISTICS PROFILE ON;&lt;/strong&gt;&amp;rdquo; to see the query plan that SQL selects for this query, you should find that it chooses a hash match:&lt;/p&gt;
&lt;pre&gt;StmtText
------------------------------------------------------------------------------------------
  |--Hash Match(Inner Join, HASH:([t1].[c1])=([t2].[c1]), RESIDUAL:([t2].[c1]=[t1].[c1]))
      |--Table Scan(OBJECT:([tempdb].[dbo].[even] AS [t1]))
      |--Table Scan(OBJECT:([tempdb].[dbo].[even] AS [t2]))&lt;/pre&gt;
&lt;p&gt;While a hash join is often the fastest join option, a hash has the highest initial &lt;strong&gt;build cost&lt;/strong&gt; of any join strategy. Before the hash join can evaluate even a single row for the join, it must first hash all of the rows in one of the join inputs and store these in a hash table (&amp;ldquo;build cost&amp;rdquo; refers to this up-front work). Only then can it begin to join the rows in the other input with the rows in the under-the-covers hash table. In contrast to a hash join, a nested loop join strategy has an extremely low build cost. There are no expensive up-front delays before a nested join can produce its first row, but by default SQL optimizes with the goal of minimizing total query execution time. In this case, the nature of a hash join allows it to complete the query in a small fraction of the time it would take for a loop join (on my machine the hash join plan runs in about 100ms), despite the high initial setup cost. So the optimizer is doing the right thing by selecting a hash join for this query. However, watch what happens if you add a &amp;ldquo;&lt;strong&gt;TOP 1&lt;/strong&gt;&amp;rdquo; to the query:&lt;/p&gt;
&lt;pre&gt;    -- QUERY #2
    SELECT &lt;strong&gt;&lt;span style="color: #ff0000;"&gt;TOP 1&lt;/span&gt;&lt;/strong&gt; *
    FROM even t1
    INNER JOIN even t2 ON t1.c1 = t2.c1&lt;/pre&gt;
&lt;p&gt;The plan changes to use a loop join, and the query completes in a few milliseconds:&lt;/p&gt;
&lt;pre&gt;   Rows  EstRows  Executes  EstExecs StmtText
   ----- -------- --------- --------- -------------------------------------------------------------
   1     1        1         1         |--Top(TOP EXPRESSION:((1)))
   1     1        1         1             |--Nested Loops(Inner Join, WHERE:([t2].[c1]=[t1].[c1]))
   1     1        1         1                 |--Table Scan(OBJECT:([tempdb].[dbo].[even] AS [t1]))
   1     10000    1         1                 |--Table Scan(OBJECT:([tempdb].[dbo].[even] AS [t2]))&lt;/pre&gt;
&lt;p&gt;The addition of a &lt;strong&gt;TOP 1&lt;/strong&gt; tells the optimizer that it doesn&amp;rsquo;t have to execute the whole query; it only has to produce enough rows out of each child operator in the query to get a single row out the top. The optimizer takes this into account and correctly estimates that for this join it won&amp;rsquo;t have to scan many rows at all before finding a match and taking an early exit. As a result, it decides to use a nested loop join, because a loop join should be able to produce that single row and exit long before the hash join could build its hash table. This is also the right thing to do. If you want to prove it to yourself, run query #2 with &lt;strong&gt;SET STATISTICS TIME ON&lt;/strong&gt;, then again with an &lt;strong&gt;OPTION(HASH JOIN)&lt;/strong&gt; hint to force a hash join despite the higher cost. You should find that the hash join is faster when returning all rows, but the loop join is faster when only one row must be returned. This query optimizer intelligence is possible because of the &amp;ldquo;row goal&amp;rdquo; feature.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;TOP&lt;/strong&gt; operator isn&amp;rsquo;t the only thing that can cause the optimizer to set a row goal. An &lt;strong&gt;OPTION (FAST N)&lt;/strong&gt; query hint sets a row goal of N &amp;mdash; the optimizer will cost plans in about the same way that it would with a &lt;strong&gt;TOP N&lt;/strong&gt;, but without artificially restricting the number of rows the query will return. The chosen plan should return the first N rows as quickly as possible, but the end-to-end query execution time might be much slower than you&amp;rsquo;d get without the hint. Also, semi-joins or anti-semi-joins, like you&amp;rsquo;d get with an &lt;strong&gt;EXISTS&lt;/strong&gt; or &lt;strong&gt;NOT EXISTS&lt;/strong&gt; clause, can also implicitly set a row goal of 1.&lt;/p&gt;
&lt;p&gt;If row goals are a new concept for you, I recommend reading through Paul White&amp;rsquo;s &lt;a title="Row Goals In Depth" href="http://sqlblog.com/blogs/paul_white/archive/2010/08/18/inside-the-optimiser-row-goals-in-depth.aspx" target="_parent"&gt;Row Goals In Depth&lt;/a&gt; article before continuing.&lt;/p&gt;
&lt;p&gt;Now, let&amp;rsquo;s run the same &lt;strong&gt;TOP 1&lt;/strong&gt; query, but instead of joining [even] to itself, we&amp;rsquo;ll join [even] to the [odd] table:&lt;/p&gt;
&lt;pre&gt;     -- QUERY #3
     SELECT TOP 1 *
     FROM even t1
     INNER JOIN &lt;strong&gt;&lt;span style="color: #ff0000;"&gt;odd&lt;/span&gt; &lt;/strong&gt;t2 ON t1.c1 = t2.c1&lt;/pre&gt;
&lt;p&gt;You&amp;rsquo;ll note that this query is extremely slow relative to the others. On my machine it takes about 9 seconds, which is several thousand times slower than the similar query that does a self-join on the [even] table. Now you and I know that the [even] table contains even integers, and the [odd] table contains odd integers. Based on that understanding, we can guess that this query should return no rows. But the query optimizer has no concept of &amp;ldquo;odd&amp;rdquo; and &amp;ldquo;even&amp;rdquo;. Remember that &lt;a href="http://blogs.msdn.com/b/bartd/archive/2006/07/25/limited-statistics-granularity.aspx" target="_parent"&gt;statistics only provide a high-level summary of the data in a column&lt;/a&gt;. In this case, the statistics on the [even] and [odd] tables seem to indicate that the [c1] columns in these two tables have similar data distributions, so from the query optimizer&amp;rsquo;s perspective this is the same problem it was faced with in query #2. It guesses that it will very quickly find a matching row, so it chooses a loop join-based plan to minimize operator startup costs. If you look at the plan, you can see that it selects the same plan that was used for query #2. Unfortunately, in this case the results are disastrous. The query plan slogged through 100 million rows (10,000 * 10,000) looking for a match for the join, and it never found one. The &lt;strong&gt;Top &lt;/strong&gt;operator in this plan reflects the row goal of 1.&lt;/p&gt;
&lt;pre&gt;   Rows      EstRows  Executes  EstExecs StmtText
   --------- -------- --------- --------- -------------------------------------------------------------
   0         1        1         1         |--Top(TOP EXPRESSION:((1)))
   0         1        1         1             |--Nested Loops(Inner Join, WHERE:([t2].[c1]=[t1].[c1]))
   10000     1        1         1                 |--Table Scan(OBJECT:([tempdb].[dbo].[even] AS [t1]))
   100000000 10000    1         2                 |--Table Scan(OBJECT:([tempdb].[dbo].[even] AS [t2]))&lt;/pre&gt;
&lt;p&gt;What happened here? This mess is caused by a combination of things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &amp;ldquo;&lt;strong&gt;TOP 1&lt;/strong&gt;&amp;rdquo; told the query optimizer that it should choose a plan that is likely to return a single row as fast as possible, rather than optimizing for return of all rows that could satisfy the join.&lt;/li&gt;
&lt;li&gt;The data in the [even] and [odd] tables exhibit negative correlation. That is, the values in [even] are never going to match a value in [odd], even though the data distribution is similar. (If you can make a prediction about the data in one column based on the data in another column, you can say that those columns are &lt;strong&gt;correlated&lt;/strong&gt;.)&lt;/li&gt;
&lt;li&gt;There are a few exceptions, but in general you can say that &lt;em&gt;the query optimizer is ignorant of data correlation&lt;/em&gt;. In this case, the implication is that the QO can&amp;rsquo;t know at compile time that it will never find a value in [even] that matches a value in [odd].&lt;/li&gt;
&lt;li&gt;Where the data distributions described by SQL&amp;rsquo;s statistics indicate overlapping data ranges, the optimizer is generally optimistic, and assumes that it will find a match if it joins those two sets.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last item is key. It means that there is a fundamental assumption of positive correlation built into join cardinality estimation. Usually when you join two sets this is the right assumption. For example, consider:&lt;/p&gt;
&lt;pre&gt;    SELECT * FROM Employee
    INNER JOIN Manager ON Employee.ManagerId = Manager.Id&lt;/pre&gt;
&lt;p&gt;Every employee&amp;rsquo;s [ManagerId] field will generally refer to a row that exists in the [Manager] table. But occasionally you join two sets together expecting to get no or few matches even though the high-level data distribution looks similar. The example in this post is artificial, but there are plenty of real world examples. Consider a data cleansing routine that is looking for illegal values in some data imported from an untrusted source.&lt;/p&gt;
&lt;pre&gt;    SELECT * FROM ImportedData
    WHERE RegionCode IN (SELECT RegionCode FROM DisallowedRegions)&lt;/pre&gt;
&lt;p&gt;That query by itself won&amp;rsquo;t hit this problem because there&amp;rsquo;s no row goal. But combine it with an &lt;strong&gt;EXISTS&lt;/strong&gt; so that the QO sets an implied row goal of 1, and you may find yourself waiting for a while:&lt;/p&gt;
&lt;pre&gt;    &lt;strong&gt;&lt;span style="color: #ff0000;"&gt;IF EXISTS&lt;/span&gt; &lt;/strong&gt;(
        SELECT * FROM ImportedData
        WHERE RegionCode IN (SELECT RegionCode FROM DisallowedRegions))
    BEGIN
        RAISERROR ('Found some bad data', 16, 1)
    END;&lt;/pre&gt;
&lt;p&gt;This is by-design behavior. While the optimizer is pretty smart, the hypothetical world it models to come up with cost estimates doesn&amp;rsquo;t always capture all of the nuances of real world data. Usually these simplifying assumptions are good, because they allow SQL to produce good plans without taking too long to compile. But sometimes the simplified model leads to suboptimal plans. It pays to know the optimizer&amp;rsquo;s limits, so that you can quickly recognize these cases during query tuning.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;(cross-posted &lt;a href="http://bartduncansql.wordpress.com/2012/03/14/row-goals-gone-rogue"&gt;here&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10283345" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/T_2D00_SQL/">T-SQL</category></item><item><title>Don’t depend on expression short circuiting in T-SQL (not even with CASE)</title><link>http://blogs.msdn.com/b/bartd/archive/2011/03/03/don-t-depend-on-expression-short-circuiting-in-t-sql-not-even-with-case.aspx</link><pubDate>Thu, 03 Mar 2011 18:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10136555</guid><dc:creator>bartduncan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10136555</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10136555</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2011/03/03/don-t-depend-on-expression-short-circuiting-in-t-sql-not-even-with-case.aspx#comments</comments><description>&lt;p&gt;There are a fair number of blog posts and forum discussions regarding expression short circuiting in T-SQL. Some of the most authoritative posts, like &lt;a href="http://www.sqlmag.com/article/tsql3/short-circuit.aspx"&gt;&lt;span style="color: #2970a6;"&gt;this one&lt;/span&gt;&lt;/a&gt;, come to the following conclusions: (a) You cannot depend on expression evaluation order for things like &amp;ldquo;&lt;strong&gt;WHERE &amp;lt;expr1&amp;gt; OR &amp;lt;expr2&amp;gt;&lt;/strong&gt;&amp;ldquo;, since the optimizer might choose a plan that evaluates the second predicate before the first one. But, (b) order of evaluation of the expressions in a CASE statement is fixed, so you can depend on deterministic short circuit evaluation of a CASE statement. For example, this wouldn&amp;rsquo;t protect you from a divide-by-zero error:&lt;/p&gt;
&lt;pre&gt;&lt;span style="font-family: courier new,courier;"&gt;    WHERE (@value = 0) OR ((col1 / @value) = 2)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;But the idea is that this variation is functionally-equivalent, and should protect you from the error:&lt;/p&gt;
&lt;pre&gt;&lt;span style="font-family: courier new,courier;"&gt;    WHERE
        CASE
            WHEN (@value = 0) THEN 2
            ELSE (col1 / @value)
        END = 2&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Before now that&amp;rsquo;s the advice I would have offered, too. But I just ran into a situation where a CASE statement does not provide predictable short circuiting. Here&amp;rsquo;s a simplified repro:&lt;/p&gt;
&lt;pre&gt;&lt;span style="font-family: courier new,courier;"&gt;    ALTER FUNCTION dbo.test_case_short_circuit (@input INT)
    RETURNS TABLE
    AS
    RETURN (
        SELECT calculated_value =
            CASE
                WHEN @input &amp;lt;= 0 THEN 0
                ELSE LOG10 (@input)
            END
    );
    GO

    SELECT * FROM dbo.test_case_short_circuit (0);
    GO&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This fails with this error:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: #ff0000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Msg 3623, Level 16, State 1, Line 2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #ff0000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; An invalid floating point operation occurred.&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The LOG10 function raises this error when its input is 0 or a negative value. In some cases it appears that the plan may still evaluate the expression in the second CASE branch even when it won&amp;rsquo;t be using the value. This is a case where CASE doesn&amp;rsquo;t provide deterministic short circuiting.&lt;/p&gt;
&lt;p&gt;I want to make sure no one takes away the conclusion that SQL Server doesn&amp;rsquo;t support expression short circuiting. It definitely does. It&amp;rsquo;s just that you don&amp;rsquo;t have explicit control over the order of expression evaluation &amp;mdash; even with CASE, apparently. And if you&amp;rsquo;re going to depend on short circuiting, you need a deterministic order of expression evaluation.&lt;/p&gt;
&lt;p&gt;What can you do about it? One option would be to always scrub things so that an error isn&amp;rsquo;t possible even when the CASE branch&amp;rsquo;s output won&amp;rsquo;t be used. For example, using &amp;ldquo;ELSE LOG10 (CASE WHEN @input &amp;lt;= 0 THEN 1 ELSE @input END)&amp;rdquo; in the repro script doesn&amp;rsquo;t change the behavior of the function, but avoids the error. Unfortunately, that&amp;rsquo;s not so pretty.&lt;/p&gt;
&lt;p&gt;UPDATE (4 Mar 2011): To be clear, I&amp;rsquo;ve used CASE before for its short-circuiting properties, and I don&amp;rsquo;t intend to go back and revisit all of that old code in light of this example. This seems like an edge case to me. But it&amp;rsquo;s worth being aware that such edge cases exist if you&amp;rsquo;re thinking about relying on CASE for short circuiting. The most defensive programming approach would be to write the expression in such a way that it doesn&amp;rsquo;t require particular short circuiting behavior. &lt;/p&gt;
&lt;p&gt;UPDATE (10 Jun 2011): The owners of this code have marked this bug as fixed. From their comments, it sounds like you are supposed to be able to rely on deterministic order of expression evaluation for CASE statements. But any SQL release in your hands right now will still be vulnerable to this problem -- keep an eye out for the issue as you use CASE for short circuiting.&lt;/p&gt;
&lt;p&gt;(Cross-posted to &lt;a href="http://bartduncansql.wordpress.com/2011/03/03/dont-depend-on-expression-short-circuiting-in-t-sql-not-even-with-case/"&gt;here&lt;/a&gt;.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10136555" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/T_2D00_SQL/">T-SQL</category></item><item><title>Query Tuning Fundamentals: Density, Predicates, Selectivity, and Cardinality</title><link>http://blogs.msdn.com/b/bartd/archive/2011/01/25/query_5F00_tuning_5F00_key_5F00_terms.aspx</link><pubDate>Tue, 25 Jan 2011 19:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10120173</guid><dc:creator>bartduncan</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10120173</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10120173</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2011/01/25/query_5F00_tuning_5F00_key_5F00_terms.aspx#comments</comments><description>&lt;p&gt;A few days ago I was in the middle of writing up a quick post about a query performance problem I recently ran into. The writeup referenced &lt;em&gt;predicate selectivity&lt;/em&gt;, and I found myself wondering whether everyone who came across the post would have a good intuitive understanding of what that referred to. Just in case, I thought I'd do a quick overview of some terms that you'll definitely run across if you're doing much query tuning.&amp;nbsp;You can find this information in other forms elsewhere on the Internet, but these are practical descriptions that I've found to be useful when introducing people to query tuning concepts.&amp;nbsp;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="font-size: large;"&gt;Density &lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;Density, when used to describe the data in a column, is a measure of how often duplicate values occur in that column. Another way to think of density is as a measure of the uniqueness of the data in a column: high density --&amp;gt; less unique data. Density values range from 0 to 1.0.&amp;nbsp; There are different (but equivalent) ways to think of density.&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;Density = 1/[# of distinct values in a column] &lt;br /&gt;Density = Avg. number of duplicates for a given value / Total row count&lt;/p&gt;
&lt;p&gt;Consider a Customers table with a CountryID column and a RegionID column. Suppose that 100 different CountryID values are present in the table, and 1000 different RegionIDs are present. The density of the CountryID column would be higher than the density of the RegionID column because a typical country is less unique than a typical Region. Note that this is an overall characterization of a range of values. There may be some regions&amp;nbsp;that occur more frequently than some countries -- a U.S.-based company might have more customers in California than in Estonia, for example.&amp;nbsp; But CountryID still has higher density than RegionID simply because the &lt;em&gt;average &lt;/em&gt;country is represented in more rows than the &lt;em&gt;average &lt;/em&gt;region.&lt;/p&gt;
&lt;p&gt;Density of the CountryID column:&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;Density = 1/[# of distinct values in&amp;nbsp;the column] &lt;br /&gt;Density = 1/100 = 0.01&lt;/p&gt;
&lt;p&gt;Note that the density of 0.01, or 1%, corresponds to the percentage of the total rows that would be returned by a query for a single value.&amp;nbsp; In other words, an average country makes up 1% of the table.&amp;nbsp; if there were 10,000 rows in the table, there were be 100 rows (1% of 10,000) per country, on average.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Density of the RegionID column:&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;Density = 1/[# of distinct values in a column] &lt;br /&gt;Density = 1/1000 = 0.001&lt;/p&gt;
&lt;p&gt;The density of the CountryID column (0.01) is higher than the density of the RegionID column (0.001) because the CountryID column is less unique.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Q: What is the density of a column that contains only a single value repeated in every row? &lt;br /&gt;&lt;/em&gt;A: 1.0 -- the highest possible density.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Q: What is the density of a unique column, such as an IDENTITY or primary key column? &lt;/em&gt;&lt;br /&gt;A: 1/[row count]&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hands-On Example &lt;br /&gt;&lt;/strong&gt;Here is a script to create the scenario described above.&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;use tempdb&lt;br /&gt;go&lt;br /&gt;create table customers (customerid int identity, country varchar(10), region varchar(10))&lt;br /&gt;go&lt;br /&gt;set nocount on&lt;br /&gt;begin tran&lt;br /&gt;declare @x int&lt;br /&gt;set @x = 1&lt;br /&gt;while (@x &amp;lt;=10000)&lt;br /&gt;begin&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; insert into customers (country, region) values ('Country' + convert (varchar, @x % 100), 'Region' + convert (varchar, @x % 1000))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set @x = @x + 1&lt;br /&gt;end&lt;br /&gt;while @@trancount &amp;gt; 0 commit tran&lt;br /&gt;go&lt;br /&gt;create statistics stats_country on customers (country) with fullscan&lt;br /&gt;create statistics stats_region on customers (region) with fullscan&lt;br /&gt;create statistics stats_country_region on customers (country, region) with fullscan&lt;br /&gt;go&lt;br /&gt;dbcc show_statistics (customers, stats_country)&lt;br /&gt;dbcc show_statistics (customers, stats_region)&lt;br /&gt;go&lt;/p&gt;
&lt;p&gt;Run this and check out the DBCC SHOW_STATISTICS output for the stats on the [country] and [region] columns.&amp;nbsp; In the SHOW_STATISTICS output you'll see a value called "&lt;strong&gt;All density&lt;/strong&gt;".&amp;nbsp; This should match the values predicted in the calculations described above: Country and Region densities of 0.01 and 0.001, respectively.&amp;nbsp; (TIP: you should generally use "&lt;strong&gt;All density&lt;/strong&gt;" and disregard the "&lt;strong&gt;Density&lt;/strong&gt;" value in SHOW_STATISTICS's first resultset -- the value in the "Density" column is generally not used by the query optimizer.)&amp;nbsp; If the statistics are multi-column as in the [stats_country_region] statistics created in this example, an&amp;nbsp;&lt;strong&gt;All density&lt;/strong&gt;&amp;nbsp;value is also maintained for combinations of columns; this can help the optimizer make more accurate predictions when a query includes a filter on more than one column.&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Updated&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Rows&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Rows Sampled&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Steps&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Density&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Average Key Length&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;String Index&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Filter Expression&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Unfiltered Rows&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;stats_country&lt;/td&gt;
&lt;td&gt;Jan 25 2011 10:14AM&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;55&lt;/td&gt;
&lt;td&gt;0.01&lt;/td&gt;
&lt;td&gt;8.9&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;All density&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Average Length&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Columns&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;span style="background-color: #ffff00;"&gt;0.01&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;8.9&lt;/td&gt;
&lt;td&gt;country&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="font-size: large;"&gt;Predicate &lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;A predicate is an expression that evaluates to True or False. In SQL you encounter predicates in joins and in WHERE or HAVING clauses, where they are used to either filter out or qualify rows. Here's a query with two filter predicates:&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;SELECT * &lt;br /&gt;FROM table1 &lt;br /&gt;WHERE column1 &amp;gt; 20 AND column2 IS NULL&lt;/p&gt;
&lt;p&gt;And a query with one join predicate:&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;SELECT * &lt;br /&gt;FROM table1 &lt;br /&gt;INNER JOIN table2 ON table1.column1 = table2.column1&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="font-size: large;"&gt;Selectivity &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;Selectivity is also a measure of uniqueness, of sorts. High selectivity implies high uniqueness, or a low number of matching values. Low selectivity implies a low uniqueness, or a high percent of matches.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Selectivity is most commonly used to describe a predicate. Returning to the example used to describe density, consider a Customers table with a Region column. Suppose that most of the company's customers are in RegionA, and only a few are in RegionB. The predicate "Region='RegionB'" would be described as &lt;em&gt;more selective &lt;/em&gt;because it returns a smaller percentage of the rows in the table. (As an aside, this kind of uneven distribution of data within a column is an example of &lt;em&gt;data skew&lt;/em&gt;.)&amp;nbsp; The estimated selectivity of a predicate is essential when the optimizer is evaluating possible plans.&amp;nbsp;It will affect things like join order (all other things equal, the most selective join operation is generally performed first) and whether SQL chooses to do a table scan or an index seek followed by a bookmark lookup.&lt;/p&gt;
&lt;p&gt;Selectivity for a filter predicate against a base table can be calculated as "[# rows that pass the predicate]/[# rows in the table]".&amp;nbsp; If the predicate passes all rows in the table, its&amp;nbsp;selectivity is 1.0.&amp;nbsp; If it disqualifies all rows, its selectivity is 0. (This can be confusing.&amp;nbsp; Note that 0.000001 reflects a &lt;em&gt;high selectivity &lt;/em&gt;even though the number is small, while 1.0 is &lt;em&gt;low selectivity &lt;/em&gt;even though the number is higher.)&lt;/p&gt;
&lt;p&gt;The optimizer often uses the histogram steps in column statistics to estimate the selectivity of a predicate.&amp;nbsp; Re-run the sample script above, and note the third and final resultset in the "DBCC SHOW_STATISTICS(customers, stats_country)" output.&amp;nbsp; A single row in this resultset represents a step in a histogram.&amp;nbsp; Each histogram step summarizes the density of&amp;nbsp;all rows in the table that have a [country] value that is equal to or less than that row's RANGE_HI_KEY value, but greater than the RANGE_HI_KEY of the preceding histogram step. The snippet below shows the first 4 rows.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;RANGE_HI_KEY&lt;/td&gt;
&lt;td&gt;RANGE_ROWS&lt;/td&gt;
&lt;td&gt;EQ_ROWS&lt;/td&gt;
&lt;td&gt;DISTINCT_RANGE_ROWS&lt;/td&gt;
&lt;td&gt;AVG_RANGE_ROWS&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Country0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Country1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Country11&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Country13&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Take the third row in the histogram as an example. This step summarizes all rows with&amp;nbsp;a country value&amp;nbsp;greater than 'Country1' (the RANGE_HI_KEY for the preceding step),&amp;nbsp;but less than or equal to 'Country11' (this step's RANGE_HI_KEY).&amp;nbsp; There are 100 rows with exactly the value 'Country11' (EQ_ROWS), and&amp;nbsp;100 rows with some other value in this range (RANGE_ROWS).&amp;nbsp;&amp;nbsp; The DISTINCT_RANGE_ROWS value of 1 tells us that those 100 other RANGE_ROWS are made up of exactly one other distinct [country] value.&amp;nbsp; (These happen to be the rows with a country value of 'Country10', although that information isn't present in the statistics.)&amp;nbsp; I've often thought that the DISTINCT_RANGE_ROWS column was poorly-named; a better name might be "DISTINCT_RANGE_&lt;strong&gt;VALUES&lt;/strong&gt;" because it reports a count of distinct values, not rows.&amp;nbsp; The AVG_RANGE_ROWS column reports the average number of rows (100) for each of the (1) distinct values in the range.&amp;nbsp; Note that by dividing RANGE_ROWS by&amp;nbsp;DISTINCT_RANGE_ROWS, you calculate &lt;em&gt;step density&lt;/em&gt;, a measure of the uniqueness of an average value within the range of values described by that histogram step.&lt;/p&gt;
&lt;p&gt;Some simple examples might help&amp;nbsp;illustrate how the optimizer uses this data to estimate selectivity.&amp;nbsp; For the query below, the optimizer's task is to guess the selectivity of a predicate like "country='Country11'".&amp;nbsp; It will look for&amp;nbsp;the "Country11" value in the&amp;nbsp;histogram on the country column, and will find that this value happens to be one of the RANGE_HI_KEY values (histogram step endpoints).&amp;nbsp;&amp;nbsp;&amp;nbsp;As a result, it can use EQ_ROWS -- the number of rows observed with this exact value. The estimated selectivity of the predicate [country]='Country11' is 100/10,000 (EQ_ROWS / [table rowcount]), or 0.01.&amp;nbsp; The actual selectivity is also 0.01, meaning that the optimizer's&amp;nbsp;estimated selectivity turned out to be accurate.&amp;nbsp;&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Actual Selectivity&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;Est. Selectivity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;... WHERE [country]=&lt;span style="background-color: #ffff00;"&gt;'Country11'&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;0.1&lt;/td&gt;
&lt;td&gt;0.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For the query below the search value 'Country10' is not a RANGE_HI_KEY -- instead, the value falls somewhere in the middle of a range of values that is summarized by a histogram step.&amp;nbsp; This means that the optimizer will need to use step density, which implies an assumtion that the search value is typical of the values in its histogram step. The&amp;nbsp;estimated&amp;nbsp;number of rows with a given value in this historgram step is AVG_RANGE_ROWS, which is 100 for this histogram step.&amp;nbsp; That means that the estimated and actual selectivity are both 0.01 (100 / 10,000).&amp;nbsp;&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Actual Selectivity&lt;/td&gt;
&lt;td&gt;Est. Selectivity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;... WHERE [country]=&lt;span style="background-color: #ffff00;"&gt;'Country10'&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;0.1&lt;/td&gt;
&lt;td&gt;0.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here's a more interesting example. Note that the optimizer's guessed selectivity is wrong: it thought that 100 rows would be returned (selectivity of 0.1 * 10000 rows in the table), but in reality no rows passed the filter predicate.&amp;nbsp;The search value 'Country10xyz' falls within the same histogram step used in the prior query, so the optimizer guessed the number of rows that would be returned using the exact same calculation as in the prior example. This isn't a bug; it's just a reflection of the fact that statistics can only provide an imperfect summary of&amp;nbsp;the data in a table. You can read more about this &lt;a href="http://blogs.msdn.com/b/bartd/archive/2006/07/25/limited-statistics-granularity.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Actual Selectivity&lt;/td&gt;
&lt;td&gt;Est. Selectivity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;..&amp;nbsp;WHERE&lt;span style="background-color: #ffffff;"&gt; [country]=&lt;span style="background-color: #ffff00;"&gt;'Country10xyz'&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The information exposed by DBCC SHOW_STATISTICS can give you a better understanding of why the optimizer thinks that a particular plan will be cheaper than other plans. This is just a quick overview; for a much more detailed description SHOW_STATISTICS output and SQL statistics in general, check out the whitepaper "Statistics Used by the Query Optimizer in Microsoft SQL Server 2008" @ &lt;a href="http://msdn.microsoft.com/en-us/library/dd535534(v=sql.100).aspx"&gt;http://msdn.microsoft.com/en-us/library/dd535534(v=sql.100).aspx&lt;/a&gt;.&amp;nbsp; (If you haven't ever read this paper, it's a very good read.)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="font-size: large;"&gt;Cardinality &lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;All of this brings us finally to cardinality, which is kind of "where the rubber hits the road" when you are&amp;nbsp;trying to understand why a query plan is slow.&lt;/p&gt;
&lt;p&gt;For our purposes, cardinality can be thought of as the number of rows returned by a query operator.&amp;nbsp; (A few examples of physical query operators that you will probably recognize: Index Seek, Nested Loop Join, Filter)&amp;nbsp; The cardinality of an operator like a Filter is determined by multiplying the selectivity of that operator -- that is, the % of its input rows that it will pass --&amp;nbsp;by the cardinality (rowcount) of the operator's child.&amp;nbsp; If an operator receives 500 rows from its child operator and has a selectivity of 0.3 (30%), it has a cardinality of 500*0.3, or 150 rows.&lt;/p&gt;
&lt;p&gt;Each operator in a query plan has an &lt;em&gt;estimated cardinality &lt;/em&gt;(the number of rows the optimizer guessed that the operator would return) and an &lt;em&gt;actual cardinality&lt;/em&gt; (the number of rows that the operator returned in the real world).&amp;nbsp; You can see both by running a query with "SET STATISTICS PROFILE ON".&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Rows&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Executes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;EstimateRows&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;EstimateExecutions&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;SELECT c1 FROM t1 WHERE c1 = 'xyz'&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2210&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&amp;nbsp; |--Filter([t1].[c1] = 'xyz')&amp;nbsp;&lt;/td&gt;
&lt;td&gt;21.0&lt;/td&gt;
&lt;td&gt;1.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2742&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Index Scan(OBJECT:([t1].[idx1]))&lt;/td&gt;
&lt;td&gt;2971&lt;/td&gt;
&lt;td&gt;1.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Sometimes the query optimizer cannot accurately predict the number of rows that a given operator will return. This can prevent SQL from estimating the cost of a query plan correctly, which can lead to the selection of a suboptimal plan. Cardinality estimation errors&amp;nbsp;are one of the most common causes of slow query plans in SQL Server, so it is very important to know how to identify cardinality estimation problems in a query plan. (That's a little beyond the point of this post, though -- here I'm&amp;nbsp;just defining&amp;nbsp;some terms.) In the plan above, you can see that SQL made a cardinality estimation error: it guessed that the filter would be very selective and would only pass 21 of 2971 rows, when&amp;nbsp;in reality almost all of the rows passed the filter.&lt;/p&gt;
&lt;p&gt;The optimizer has a number of ways&amp;nbsp;to estimate cardinality, none of which are completely foolproof.&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If the predicate is&amp;nbsp;simple like "column=123" and if the search value happens to be a histogram endpoint (RANGE_HI_KEY), then EQ_ROWS can be used for a very accurate estimate.&lt;/li&gt;
&lt;li&gt;If the search value happens to fall between two step endpoints, then the average density of values in that particular histogram step is used to estimate predicate selectivity and operator cardinality.&lt;/li&gt;
&lt;li&gt;If the specific search value is not known at compile time, the next best option is to use average column density ("All density"), which can be used to calculate the number of rows that will match an average value in the column.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;In some cases none of the above are possible and optimizer has to resort to a "&lt;em&gt;magic number"&lt;/em&gt;-based estimate.&amp;nbsp;&amp;nbsp;For example, it might&amp;nbsp;make a totally&amp;nbsp;blind guess that 10% of the rows will be returned, where the "10%" value would be hardcoded in the optimizer's code rather than being derived from statistics.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In most scenarios option #1 is the ideal, but because most values aren't histogram endpoints, option #2 is the most frequently-used method for simple filter and join predicates.&amp;nbsp;Option #4 usually provides the worst estimates, and is only used when there is no other alternative. Thankfully, most of the time when a magic number is used, this is a side effect of poor query design; by rewriting the query you can enable a more accurate cardinality estimate based on either average column density or histogram lookup.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10120173" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>Living with SQL's 900 Byte Index Key Length Limit</title><link>http://blogs.msdn.com/b/bartd/archive/2011/01/06/optionsforindexedlookupsoflongvalues.aspx</link><pubDate>Thu, 06 Jan 2011 21:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10112677</guid><dc:creator>bartduncan</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10112677</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10112677</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2011/01/06/optionsforindexedlookupsoflongvalues.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;We recently had a situation where we needed to interface with an external non-relational data source that happened to use really long strings to identify entity instances. These identity strings had theoretically unbounded length, but in practice they were never more than 1000-2000 characters. The database layer needed to accept an identifier of this sort and use it to retrieve some information from the relational store that was related to that object. I&amp;rsquo;ll call the entity type &amp;ldquo;widget&amp;rdquo;, and the long string identifier from the external system the &amp;ldquo;external key&amp;rdquo;. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;We could just store the external key in the database table and use it to locate the correct widget, but we couldn&amp;rsquo;t afford to scan the widget table for every lookup. The number of rows in the widget table was expected to be large, so we needed the lookup to be indexed. Unfortunately, SQL has a limit of 900 bytes for the keys in each index, and the external key values can be longer than this. If you create an index on combination of columns that could contain more than 900 bytes, you get this warning: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color: red;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color: red;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Warning! The maximum key length is 900 bytes. The index 'xyz' has maximum length of 4000 bytes. For some combination of large values, the insert/update operation will fail.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color: red;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;Then if you try to insert a row into the table that has more than 900 bytes in the indexed columns, it will fail with this error: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color: red;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Msg 1946, Level 16, State 3, Line 11&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color: red;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Operation failed. The index entry of length 1501 bytes for the index 'xyz' exceeds the maximum length of 900 bytes.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;Even if this restriction within SQL didn&amp;rsquo;t exist, I wouldn&amp;rsquo;t ever think of creating a table with a &amp;gt;1KB primary key. A surrogate primary key like an IDENTITY column or a GUID/UNIQUEIDENTIFIER column should be used, instead. You'd use the surrogate as your primary key, and all lookups and foreign keys within the data tier should use the surrogate.&amp;nbsp;But the external system doesn&amp;rsquo;t know anything about the surrogate keys used in the data tier; the only identifier that the external system can pass to the database is that &amp;gt;900 byte external key value. So while a surrogate for primary key is a good idea here, it doesn&amp;rsquo;t solve the immediate problem &amp;ndash; we still need to be able to do an efficient (indexed) lookup of an external key value in the database, even though a column that could store the external key values would be too large to index. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;If you find yourself in a similar situation, here are some alternatives to consider: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;A. Change the external system to use an identifier that is always less than 900 bytes long.&lt;/b&gt; Examine the entity&amp;rsquo;s attributes: is there some combination of attributes that together make up a candidate key (in other words, is there some combination of other attributes that is guaranteed to be unique for each entity instance)?&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Even if the external system doesn&amp;rsquo;t internally use those columns as an identifier, it could still pass them to the data tier when it wanted to look up a widget. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;In the database, you&amp;rsquo;d create a UNIQUE index on this column or combination of columns. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;B. Require non-key columns as additional criteria for each lookup&lt;/b&gt;. For example, suppose a widget has a &amp;ldquo;Name&amp;rdquo; attribute that is always short enough to be indexed. Name is not guaranteed to be globally unique, so neither the external system nor the relational model can use this as a key. But if the column is fairly selective it may be sufficient to give you lookups that are efficient enough that it&amp;rsquo;s almost like doing an index seek on the external key. In this example you would index the [name] column in the database, and use a routine for lookups like this. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 9pt; mso-no-proof: yes;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 9pt; mso-no-proof: yes;"&gt; &lt;span style="color: blue;"&gt;PROC&lt;/span&gt; usp_get_widet @external_key &lt;span style="color: blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;MAX&lt;/span&gt;&lt;span style="color: gray;"&gt;),&lt;/span&gt; @name &lt;span style="color: blue;"&gt;NVARCHAR&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;60&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT&lt;/span&gt; &lt;span style="color: gray;"&gt;*&lt;/span&gt; &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM&lt;/span&gt; widgets &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE&lt;/span&gt; external_key &lt;span style="color: gray;"&gt;=&lt;/span&gt; @external_key &lt;span style="background: yellow; color: gray; mso-highlight: yellow;"&gt;AND&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; name &lt;span style="color: gray;"&gt;=&lt;/span&gt; @name&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;SQL will use a seek on the non-clustered [name] index to narrow the results down to a tiny percentage of the table (hopefully no more than a few rows), then it will do a residual filter to get rid of all but the single row that matches the external key value. Note that you can add more than one supplemental search column if necessary to increase the selectivity of the non-key index seek. Also be sure to note that you still need to do the filter on the [external_key] column. That predicate won&amp;rsquo;t be satisfied by an index seek, but it&amp;rsquo;s still necessary for correctness. Consider making the full external key INCLUDEd in the nonclustered index, to minimize the number of bookmark lookups the engine needs to do to find the single matching row. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;One obvious downside to this approach is that the additional required search criteria complicate the interface between the database and the external component. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;C. Truncate the external key to 900 bytes and index the truncated portion of the key&lt;/b&gt;. For example, your widget table might look like this: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 9pt; mso-no-proof: yes;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 9pt; mso-no-proof: yes;"&gt; &lt;span style="color: blue;"&gt;TABLE&lt;/span&gt; widgets&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;br /&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;surrogate_key &lt;span style="color: blue;"&gt;INT&lt;/span&gt; &lt;span style="color: blue;"&gt;PRIMARY KEY IDENTITY&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; &lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;external_key &lt;span style="color: blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;MAX&lt;/span&gt;&lt;span style="color: gray;"&gt;),&lt;/span&gt; &lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;external_key_fragment &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;LEFT &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;external_key&lt;span style="color: gray;"&gt;,&lt;/span&gt; 900&lt;span style="color: gray;"&gt;),&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: gray;"&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;You&amp;rsquo;d create a (nonunique) index on the [external_key_fragment] column, and your lookup procedure might look something like this: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 9pt; mso-no-proof: yes;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 9pt; mso-no-proof: yes;"&gt; &lt;span style="color: blue;"&gt;PROC&lt;/span&gt; usp_get_widet @external_key &lt;span style="color: blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;MAX&lt;/span&gt;&lt;span style="color: gray;"&gt;) &lt;/span&gt;&lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT&lt;/span&gt; &lt;span style="color: gray;"&gt;*&lt;/span&gt; &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM&lt;/span&gt; widgets &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE&lt;/span&gt; external_key &lt;span style="color: gray;"&gt;=&lt;/span&gt; @external_key &lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="background: yellow; color: gray; mso-highlight: yellow;"&gt;AND&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; external_key_fragment &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;LEFT &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;@external_key&lt;span style="color: gray;"&gt;,&lt;/span&gt; 900&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;Of course, this assumes that some predictable 900-byte portion of the external key is reasonably selective. If it&amp;rsquo;s possible for a large portion of the rows to have the same initial 900 bytes (assuming the fragment is generated in the way shown above), then this won&amp;rsquo;t help any &amp;ndash; the optimizer will correctly estimate that a scan would be more efficient than a seek, and you&amp;rsquo;ll still end up scanning the entire table for each lookup. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;D. Break up or parse the external key into its component parts, and index a selective subset of the parts.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;This assumes that the long external key is really a serialized version of a composite key. It must have some underlying structure, and it must be practical to break the key into its child parts within the data tier. It also assumes that some portion of this structured key is guaranteed to be unique enough to support an efficient index seek that only returned a few rows. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;For example, suppose that the external key was string that actually encoded a hierarchy like &amp;ldquo;RootIdentifierX\ChildIdentifierY\GrandChildIdentifierZ\...&amp;rdquo;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If you&amp;rsquo;re willing to push knowledge of the structure of this serialized key down into the data tier, the database could parse this apart and use the identifiers for the first few levels of the hierarchy to do a secondary lookup that could be satisfied via an index seek. You&amp;rsquo;d need to store those parts of the external key in separate columns (so they could be index) in addition to the full external key. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Once you&amp;rsquo;ve indexed the portions that you can, consider adding the remainder as an INCLUDEd column in the index; that would allow the index seek to locate the single matching row without resorting to a bookmark lookup. (It would also make it less likely that the optimizer would choose to scan the table because it guessed that a seek on the component parts wouldn&amp;rsquo;t be selective enough.) &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;E. Hash the external key and index the hash.&lt;/b&gt; This is essentially building a hash table and persisting it in the database. With this approach, your widgets table might look something like this: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 9pt; mso-no-proof: yes;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 9pt; mso-no-proof: yes;"&gt; &lt;span style="color: blue;"&gt;TABLE&lt;/span&gt; widgets&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;br /&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;surrogate_key &lt;span style="color: blue;"&gt;INT PRIMARY KEY IDENTITY&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;external_key &lt;span style="color: blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;MAX&lt;/span&gt;&lt;span style="color: gray;"&gt;),&lt;/span&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: green;"&gt;-- A computed column that hashes the external key (MD5 returns a&amp;nbsp;&lt;br /&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;-- &lt;/span&gt;128-bit hash).&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 9pt; mso-no-proof: yes;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;external_key_hash &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;CAST&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;HASHBYTES&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: red;"&gt;'MD5'&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; external_key&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;span style="color: blue;"&gt;VARBINARY&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;16&lt;span style="color: gray;"&gt;)),&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;The [external_key_hash] column would be indexed, and the lookup proc would do this: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 9pt; mso-no-proof: yes;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 9pt; mso-no-proof: yes;"&gt; &lt;span style="color: blue;"&gt;PROC&lt;/span&gt; usp_get_widet @external_key &lt;span style="color: blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;MAX&lt;/span&gt;&lt;span style="color: gray;"&gt;) &lt;/span&gt;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;SELECT&lt;/span&gt; &lt;span style="color: gray;"&gt;*&lt;/span&gt;&amp;nbsp;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;FROM&lt;/span&gt; widgets&amp;nbsp;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;WHERE&lt;/span&gt; external_key &lt;span style="color: gray;"&gt;=&lt;/span&gt; @external_key&amp;nbsp;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="background: yellow; color: gray; mso-highlight: yellow;"&gt;AND&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; external_key_hash&amp;nbsp;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;CAST&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;HASHBYTES&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: red;"&gt;'MD5'&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @external_key&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;span style="color: blue;"&gt;VARBINARY&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;16&lt;span style="color: gray;"&gt;));&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;It could be argued that the CAST(HASHBYTES(&amp;hellip;)) stuff should be hidden within a UDF (ditto for alternatives (C) and (D)): &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 9pt; mso-no-proof: yes;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 9pt; mso-no-proof: yes;"&gt; &lt;span style="color: blue;"&gt;FUNCTION&lt;/span&gt; dbo&lt;span style="color: gray;"&gt;.&lt;/span&gt;fn_hash_external_key&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;@urn &lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;max&lt;/span&gt;&lt;span style="color: gray;"&gt;))&lt;/span&gt; &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;RETURNS&lt;/span&gt; &lt;span style="color: blue;"&gt;VARBINARY&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;16&lt;span style="color: gray;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WITH&lt;/span&gt; &lt;span style="color: blue;"&gt;SCHEMABINDING&lt;/span&gt; &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AS&lt;/span&gt; &lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BEGIN&lt;br /&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;RETURN&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;HASHBYTES&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: red;"&gt;'MD5'&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @urn&lt;span style="color: gray;"&gt;);&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;END&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;The computed column and the lookup proc would both reference this function so that the details of the hash function lived in a single place. That would indeed be cleaner, but before taking that route be sure you&amp;rsquo;re aware of the potential problems with scalar UDF performance that are described at &lt;/span&gt;&lt;a href="http://www.developerfusion.com/community/blog-entry/8389063/tsql-scalar-functions-are-evil/"&gt;&lt;span style="font-family: Calibri; color: #0000ff; font-size: small;"&gt;http://www.developerfusion.com/community/blog-entry/8389063/tsql-scalar-functions-are-evil/&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt; and &lt;/span&gt;&lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2006/08/04/scalar-functions-inlining-and-performance-an-entertaining-title-for-a-boring-post.aspx"&gt;&lt;span style="font-family: Calibri; color: #0000ff; font-size: small;"&gt;http://sqlblog.com/blogs/adam_machanic/archive/2006/08/04/scalar-functions-inlining-and-performance-an-entertaining-title-for-a-boring-post.aspx&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;Of all of these, option (A) is the only one that&amp;nbsp;I find&amp;nbsp;very aesthetically pleasing.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;The others could work in a pinch, though.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Please drop me a comment if I&amp;rsquo;m overlooking some other alternative here. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;(Cross-posted &lt;a href="http://bartduncansql.wordpress.com/2011/02/26/living-with-sqls-900-byte-index-key-length-limit/"&gt;here&lt;/a&gt;)&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10112677" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category></item><item><title>Query Hash Statistics, a query cost analysis tool, now available for download</title><link>http://blogs.msdn.com/b/bartd/archive/2010/11/03/query-hash-statistics-a-query-cost-analysis-tool-now-available-for-download.aspx</link><pubDate>Wed, 03 Nov 2010 18:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10085599</guid><dc:creator>bartduncan</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10085599</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10085599</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2010/11/03/query-hash-statistics-a-query-cost-analysis-tool-now-available-for-download.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;Some time ago I described the &lt;/span&gt;&lt;a href="http://blogs.msdn.com/b/bartd/archive/2008/09/03/query-fingerprints-and-plan-fingerprints_3a00_-the-best-new-sql-2008-feature-you_2700_ve-never-heard-of.aspx"&gt;&lt;span style="font-size: small;"&gt;query fingerprint&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt; and query plan fingerprint (a.k.a. query hash / query plan hash) features that were added in SQL Server 2008. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;A fingerprint identifies all queries or all plans that have the same &amp;ldquo;shape&amp;rdquo;. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;With query fingerprints you can get the cumulative cost of all executions of a query &lt;i style="mso-bidi-font-style: normal"&gt;even if the query is non-parameterized&lt;/i&gt; and has different inline literal values for each execution. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Previously, the only way to get this type of query performance data was to capture a Profiler trace and run the trace through a post-processing tool like &lt;a href="http://support.microsoft.com/kb/944837"&gt;ReadTrace&lt;/a&gt; or &lt;a href="http://www.scalesql.com/cleartrace"&gt;ClearTrace&lt;/a&gt;. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Now you can do it with a few DMV queries.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;Query Hash Statistics is a free download that collects historical query and query plan fingerprint statistics and allows you easily&amp;nbsp;to view the data in SSMS and see the true cumulative cost of the queries in each of your databases. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;You can use the project as-is, or &amp;ndash; if you&amp;rsquo;re the tinkering type &amp;ndash; disassemble it and build your own custom query hash stats solution. (Source code is included.) &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;o:p&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;em&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;Download Query Hash Statistics @&lt;/span&gt;&lt;/b&gt;&lt;/em&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt; &lt;a href="http://code.msdn.microsoft.com/QueryHashStatistics"&gt;http://code.msdn.microsoft.com/QueryHashStatistics&lt;/a&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="color: black;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;strong&gt;Project Description: &lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Query Hash Statistics collects Microsoft SQL Server query execution statistics and uploads the data to a central warehouse. The solution also includes a set of reports to analyze and visualize this data within SQL Server Management Studio (SSMS). Query Hash Statistics differs from most other query cost analysis tools in that it builds on the "query fingerprint" and "query plan fingerprint" features that were added in SQL Server 2008. These features make it possible to calculate the cumulative cost of all executions of a query, even when the application doesn't parameterize its queries. A solution like Query Hash Statistics that builds on these features can do low-overhead query cost monitoring with a degree of accuracy that was impossible in prior releases of SQL Server. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;Because the solution relies on new SQL engine features that were added in SQL Server 2008, it only works with SQL Server 2008 and later. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="color: black;"&gt;Source code is included, to demonstrate how to build similar solutions. In addition to highlighting the engine's query and query plan fingerprint features, the project shows how to use extensibility features in Data Collector and SSMS. By following the patterns demonstrated in this project, you can learn how to:&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;strong&gt;Identify cumulative query cost without a profiler trace &lt;/strong&gt;using the SQL Server "plan fingerprint" and "query fingerprint" features. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;strong&gt;Use Data Collector to upload data from SQL&lt;/strong&gt; &lt;strong&gt;DMVs&lt;/strong&gt; to a central management database. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;strong&gt;Build reports that can be hosted within SQL Server Management Studio&lt;/strong&gt; (SSMS) to analyze the data in your central management database.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-10-08-55-99/rpt_5F00_query_5F00_hash_5F00_thumbnails.png" /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;Check it out, and let us know about your experiences in the Discussions tab on the MSDN Code Gallery project. If you have a suggestion or think you've found a bug, please open a new issue under the Issue Tracker tab. Thanks! &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Segoe UI','sans-serif'; color: black;"&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="color: #ffffff;"&gt;Additional query keywords: QueryHashStats Query Hash Stats query performance query troubleshooting cumulative query cost analysis Microsoft SQL Server MSSQL MS SQL T-SQL TSQL Transact-SQL query plan cost cumulative plan cost find expensive queries find slow queries DMV dm_exec_query_stats plan fingerprints query fingerprints plan hash query hash query_plan_hash query_hash dm_exec_requests top queries most expensive queries&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10085599" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-08-55-99/rpt_5F00_query_5F00_hash_5F00_thumbnails.png" length="93198" type="image/x-png" /><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Procedure+Cache/">SQL Procedure Cache</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Capacity+Planning/">SQL Capacity Planning</category></item><item><title>T-SQL UDTs.  (Huh!)  What are they good for? </title><link>http://blogs.msdn.com/b/bartd/archive/2010/08/25/t-sql-udts-what-are-they-good-for.aspx</link><pubDate>Wed, 25 Aug 2010 07:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10053969</guid><dc:creator>bartduncan</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10053969</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10053969</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2010/08/25/t-sql-udts-what-are-they-good-for.aspx#comments</comments><description>&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;(The title of this post might seem a little inflammatory, but it&amp;rsquo;s not just a troll -- I really do think that people should seriously question whether it&amp;rsquo;s wise to use T-SQL User Defined Data Types.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Mostly, though, I just like that Edwin Starr song.)&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Let me start this post off by saying that I understand why user-defined data types (UDTs) in T-SQL seem alluring.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;At first glance, you might expect UDTs to give you some very nice benefits, such as: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;span style="text-decoration: underline;"&gt;Expected Benefit #1&lt;/span&gt;:&lt;/b&gt; Suppose you have a field that stores percentages values (0-100).&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;In T-SQL you might choose the &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint &lt;/b&gt;data type for such a field, but this isn&amp;rsquo;t the only data type that you could have chosen to store percentage values.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If there is more than one table that contains a percentage-type field, you could end up with the same sort of value being stored using different data types.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;By creating a UDT called &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT&lt;/b&gt;, you can avoid the need for database developers to memorize &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Percentages should be stored using &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint&lt;/b&gt;&lt;/i&gt;.&amp;rdquo; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;span style="text-decoration: underline;"&gt;Reality&lt;/span&gt;: &lt;/b&gt;This is based on the idea that it&amp;rsquo;s easier for a developer to memorize &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Percentages are stored using a UDT named &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT&lt;/b&gt;&lt;/i&gt;&amp;rdquo; than it is to memorize &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Percentages are stored using system type &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint&lt;/b&gt;&lt;/i&gt;.&amp;rdquo;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;The problem is that you have to know the underlying data type in order to deal with the column correctly.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;For example, a database developer that was retrieving a &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT&lt;/b&gt; (UDT) field in a VB.NET or C# app would need to know what .Net data type should be used to retrieve values from the field.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;A thoughtful dev would wonder: &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Maybe I should store this &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT &lt;/b&gt;value in a &lt;b style="mso-bidi-font-weight: normal;"&gt;byte&lt;/b&gt; variable, which can hold values from 0 to 255.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;But wait: Can a &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT &lt;/b&gt;column hold a negative percentage?&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If so, I&amp;rsquo;ll need to use the &lt;b style="mso-bidi-font-weight: normal;"&gt;short&lt;/b&gt; data type, which can represent negative integers.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Hold on a second! &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;I wonder if this &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT &lt;/b&gt;data&lt;b style="mso-bidi-font-weight: normal;"&gt; &lt;/b&gt;type can store fractional percentages?&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If so, I&amp;rsquo;d need to retrieve the column value into a &lt;b style="mso-bidi-font-weight: normal;"&gt;decimal &lt;/b&gt;variable...&lt;/i&gt;&amp;rdquo;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;In other words, if you don&amp;rsquo;t use a UDT, the application developer has to memorize &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Percentages are stored using system type &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint&lt;/b&gt;&lt;/i&gt;.&amp;rdquo;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;But if you use a T-SQL UDT the application developer has to know both &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Percentages are stored using a UDT named &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT&lt;/b&gt;&lt;/i&gt;&amp;rdquo; &lt;span style="text-decoration: underline;"&gt;and&lt;/span&gt; &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Percentages are stored using system type &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint&lt;/b&gt;&lt;/i&gt;.&amp;rdquo;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;The attempt to make the app developer&amp;rsquo;s life easier can backfire and actually made it a little harder.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;(It&amp;rsquo;s worth mentioning that SQLCLR UDTs don&amp;rsquo;t have this same limitation.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;App developers can reference an assembly that gives them a client-side data type that corresponds to the server-side SQLCLR UDT, so the developer doesn&amp;rsquo;t have to know the details of the UDT&amp;rsquo;s implementation in order to use it.) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;span style="text-decoration: underline;"&gt;Expected Benefit #2&lt;/span&gt;: &lt;/b&gt;A &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint&lt;/b&gt; can store 0-255, but your percentage fields should only allow 0-100.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;You can restrict a column&amp;rsquo;s domain by defining a CHECK constraint on the column.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;But if there are several percentage type columns in several different tables, you&amp;rsquo;d need to define identical CHECK constraints on each column.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Wouldn&amp;rsquo;t it be nice if you could attach the constraint to the &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT&lt;/b&gt; user-defined data type, so that it would apply automatically to each column that used this UDT?&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;span style="text-decoration: underline;"&gt;Reality&lt;/span&gt;:&lt;/b&gt; This would be a real benefit of T-SQL UDTs.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Unfortunately, the ability to attach a rule to a UDT has been officially deprecated, so you shouldn&amp;rsquo;t use this functionality.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Since you can no longer attach custom domain constraints or other business rules to the data type, a T-SQL UDT is really nothing more than an alias for a system data type.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Sorry! &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;span style="text-decoration: underline;"&gt;Expected Benefit #3&lt;/span&gt;: &lt;/b&gt;Suppose that your requirements have changed, and now you need to be able to store fractional percentages.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;You decide to store percentages as &lt;b style="mso-bidi-font-weight: normal;"&gt;decimal(3,2)&lt;/b&gt; instead of &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint&lt;/b&gt;.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If all percentage columns were defined as &lt;b style="mso-bidi-font-weight: normal;"&gt;tinyint&lt;/b&gt;, you&amp;rsquo;d have to go around to all of the columns and modify each column&amp;rsquo;s data type separately.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;And to change a column&amp;rsquo;s data type you must first drop any indexes or constraints that reference the column.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;What a pain.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Wouldn&amp;rsquo;t it be easier if you could just change the &lt;b style="mso-bidi-font-weight: normal;"&gt;PERCENT&lt;/b&gt; UDT definition in one place? &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;span style="text-decoration: underline;"&gt;Reality&lt;/span&gt;: &lt;/b&gt;T-SQL doesn&amp;rsquo;t have an ALTER TYPE command for UDTs. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;To change a UDT definition you must do a DROP TYPE followed by CREATE TYPE.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;But of course you can&amp;rsquo;t drop a type that is being used in a column, so you have to do an awkward little dance to switch all of the columns that use the UDT to use the underlying system data type, instead (so they no longer reference the UDT), then drop and redefine the UDT, then ALTER the columns a second time to re-reference the new UDT.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl1 ALTER COLUMN foo_pct TINYINT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl2 ALTER COLUMN foo_pct TINYINT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl3 ALTER COLUMN foo_pct TINYINT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl4 ALTER COLUMN foo_pct TINYINT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DROP TYPE PERCENT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CREATE TYPE PERCENT FROM DECIMAL(3,2);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl1 ALTER COLUMN foo_pct PERCENT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;ALTER TABLE tbl2 ALTER COLUMN foo_pct PERCENT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl3 ALTER COLUMN foo_pct PERCENT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl4 ALTER COLUMN foo_pct PERCENT;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;And if any indexes or constraints reference any of these columns, you&amp;rsquo;d still have to drop those before the first ALTER COLUMN, and recreate them afterward.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If you have to write DDL that redefines the columns using the base data type that the UDT is supposed to hide, where&amp;rsquo;s the abstraction benefit?&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;More importantly, if you didn&amp;rsquo;t use UDTs this same change can be done with less than half the code: &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl1 ALTER COLUMN foo_pct DECIMAL(3,2);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;ALTER TABLE tbl2 ALTER COLUMN foo_pct DECIMAL(3,2);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl3 ALTER COLUMN foo_pct DECIMAL(3,2);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ALTER TABLE tbl4 ALTER COLUMN foo_pct DECIMAL(3,2);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;So there you have the reasons why I avoid using T-SQL user-defined types in my own code.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;It&amp;rsquo;s not that UDTs are all that bad; it&amp;rsquo;s just that in my experience they fall short of their promise, and they can make some things even more cumbersome than the same task would be without the use of UDTs.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If you think I&amp;rsquo;m overlooking some concrete benefit of UDTs, I&amp;rsquo;d love to hear from you in the comments section.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If you do choose to use UDTs, just be sure you aren&amp;rsquo;t basing the decision to use them on inaccurate assumptions about the benefits they&amp;rsquo;ll provide.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&lt;span style="text-decoration: underline;"&gt;An important caveat&lt;/span&gt;&lt;/b&gt;: The above applies to simple T-SQL UDTs.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;You can also create a user-defined type in SQL Server using SQLCLR and .Net.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;While a T-SQL UDT is never anything more than a simple alias for one of the system data types, a SQLCLR UDT can be a truly custom data type.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;With SQLCLR you can implement complex types, embed custom business logic like domain constraints or validation rules into the type, and so on.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;In other words, with SQLCLR UDTs you can actually realize the benefit that I called &amp;ldquo;&lt;i style="mso-bidi-font-style: normal;"&gt;Expected Benefit #2&lt;/i&gt;&amp;rdquo;, above.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;As mentioned earlier, you also get much of &lt;i style="mso-bidi-font-style: normal;"&gt;Expected Benefit #1&lt;/i&gt;.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;SQLCLR UDTs still have the limitations described in #3, but the ability to extend SQL Server in the way that SQLCLR allows is very powerful.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;That benefit could easily compensate for the issue discussed in #3, if the task you are trying to accomplish would be much harder to implement using one of the system data types.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10053969" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category></item><item><title>SQL Server Sizing Resources</title><link>http://blogs.msdn.com/b/bartd/archive/2010/06/16/sql-server-sizing-resources.aspx</link><pubDate>Thu, 17 Jun 2010 03:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10026244</guid><dc:creator>bartduncan</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10026244</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10026244</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2010/06/16/sql-server-sizing-resources.aspx#comments</comments><description>&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;Recently, I was asked whether Microsoft had any SQL Server hardware sizing tools.&amp;nbsp; The asker referred me to &lt;a href="http://www.sizinglounge.com"&gt;http://www.sizinglounge.com&lt;/a&gt;&amp;nbsp;as an example of what he was looking for.&amp;nbsp; (Sizing Lounge apparently allows you to select an app like SAP or Exchange, answer a couple of questions about things like concurrent user counts and desired CPU utilization levels, and receive a list of possible server configurations that are supposed to be able to handle the load.)&amp;nbsp; This isn't the first time I've fielded this question, so I decided to put my answer in a blog post.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;When you're trying to size a server that will be running a well-known application -- say, SAP, or Exchange -- you have a chance of getting a recommendation that is reasonably precise.&amp;nbsp; This is because the person or tool that is&amp;nbsp;making the sizing recommendation can make well-educated guesses based on knowledge of the way that users generally use that app, and the app's typical transaction costs.&amp;nbsp; But if you want non-app-specific guidance for an unknown or arbitrary database app, the problem gets much harder.&amp;nbsp; This is because a single business transaction from one application (say, &amp;ldquo;&lt;em&gt;log in&lt;/em&gt;&amp;rdquo;, or &amp;ldquo;&lt;em&gt;submit purchase order&lt;/em&gt;&amp;rdquo;) could be orders of magnitude cheaper or more expensive than the exact same type of business transaction in a different app.&amp;nbsp; In other words, there are limits to how precise application-independent sizing guidance can be for an unknown app when the&amp;nbsp;inputs are nothing more than simple metrics like # of concurrent users, queries/second, or transactions/sec.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: large;"&gt;&lt;strong&gt;Application-Specific Server Sizing&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;My first recommendation is to go to the application developer, not the RDBMS&amp;nbsp;developer, for the most meaningful sizing guidance.&amp;nbsp; I don't think that even Microsoft has explicit sizing guidance for every MS app that runs on SQL, but there are sizing tools or whitepapers that make hardware recommendations for many or most of our SQL apps.&amp;nbsp; A few examples: &lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;a href="http://technet.microsoft.com/en-us/library/cc298801.aspx"&gt;Sharepoint&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc966418.aspx"&gt;Reporting Services&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;a href="http://technet.microsoft.com/en-us/library/bb418778.aspx"&gt;Forefront&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb668966.aspx"&gt;Team Foundation Server&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;If you can't find explicit sizing guidelines for the application you'll be running on SQL, I'd try these alternatives,&amp;nbsp;listed in no particular order: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;Case studies from the ISV (case studies often include server hardware details and information about concurrent user counts, data volumes, etc)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;Post a query to online forums dedicated to the app (for&amp;nbsp;Microsoft apps you might try the &lt;a href="http://social.technet.microsoft.com/Forums/en-US/categories"&gt;TechNet forums&lt;/a&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;Contact the app ISV directly&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: large;"&gt;&lt;strong&gt;Application-Independent Server Sizing&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;You may come up empty-handed in your search for application-specific sizing guidance.&amp;nbsp; If the application was developed in-house or if you're sizing a server for an app that doesn't even exist yet, you'll definitely be in this situation.&amp;nbsp; &lt;span style="font-size: small;"&gt;Application-independent hardware sizing guides do exist.&amp;nbsp; The first type of resource in this category may not be&amp;nbsp;as simple to use as you might like, but the conclusions you'll come to will be more trustworthy.&amp;nbsp; &lt;span style="font-size: small;"&gt;The first two whitepapers below take the approach of giving you the tools you need to identify the key characteristics of&amp;nbsp;an existing application workload, then&amp;nbsp;providing you with a process to translate that into a set of hardware requirements.&amp;nbsp; The database sizing&amp;nbsp;tool will give you a fairly&amp;nbsp;accurate estimated database size, but to get it you must provide a lot of&amp;nbsp;schema details.&amp;nbsp; These&amp;nbsp;are not simple processes, so don't expect to have a set of server specs in 30 seconds.&amp;nbsp; But the complexity is inherent to the problem and can't be eliminated without sacrificing precision, so you can consider it time well spent.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;Disk subsystem sizing guide from the SQL CAT team: &lt;a href="http://msdn.microsoft.com/en-us/library/ee410782(SQL.100).aspx"&gt;&lt;span style="font-size: small;"&gt;http://msdn.microsoft.com/en-us/library/ee410782(SQL.100).aspx&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;Fast Track Data Warehouse 2.0 Architecture: &lt;a href="http://msdn.microsoft.com/en-us/library/dd459178.aspx"&gt;http://msdn.microsoft.com/en-us/library/dd459178.aspx&lt;/a&gt;&amp;nbsp;(not app-specific, but tailored to a particular class of application) &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;DataSizer tool (helps estimate database sizes based on your table schema):&amp;nbsp; &lt;/span&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=564c5704-d4f5-4ee8-9f3c-cb429499d075&amp;amp;displaylang=en"&gt;&lt;span style="font-size: small;"&gt;http://www.microsoft.com/downloads/details.aspx?FamilyID=564c5704-d4f5-4ee8-9f3c-cb429499d075&amp;amp;displaylang=en&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt; &amp;ndash; This was written for SQL 7.&amp;nbsp; The estimates won&amp;rsquo;t be quite as accurate for more recent SQL versions, but in most cases they should be pretty close unless you&amp;rsquo;re using Enterprise Edition SQL features like compression.&amp;nbsp; (For details about performing this calculation by hand in SQL 2008 R2, see the "&lt;a href="http://msdn.microsoft.com/en-us/library/ms187445.aspx"&gt;Estimating the size of&amp;nbsp;a database&lt;/a&gt;" topic in Books Online.)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;Finally, there are SQL sizing tools that will give you hardware recommendations for an unknown app with little to&amp;nbsp;no time investment on your part.&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: small;"&gt;These might provide a nice starting point, but I&amp;rsquo;d take their recommendations with a &lt;a href="http://www.sqlmag.com/article/sql-server/database-sizing-tools-for-sql-server-.aspx"&gt;grain of salt&lt;/a&gt;.&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: small;"&gt;As&amp;nbsp;mentioned earlier, you can't estimate required hardware with any real precision when the app and the nature of its SQL workload are unknown variables, so be cautious about any recommendation&amp;nbsp;that is made on the basis of a few vague metrics.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;Dell SQL Advisor: &lt;/span&gt;&lt;a href="http://www.dell.com/content/topics/global.aspx/tools/advisors/sql_advisor?c=us&amp;amp;cs=555&amp;amp;l=en&amp;amp;s=biz"&gt;&lt;span style="font-size: small;"&gt;http://www.dell.com/content/topics/global.aspx/tools/advisors/sql_advisor?c=us&amp;amp;cs=555&amp;amp;l=en&amp;amp;s=biz&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;HP SQL Sizers (note: separate links to sizing tools for BI workloads vs. OLTP workloads): &lt;/span&gt;&lt;a href="http://h71028.www7.hp.com/enterprise/cache/3887-0-0-0-121.html"&gt;&lt;span style="font-size: small;"&gt;http://h71028.www7.hp.com/enterprise/cache/3887-0-0-0-121.html&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10026244" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Capacity+Planning/">SQL Capacity Planning</category></item><item><title>Purging Data</title><link>http://blogs.msdn.com/b/bartd/archive/2010/06/01/purging-data.aspx</link><pubDate>Tue, 01 Jun 2010 22:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10018566</guid><dc:creator>bartduncan</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10018566</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10018566</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2010/06/01/purging-data.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;This post is about deleting a lot of rows, as you might do in a data archiving or purging task.&amp;nbsp; By &amp;ldquo;a lot of rows&amp;rdquo;, I mean anything from a few thousand rows up to billions.&amp;nbsp; This may seem elementary, but there are some surprising problems that can appear if you use a simple DELETE to delete a large volume of data.&amp;nbsp; Below are some best practices to consider. &amp;nbsp;(Note that some, but not all, of these suggestions also apply to large UPDATE or INSERT operations.)&amp;nbsp; &lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;h2 style="margin: 10pt 0in 0pt;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="color: #4f81bd;"&gt;&lt;span style="font-family: Cambria;"&gt;TRUNCATE When You Can&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;The first, and simplest, best practice is to use TRUNCATE instead of DELETE when possible.&amp;nbsp; TRUNCATE will only log extent or page deallocation operations, while a DELETE is a row-by-row operation and must log the pre-delete image of every deleted row.&amp;nbsp; TRUNCATE runs nearly instantaneously, even on very large tables that might take days or hours to clear out with a DELETE statement.&amp;nbsp; But of course TRUNCATE can only be used when you need to delete all of the data in a table.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;h2 style="margin: 10pt 0in 0pt;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="color: #4f81bd;"&gt;&lt;span style="font-family: Cambria;"&gt;Consider Partitioning&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;If you have Enterprise or Datacenter Edition and are running SQL Server 2005 or later, you may be able to partition your data.&amp;nbsp; With an appropriately-selected partitioning scheme, you can simply switch out the old data.&amp;nbsp; The cost of this is generally similar to a TRUNCATE or DROP TABLE &amp;ndash; in other words, nearly instantaneous.&amp;nbsp; You can learn more about SQL Server partitioning in &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms345146(SQL.90).aspx#sql2k5parti_topic24"&gt;&lt;span style="font-family: Calibri; color: #0000ff; font-size: small;"&gt;this whitepaper&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt; by Kim Tripp.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;h2 style="margin: 10pt 0in 0pt;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="color: #4f81bd;"&gt;&lt;span style="font-family: Cambria;"&gt;Delete in Chunks &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Most of the time you only want to delete a subset of the rows in a table.&amp;nbsp; If you can&amp;rsquo;t use partitioning, you must use DELETE.&amp;nbsp; Suppose that you needed to delete a lot of data with a straightforward DELETE statement like this one:&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DELETE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; big_table&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; ready_to_archive &lt;span style="color: gray;"&gt;=&lt;/span&gt; 1&lt;span style="color: gray;"&gt;;&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;This could cause two problems.&amp;nbsp; The first is that the DELETE will acquire exclusive locks on all deleted rows, and those locks will be held until the DELETE completes.&amp;nbsp; If the DELETE runs for hours, other users trying to query or modify the table may be blocked for hours.&amp;nbsp; If more than a few thousand locks are acquired, SQL may escalate to an X table lock that will prevent anyone else from modifying &lt;i&gt;any&lt;/i&gt; row in the table, even rows that will not be deleted.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;The second issue that you might see is transaction log growth.&amp;nbsp; If your database is in simple recovery mode, under normal circumstances a relatively small .LDF file will be sufficient because the space in the log file is frequently reused.&amp;nbsp; But SQL can only reuse space in a transaction log prior to the region that records the oldest uncommitted transaction.&amp;nbsp; The long-running DELETE may cause your .LDF file(s) to grow.&amp;nbsp; If the file grows too large or if you have disabled autogrow you will eventually run out of log space, bringing all activity in the database to a halt.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;You can avoid both of these problems by deleting the rows in chunks, like this: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DECLARE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; @rows_affected &lt;span style="color: blue;"&gt;BIGINT&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DECLARE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; @delete_batch_size &lt;span style="color: blue;"&gt;INT&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; @delete_batch_size &lt;span style="color: gray;"&gt;=&lt;/span&gt; 1000&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; @rows_affected &lt;span style="color: gray;"&gt;=&lt;/span&gt; @delete_batch_size&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHILE &lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: gray; font-size: 10pt;"&gt;(&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;@rows_affected &lt;span style="color: gray;"&gt;=&lt;/span&gt; @delete_batch_size&lt;span style="color: gray;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;DELETE&lt;/span&gt; &lt;span style="color: blue;"&gt;TOP &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;@delete_batch_size&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; big_table&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&lt;/span&gt; ready_to_archive &lt;span style="color: gray;"&gt;=&lt;/span&gt; 1&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;SET&lt;/span&gt; @rows_affected &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;@@ROWCOUNT&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; END&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: gray; font-size: 10pt;"&gt;;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;This will delete 1000 rows at a time, continuing until there are no more rows to delete.&amp;nbsp; Each batch of 1000 rows is deleted in a separate transaction, so the batch can run for a long time without holding locks for a long time or causing transaction log growth. &amp;nbsp;You can experiment with different batch sizes, but in my experience a batch size above 1000 rows doesn&amp;rsquo;t improve performance that much.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;If you want to avoid transaction log growth but don&amp;rsquo;t care about reduced concurrency (for example, if the DELETE is running during a maintenance window when no one else will need to query the table), you may see a small performance boost by deleting in chunks but forcing a table lock on each DELETE.&amp;nbsp; The cost of lock management and other housekeeping tasks is reduced when SQL doesn&amp;rsquo;t have to worry about concurrency.&amp;nbsp; A TABLOCK hint also allows SQL to reclaim empty pages that would otherwise remain allocated to the table.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHILE &lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: gray; font-size: 10pt;"&gt;(&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;@rows_affected &lt;span style="color: gray;"&gt;=&lt;/span&gt; @delete_batch_size&lt;span style="color: gray;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;DELETE&lt;/span&gt; &lt;span style="color: blue;"&gt;TOP &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;@delete_batch_size&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; big_table &lt;b&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="background: yellow; color: blue; mso-highlight: yellow;"&gt;WITH&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; &lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;TABLOCK&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&lt;/span&gt; ready_to_archive &lt;span style="color: gray;"&gt;=&lt;/span&gt; 1&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;SET&lt;/span&gt; @rows_affected &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;@@ROWCOUNT&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; END&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: gray; font-size: 10pt;"&gt;;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;On the other hand, if you don&amp;rsquo;t care about transaction log growth but you do want to prevent an unnecessary table lock escalation, you can use trace flag 1224:&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-family: courier new,courier;"&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background-color: #ffff00;"&gt;DBCC&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #ffff00;"&gt;&lt;span style="font-size: x-small;"&gt; TRACEON&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;1224&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: courier new,courier;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DELETE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; big_table&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; ready_to_archive &lt;span style="color: gray;"&gt;=&lt;/span&gt; 1&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;&lt;/span&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: courier new,courier;"&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background-color: #ffff00;"&gt;DBCC&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #ffff00;"&gt;&lt;span style="font-size: x-small;"&gt; TRACEOFF&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;1224&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: courier new,courier;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="font-family: courier new,courier;"&gt;&lt;span style="FONT-FAMILY: ; COLOR: #0000ff; FONT-SIZE: x-small"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Trace flag 1224 is documented and supported.&amp;nbsp; It prevents SQL from escalating to table locks except when it must because the server is running low on memory.&amp;nbsp; This may also be a good compromise solution if you want to maximize concurrency but you require that the entire delete operation be transactional.&amp;nbsp; &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: medium;"&gt;&lt;span style="color: #4f81bd;"&gt;&lt;span style="font-family: Cambria;"&gt;Consider Indexes and Foreign Keys&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;If your DELETE statement is running slower than you expect, it might make sense to drop indexes on the table and/or any foreign keys referring to the table, do the DELETE, then recreate the objects you dropped.&amp;nbsp; Indexes on a table can dramatically slow down a large DELETE, even to the point where it is cheaper to rebuild the index from scratch after the DELETE than it would be to maintain the index in place.&amp;nbsp; (Note that sometimes you clearly will see the extra index maintenance operators in the query plan, and other times the query plan won&amp;rsquo;t show this work.&amp;nbsp; But just because you don&amp;rsquo;t see any Index Delete operators in the plan doesn&amp;rsquo;t mean that the DELETE&amp;rsquo;s performance isn&amp;rsquo;t affected by index maintenance overhead.&amp;nbsp; For details, see my earlier post on &lt;/span&gt;&lt;a href="http://blogs.msdn.com/b/bartd/archive/2006/07/27/wide-vs-narrow-plans.aspx"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;Wide vs. Narrow Plans&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;.)&amp;nbsp; Foreign keys on other tables that refer to the table that you&amp;rsquo;re deleting from can also slow down the DELETE.&amp;nbsp; The FK overhead comes from the fact that SQL must verify that you&amp;rsquo;re not deleting any rows that are referenced by rows in the child table.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;It&amp;rsquo;s difficult to generalize about when it might be cost effective to drop indexes or FKs.&amp;nbsp; The effect on the overall runtime of the delete job depends on how many indexes are on the table, the size of the indexes, whether any FK columns in other tables are indexed, the portion of the table you&amp;rsquo;re deleting, whether there is correlation between nonclustered and clustered index keys values, etc.&amp;nbsp; Personally, I&amp;rsquo;d consider testing out the approach and quantifying the benefits when all of the following are true: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Your DELETE query runs slower than you would like &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Temporarily dropping indexes or FKs won&amp;rsquo;t hurt anything (for example, if the DELETE runs in a maintenance window when no one else is using the database)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;You are deleting a fairly large % of the rows in the table (rule of thumb: &amp;gt; 10% of the rows)&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10018566" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Locking/">SQL Locking</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>Finding procedure cache bloat</title><link>http://blogs.msdn.com/b/bartd/archive/2010/05/26/finding-procedure-cache-bloat.aspx</link><pubDate>Wed, 26 May 2010 20:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10015883</guid><dc:creator>bartduncan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=10015883</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=10015883</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2010/05/26/finding-procedure-cache-bloat.aspx#comments</comments><description>&lt;p&gt;Explicitly parameterizing queries is&amp;nbsp;a well-known best-practice for database app developers.&amp;nbsp; There are cases where it can make sense to execute a query unparameterized, but, in general, you should default to explicit parameterization.&amp;nbsp; It can make your queries faster by avoiding unnecessary compilation when the "same" query&amp;nbsp;is run repeatedly with different parameters, and it can reduce competition for limited&amp;nbsp;memory in SQL Server's visible buffer pool.&amp;nbsp; If you don't already know how to parameterize your queries, or if you want more information about the benefits of parameterization, check out: &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;a href="http://weblogs.sqlteam.com/dang/archive/2008/02/21/Dont-Bloat-Proc-Cache-with-Parameters.aspx"&gt;http://weblogs.sqlteam.com/dang/archive/2008/02/21/Dont-Bloat-Proc-Cache-with-Parameters.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;a href="http://weblogs.sqlteam.com/dang/archive/2008/02/18/Why-Parameters-are-a-Best-Practice.aspx"&gt;http://weblogs.sqlteam.com/dang/archive/2008/02/18/Why-Parameters-are-a-Best-Practice.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Below is a handy DMV query that can quickly identify which queries&amp;nbsp;have many different plans cached for the same query.&amp;nbsp; In most cases, this indicates a lack of explicit parameterization of the query.&amp;nbsp; This uses the query plan hash/query plan fingerprint feature that I wrote about &lt;a href="http://blogs.msdn.com/b/bartd/archive/2008/09/03/query-fingerprints-and-plan-fingerprints_3a00_-the-best-new-sql-2008-feature-you_2700_ve-never-heard-of.aspx" title="query plan fingerprints"&gt;in an earlier post&lt;/a&gt;.&amp;nbsp; It relies on the fact that two queries that have different inline literal values (e.g. "SELECT...WHERE col1 = 123", vs. "SELECT...WHERE col1 = 456") will get two different query plans, but the plans will have the same query_hash value.&amp;nbsp; You must be running SQL Server 2008 or later to use this.&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;hr width="5" size="3" style="width: 5px; height: 3px;" /&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;WITH &lt;/span&gt;duplicated_plans &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;TOP&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 20&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query_hash&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;TOP&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 1 [sql_handle] &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;dm_exec_query_stats&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s2 &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;WHERE&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s2&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;query_hash &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s1&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;query_hash &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;ORDER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; [sql_handle]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; sample_sql_handle&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;TOP&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 1 statement_start_offset &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;dm_exec_query_stats&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s2 &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;WHERE&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s2&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;query_hash &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s1&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;query_hash &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;ORDER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; [sql_handle]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; sample_statement_start_offset&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;TOP&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 1 statement_end_offset &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;dm_exec_query_stats&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s2 &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;WHERE&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s2&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;query_hash &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s1&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;query_hash &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;ORDER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; [sql_handle]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; sample_statement_end_offset&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CAST&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;pa&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;value &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;INT&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;dbid&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;COUNT&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(*)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; plan_count&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;dm_exec_query_stats&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; s1&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;OUTER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;APPLY&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;dm_exec_plan_attributes&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;s1&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;plan_handle&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; pa&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; pa&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;attribute &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #ff0000; font-size: x-small;"&gt;&lt;span style="color: #ff0000; font-size: x-small;"&gt;'dbid'&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GROUP&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; query_hash&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; pa&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;value&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ORDER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;COUNT&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(*)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;DESC&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;SELECT&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;query_hash&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;plan_count&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CONVERT&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;NVARCHAR&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;80&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;REPLACE&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;REPLACE&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;LTRIM&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SUBSTRING&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sql&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;[text]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;sample_statement_start_offset &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 2&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 1&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CASE&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; sample_statement_end_offset &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;-&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;1 &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;THEN&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;DATALENGTH&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;sql&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;[text]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ELSE&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; sample_statement_end_offset&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;-&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;sample_statement_start_offset &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 2&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;),&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CHAR&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;10&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #ff0000; font-size: x-small;"&gt;&lt;span style="color: #ff0000; font-size: x-small;"&gt;''&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;CHAR&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;13&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #ff0000; font-size: x-small;"&gt;&lt;span style="color: #ff0000; font-size: x-small;"&gt;''&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; qry&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;OBJECT_NAME&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;sql&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;objectid&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;sql&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;[dbid]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; [object_name]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DB_NAME&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;duplicated_plans&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;[dbid]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; [database_name]&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; duplicated_plans &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;CROSS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;APPLY&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;dm_exec_sql_text&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;duplicated_plans&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;sample_sql_handle&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;sql&lt;br /&gt;WHERE&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;&lt;span style="color: #ff00ff; font-size: x-small;"&gt;ISNULL&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt;&lt;span style="color: #0000ff; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;duplicated_plans&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;[dbid]&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 0&lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;!=&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; 32767&lt;/span&gt;&lt;span style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;&lt;span style="color: #008000; font-size: x-small;"&gt;-- ignore queries from Resource DB &lt;br /&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;AND&amp;nbsp;&lt;span style="color: #000000;"&gt;plan_count &lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;&amp;gt; &lt;span style="color: #000000;"&gt;1&lt;span style="color: #808080; font-size: x-small;"&gt;&lt;span style="color: #808080; font-size: x-small;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;hr width="1" style="width: 1px;" /&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;hr width="1" style="width: 1px;" /&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10015883" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Procedure+Cache/">SQL Procedure Cache</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Memory/">SQL Memory</category></item><item><title>Sometimes the Simplest Solution Isn't the Best Solution (The Optional Parameter Problem)</title><link>http://blogs.msdn.com/b/bartd/archive/2009/05/03/sometimes-the-simplest-solution-isn-t-the-best-solution-the-all-in-one-search-query.aspx</link><pubDate>Sun, 03 May 2009 19:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9584853</guid><dc:creator>bartduncan</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=9584853</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=9584853</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2009/05/03/sometimes-the-simplest-solution-isn-t-the-best-solution-the-all-in-one-search-query.aspx#comments</comments><description>&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;Programmers should naturally gravitate toward the simplest, most elegant solution.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This is because the simplest coding solution is so often the best solution: simple solutions are cheaper to implement; easier for others to understand, maintain, and extend; and less prone to bugs.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Simplicity can also be associated with efficient and consistent execution, but performance is one area where Occam's Razor doesn't apply quite as consistently.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Performance requirements, more often than many other types of requirement, may demand introducing complexity into what would otherwise be a nice, tidy implementation.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I&amp;rsquo;m going to discuss one such case here.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Cambria; COLOR: #4f81bd; FONT-SIZE: medium"&gt;The All-In-One Search Query&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;Sometimes you need a query or a stored procedure to expose several search parameters but actually do the search only for the subset of the parameters that the caller cares about.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, consider a [find_customer] stored procedure that can search for a @name value, a customer @id, an @address, or any combination of these.&amp;nbsp; The most obvious solution to this problem looks something like this: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; [id]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [address]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@name &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR&lt;/span&gt; [name] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @name&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@id &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR&lt;/span&gt; [id] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @id&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@address &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR&lt;/span&gt; [address] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @address&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 11.0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;This allows the caller to pass non-NULL values for the search criteria they care about, and to pass NULLs for any criteria that they want to be ignored.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This pattern is variously referred to as "all-in-one search query", "catch-all query", "dynamic search conditions", or "optional parameters".&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Here&amp;rsquo;s a sample stored proc that shows a slightly simplified example with just two available search parameters: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt; &lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt; find_customer &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;@name &lt;span style="COLOR: blue"&gt;nvarchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;128&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;@id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; [id]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [address]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [creation_date] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;FROM&lt;/span&gt; [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@name &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR&lt;/span&gt; [name] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @name&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@id &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR&lt;/span&gt; [id] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @id&lt;span style="COLOR: gray"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;END&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: gray; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;This idiom is simple enough that it is relatively easy to understand.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Despite its simplicity, it allows a single query to meet many different needs (in this case: a search on name, or a search on address, or a search on both name and address).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It is understandable that queries like this are so popular; it seems to be an elegant solution, and developers like elegance.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Unfortunately, it&amp;rsquo;s usually a bad idea for practical performance reasons.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The problems are significant enough that I regard this as an anti-pattern in my own code.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Cambria; COLOR: #4f81bd; FONT-SIZE: medium"&gt;What's Wrong With the Obvious Solution?&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;The first problem is that the OR operators will prevent an efficient index seek-based query plan. &amp;nbsp;If the search parameters are mutually exclusive (the caller is only supposed to provide one parameter value), you can avoid this problem by changing the query to: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; [id]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [address]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [creation_date] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@name &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NOT&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; [name] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @name&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;OR&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@id &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NOT&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; [id] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @id&lt;span style="COLOR: gray"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 11.0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;That may allow an index seek, but you should expect that it will still perform poorly in some cases because it suffers from other problems that I&amp;rsquo;ll discuss next.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Also note that the query semantics aren't quite the same as the first query.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;The second problem that affects both the original problem query and the attempted rewrite is that SQL does a poor job with plan costing for queries like this.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In general, you should avoid using filter predicates where both operands are constants and one or both are variables or parameters (e.g. &amp;ldquo;where @var = 1&amp;rdquo;, or &amp;ldquo;where @parameter is null&amp;rdquo;).&amp;nbsp; These have a tendency to screw up the optimizer&amp;rsquo;s estimates because at plan compile time the optimizer has no idea whether the predicate will qualify all rows, or disqualify all rows.&amp;nbsp; You are essentially guaranteed that its blind guess will be exactly wrong for some parameters.&amp;nbsp; The bad estimate is likely to cause inaccurate costing of plan alternatives, which in turn is likely to cause selection of a suboptimal plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;The third problem is that there is no single plan that will be appropriate for all of the different combinations of parameters, even if the optimizer was smart enough to predict the outcome of &amp;ldquo;variable = constant&amp;rdquo;-type predicates at compile time.&amp;nbsp; For example, suppose you had a nonclustered index on the [name] column, and another nonclustered index on [id]. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;If SQL chose a plan that first did a lookup using the index on the [name] column, this would be a very poor choice whenever the caller passed NULL for @name.&amp;nbsp; Conversely, a plan that first scanned an index on the [id] column would be inefficient when @id was null and @name was non-null.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;The only situations where you should use queries like either of those shown above is when (a) you don't care about the performance of the query, or (b) you can guarantee that the table will only contain a&amp;nbsp;handful of&amp;nbsp;rows so that a table scan-based plan will never be noticeably&amp;nbsp;slower than a seek-based plan.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Cambria; COLOR: #4f81bd; FONT-SIZE: medium"&gt;Possible Solutions&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;One possible solution would be to have a different query for each search parameter (or combination of search parameters,&amp;nbsp;if that is legal input&amp;nbsp;for the procedure).&amp;nbsp; This is shown below.&amp;nbsp; It may be a practical solution if you only have two or three possible search parameters.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;CREATE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt; find_customer&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@name &lt;span style="COLOR: blue"&gt;nvarchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;128&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;IF &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@name &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; [id]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [address]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [creation_date] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; [id] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @id&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;IF &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@id &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; [id]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [address]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [creation_date] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; [name] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @name&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; [id]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [address]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [creation_date] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; [name] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @name &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; [id] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @id&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;Note that parameter sniffing may cause bad plan selection even in this case if each search query is more complex than the queries shown here (if the queries include joins against large tables, for example).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;You can protect against this problem by assigning the parameters to local variables and using the variables in the queries.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;You can also avoid the problem by moving the search queries to a different compilation scope (child stored procedures, dynamic SQL, or "OPTION(RECOMPILE)" query hints).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;The second possible solution uses dynamic SQL to construct the WHERE clause dynamically.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;CREATE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt; find_customer&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@name &lt;span style="COLOR: blue"&gt;nvarchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;128&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WITH&lt;/span&gt; &lt;span style="COLOR: blue"&gt;EXECUTE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; &lt;span style="COLOR: blue"&gt;OWNER&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;DECLARE&lt;/span&gt; @search_query &lt;span style="COLOR: blue"&gt;nvarchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt; @search_query &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT [id], [name], [address], [creation_date] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE 1 = 1 '&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: gray; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;IF &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@name &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NOT&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt; @search_query &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @search_query &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'AND [name] = @name'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;IF &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@id &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NOT&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt; @search_query &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @search_query &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'AND [id] = @id'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;EXEC&lt;/span&gt; &lt;span style="COLOR: maroon"&gt;sp_executesql&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;@search_query&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;N' @name nvarchar(128), @id int'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@name &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @name&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @id&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;You should explicitly parameterize the query, as shown here, to avoid the risk of SQL injection attacks.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Note the EXECUTE AS OWNER used for this procedure, which may be appropriate to avoid the need to grant the end user SELECT permission on the base table(s).&amp;nbsp; (Caution: &lt;em&gt;always &lt;/em&gt;triple-check to ensure that the proc is not vulnerable to SQL injection before enabling EXECUTE AS OWNER.)&amp;nbsp; Also note that the pattern is simplified by always passing all parameters to sp_executesql, even though the query may only make use of a subset of the parameters.&amp;nbsp;&amp;nbsp;I generally don't like to use dynamic SQL where I can avoid it, but I make an exception for this case; the dynamic WHERE clause is actually my preferred solution when I'm faced with this problem on SQL 2005 and earlier.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;If you're running SQL 2008 or later, there is a simpler solution that generally works well.&amp;nbsp;More recent versions of SQL are better at predicting the effect of a predicate like "@p1 IS NULL" at compile time. You still have to worry about inappropriate plan reuse (getting a compiled plan that is good for the initial set of parameters but bad for subsequent parameters), though. That problem can be mitigated with an OPTION(RECOMPILE) query hint.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CREATE&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt; &lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt; find_customer &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;@name &lt;span style="COLOR: blue"&gt;nvarchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;128&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;@id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; [id]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [address]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; [creation_date] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;FROM&lt;/span&gt; [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@name &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR&lt;/span&gt; [name] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @name&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@id &lt;span style="COLOR: gray"&gt;IS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR&lt;/span&gt; [id] &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @id&lt;span style="COLOR: gray"&gt;)&lt;br /&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span style="background-color: #ffff00;"&gt;OPTION&lt;span style="color: gray;"&gt;(&lt;span style="color: #0000ff;"&gt;RECOMPILE&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #ffff00;"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;END&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; COLOR: gray; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Calibri; FONT-SIZE: small"&gt;This will generate a new plan on each execution that is optimized for that execution's&amp;nbsp;set of parameters.&amp;nbsp;The code remains very simple. The additional per-execution compile cost can generally be tolerated if the procedure execution rate is low -- say,&amp;nbsp;less than a dozen executions per second.&amp;nbsp;If the proc execution rate is higher than that, you'll need to compare the compile cost to execution cost (you can use SET STATISTICS TIME ON) to see whether the small extra per-execution compile cost is offset by the more consistent performance that you should get with the hint.&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;I&amp;rsquo;ve seen the All-In-One search query cause perf problems in code written both inside and outside of Microsoft, and I&amp;rsquo;ve been burned by the problem in my own code.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;If you&amp;nbsp;find yourself needing this type of query, my recommendation is to first evaluate OPTION(RECOMPILE), assuming you're on SQL 2008 or later.&amp;nbsp;On SQL 2005 and earlier, or if you're on a&amp;nbsp;more recent version but&amp;nbsp;can't tolerate the per-execution compile cost that the RECOMPILE hint introduces,&amp;nbsp;the dynamic SQL approach would be my next choice in most cases. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: Erland Sommarskog has a page on this very topic here: &lt;a href="http://www.sommarskog.se/dyn-search-2005.html"&gt;http://www.sommarskog.se/dyn-search-2005.html&lt;/a&gt;.&amp;nbsp; He calls it the problem of "dynamic search conditions" (a less awkward phrase than "All-In-One Search Query").&amp;nbsp; The page provides a very detailed discussion of the topic, and would be a great read if you really want to drill into the details.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;strong&gt;UPDATE #2&lt;/strong&gt;: Another good read on a variation of this problem: &lt;a href="http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/"&gt;http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/&lt;/a&gt;&amp;nbsp;(and, to a&amp;nbsp;lesser degree,&amp;nbsp;&lt;a href="http://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/"&gt;http://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/&lt;/a&gt;), both from Gail Shaw.&amp;nbsp; Gail calls this class of query&amp;nbsp;"Catch-All Queries".&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: small"&gt;&lt;span style="FONT-FAMILY: Calibri"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;strong&gt;UPDATE #3&lt;/strong&gt;: Another&amp;nbsp;discussion of the optional parameter problem from Conor Cunningham:&amp;nbsp;&lt;/span&gt;&lt;a href="http://blogs.msdn.com/conor_cunningham_msft/archive/2010/04/22/conor-vs-optional-parameters.aspx"&gt;http://blogs.msdn.com/conor_cunningham_msft/archive/2010/04/22/conor-vs-optional-parameters.aspx&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 9pt; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;FROM&lt;/span&gt; [customers]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9584853" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>The Death of DateTime?</title><link>http://blogs.msdn.com/b/bartd/archive/2009/03/31/the-death-of-datetime.aspx</link><pubDate>Wed, 01 Apr 2009 00:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9524115</guid><dc:creator>bartduncan</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=9524115</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=9524115</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2009/03/31/the-death-of-datetime.aspx#comments</comments><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;SQL Server 2008 added a new data type named “&lt;B style="mso-bidi-font-weight: normal"&gt;datetimeoffset&lt;/B&gt;”.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;This is similar to the old &lt;B style="mso-bidi-font-weight: normal"&gt;datetime&lt;/B&gt; data type, with the following significant differences: &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3&gt;·&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Internally, the time is stored in unambiguous UTC format&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3&gt;·&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The local time zone offset is stored along with the UTC time, which allows the time to be displayed as a local time value (or converted to any another time zone offset)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3&gt;·&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The data type is capable of storing more precise times than datetime&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;So, when should you use datetimeoffset, and when should you use datetime? &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;The first answer is that you have no choice at all if you aren’t using SQL 2008, since datetimeoffset was first added in this SQL version.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If you are working with SQL 2008, let’s address the question by examining some problems with the older datetime type: &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;SPAN class=MsoIntenseEmphasis&gt;&lt;SPAN style="FONT-STYLE: normal; mso-bidi-font-style: italic"&gt;&lt;FONT size=4&gt;&lt;FONT color=#365f91&gt;&lt;FONT face=Cambria&gt;Problem 1: DateTime is inherently ambiguous&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/H2&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Suppose you are a consultant looking at an existing table with a datetime column.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;A row in this table tells you that some critical event occurred at 4:35pm.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Is this the server’s local time?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Local time for the end user’s machine?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;GMT, or Coordinated Universal Time (UTC)?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Local time for a time zone selected by convention that may or may not match the server’s local time zone? &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Here in Microsoft, for example, some systems were designed to store Pacific time by convention, even though in some of these cases the SQL Server may reside in a data center that isn’t on the west coast.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Other databases store UTC times, again by convention adopted by whoever designed those systems.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;So this is the first problem: &lt;I style="mso-bidi-font-style: normal"&gt;datetime is ambiguous&lt;/I&gt;.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;A datetime value by itself actually does not identify a particular moment in time; it takes on an clear meaning only when you interpret it in the context of some assumed, and usually unenforced, time zone.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;SPAN class=MsoIntenseEmphasis&gt;&lt;SPAN style="FONT-STYLE: normal; mso-bidi-font-style: italic"&gt;&lt;FONT size=4&gt;&lt;FONT color=#365f91&gt;&lt;FONT face=Cambria&gt;Problem 2: It is impractical to convert historical DateTime values to/from time zones&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/H2&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If you’ve ever built a data warehouse that consolidated data from several data sources, you may have struggled to convert various ambiguous local time representations into a consistent form.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Some data sources store their times as UTC, some store local server time, others use a local time based on some end user’s time zone.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;You could just cram all of those values into a datetime column in your warehouse, but it would be impossible to interpret the times in a meaningful way.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;No matter what time zone you selected as a lens through which to interpret the data, it would be wrong for some values.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;So you must expend some extra effort to figure out the implicit time zone for every data source in your warehouse, and then you have to write code that converts all of these various times to some consistent time zone, likely UTC time, in the central data warehouse.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If that was the end of it, it would be bad enough. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;But Daylight Savings Time makes it next to impossible to do this conversion in a generic way.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The problem is that different locales have different Daylight Savings Time rules.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Many parts of the world don’t honor DST at all, some places do honor it but use half-hour offsets instead of full hour offsets, and various places begin and end DST on different dates.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Here’s an concrete example: suppose you have the local datetime value 2008-03-05 08:30:00 in the database, and you need to convert this to UTC time.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;By interviewing the right DBA or by examining source code, you have determined that this database stores datetime values using local server time.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The local server is in the Pacific time zone, and you’ve found that you can use a T-SQL expression like this to determine the current time zone offset for the local server: &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: fuchsia; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;DATEDIFF&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;minute&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: fuchsia"&gt;GETUTCDATE&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(),&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;GETDATE&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;())&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;This tells you that the local server time is 7 hours behind UTC right now, so you should be able to add 7 hours to the local time to get the equivalent UTC time, right?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;That would be wrong; Daylight Savings Time is in effect in the U.S. today (April), but when this datetime value was collected back in March of last year, DST was not in effect.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The correct time zone offset to use is UTC minus 8 hours.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;So you have to have knowledge of what the local time zone offset &lt;I style="mso-bidi-font-style: normal"&gt;would have been&lt;/I&gt; on arbitrary past or future dates, and bake this knowledge into your conversion routine.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If your data comes from a variety of locales, you have to have correct information about the time zone rules in every place your data comes from.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;To make matters worse, the rules for DST may change from year to year in the same locale due to legislative changes, so you have to capture different sets of rules for different ranges of dates within each region.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Have you taken into account the fact that (most of)&amp;nbsp;the state of Arizona doesn’t use DST?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Or that Indiana didn’t use DST at all prior to 2006, but you do need to adjust for DST for any data that was captured on a server from Indiana after 2006?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Does your conversion routine account for the fact that Daylight Savings Time in the U.S. was lengthened by about a month starting in 2007?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;This problem isn’t unique to the United States, and the situation can be even more grim if your data comes from more than one country.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;DBAs and developers in China, India, and Japan get off a little bit easier because DST is not observed in those countries, but they still have the problem to some degree if they ever need to consume data that originated in other places or push their data to a consumer in a different country.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Finally, there is small time window each year&amp;nbsp;that will defeat&amp;nbsp;even the world’s most intelligent time zone conversion routine.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In 2009, DST in the United States will end at 2:00 am on November 1st.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;At that time the clocks will roll back an hour, to 1:00 am.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In other words, each year there is a one-hour window during which times like 2009-11-01 01:35 am will actually occur &lt;I style="mso-bidi-font-style: normal"&gt;twice&lt;/I&gt;.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;That ambiguity is completely intractable. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;SPAN class=MsoIntenseEmphasis&gt;&lt;SPAN style="FONT-STYLE: normal; mso-bidi-font-style: italic"&gt;&lt;FONT size=4&gt;&lt;FONT color=#365f91&gt;&lt;FONT face=Cambria&gt;Problem 3: If you do manage to convert DateTime values collected in various places to a single time zone, you lose important information&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/H2&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Let’s suppose that a database you are working with stores UTC times.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Good for you: all of your times are unambiguous.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;But unless your users all live in London or Lisbon (and DST is not in effect), UTC is generally not very meaningful to a user.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;You could theoretically convert the times to the end user’s local time zone (if it weren't for the inconvenient fact that this is impractical, as we just discovered).&amp;nbsp; But what if you wanted to present the time in local time relative to the place where the timestamp was captured?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;For example, suppose you wanted to show records from a consolidated server health log as local times for the server where they were captured. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;You can’t.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The information about the current local time zone offset at the moment the timestamp was collected was lost when you converted the difficult-to-work-with local time into that nice, pure UTC time.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;These three problems can combine to create a real mess.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Frankly, I think I might consider a career change if I was tasked with solving all of these problems in a large-scale project that consolidated historical data from many different places.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Of course, you might be thinking, &lt;I style="mso-bidi-font-style: normal"&gt;My datetime values are all local server time; their meaning is perfectly clear to me&lt;/I&gt;. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Well, one day your company may expand and your little homegrown system might need to handle data from more than one region.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Or you might need to import the data into a new system when your solution is thrown out for being too provincial :).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Or the data in your local database might turn out to be needed in some central data warehouse that consolidates data from a variety of sources.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;You can save yourself and your successors some grief by using the more robust datetimeoffset data type from the start. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;SPAN class=MsoIntenseEmphasis&gt;&lt;SPAN style="FONT-STYLE: normal; mso-bidi-font-style: italic"&gt;&lt;FONT size=4&gt;&lt;FONT color=#365f91&gt;&lt;FONT face=Cambria&gt;When to Use DateTimeOffset?&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/H2&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Because the datetimeoffset data type stores a UTC date internally, it’s free of the ambiguity that causes problems #1 and #2.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;And because it also stores the time zone offset that was current at the time the timestamp was generated, it doesn’t suffer from the data loss problem that you face if you store times as UTC datetime values (problem #3).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In other words, with the same datetimeoffset value you can represent the value as a local time or easily convert it to a UTC time.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;SQL will do the right thing if you compare two datetimeoffset values, even if the values were captured from systems with very different time zone offsets.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;So, let's return to the original question: When should you use datetimeoffset instead of datetime?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The answer is: &lt;I style="mso-bidi-font-style: normal"&gt;you should almost always use datetimeoffset&lt;/I&gt;.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I’ll make the claim that there is only a single case where datetime is clearly the best data type for the job, and that’s when you actually require an ambiguous time.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;For example, if you wanted a column to record the fact that all stores in a chain should open at 8:00am local time (whatever the local time zone may be), you should use datetime.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;But any time you want to store a value that represents an &lt;I style="mso-bidi-font-style: normal"&gt;absolute&lt;/I&gt; &lt;I style="mso-bidi-font-style: normal"&gt;moment in time&lt;/I&gt;, you would be better off using datetimeoffset. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;For most applications, that means that just about everywhere you currently use datetime would be a good candidate for datetimeoffset. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Please don’t beat me up over the fact that SQL 2008’s DMVs still use datetime :).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;This is a known problem, and it’s on the books to look at for future versions.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;We’re facing the same problems that you’ll face in your existing systems: it’s hard to make a system-wide datatype change that doesn’t break someone somewhere.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;For any brand new SQL development work you do, though, I encourage you to pause and consider your choice carefully before using datetime.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;It may be an appropriate choice in some cases, but most of the time you’d probably be better off with datetimeoffset.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;H1 style="MARGIN: 24pt 0in 0pt"&gt;&lt;SPAN class=MsoIntenseEmphasis&gt;&lt;SPAN style="FONT-STYLE: normal; mso-bidi-font-style: italic"&gt;&lt;FONT size=5&gt;&lt;FONT color=#365f91&gt;&lt;FONT face=Cambria&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;SPAN class=MsoIntenseEmphasis&gt;&lt;SPAN style="FONT-SIZE: 14pt; FONT-STYLE: normal; mso-bidi-font-style: italic; mso-bidi-font-size: 13.0pt"&gt;Common DateTimeOffset-Related Tasks in T-SQL (SQL 2008 only)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/H2&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/H1&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Retrieve the current time as a datetimeoffset (comparable to the venerable GETDATE function): &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: fuchsia"&gt;SYSDATETIMEOFFSET&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Retrieve the server’s current time zone offset (the number of minutes before or after UTC): &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: fuchsia"&gt;DATENAME&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;TZoffset&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;SYSDATETIMEOFFSET&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;())&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Convert from datetime to datetimeoffset (note that this uses the server’s current time zone offset, which could be inappropriate for historical dates): &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: fuchsia"&gt;TODATETIMEOFFSET&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;datetimevalue&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;DATENAME&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;TZoffset&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;SYSDATETIMEOFFSET&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()))&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;Convert a datetimeoffset value (in this case, local server time returned by SYSDATETIMEOFFSET) to a new time zone offset: &lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Calibri size=3&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;nbsp;&lt;SPAN style="COLOR: fuchsia"&gt;SWITCHOFFSET&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;SYSDATETIMEOFFSET&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(), &lt;FONT color=#ff0000 size=2&gt;'-05:00'&lt;/FONT&gt;)&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Finally, be aware that .Net 2.0 SP1 added support for the datetimeoffset data type, so you can round-trip this nice new data type between SQL and a client app without any fuss.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9524115" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category></item><item><title>Today's Annoyingly-Unwieldy Term: "Intra-Query Parallel Thread Deadlocks"</title><link>http://blogs.msdn.com/b/bartd/archive/2008/09/24/today-s-annoyingly-unwieldy-term-intra-query-parallel-thread-deadlocks.aspx</link><pubDate>Wed, 24 Sep 2008 20:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8964067</guid><dc:creator>bartduncan</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=8964067</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=8964067</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2008/09/24/today-s-annoyingly-unwieldy-term-intra-query-parallel-thread-deadlocks.aspx#comments</comments><description>&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;I've received a couple of questions in email and in comments about deadlocks involving mysterious-sounding non-lock resources like "&lt;em&gt;exchangeEvent&lt;/em&gt;" and "&lt;em&gt;threadpool&lt;/em&gt;".&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;There are a couple of examples in the comments for post &lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;a href="http://blogs.msdn.com/bartd/archive/2006/09/25/deadlock-troubleshooting-part-3.aspx"&gt;&lt;span style="FONT-FAMILY: 'Verdana','sans-serif'; COLOR: blue"&gt;http://blogs.msdn.com/bartd/archive/2006/09/25/deadlock-troubleshooting-part-3.aspx&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;, and here's a forum post on the topic: &lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;a href="http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3913233&amp;amp;SiteID=1"&gt;&lt;span style="FONT-FAMILY: 'Verdana','sans-serif'; COLOR: blue"&gt;http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3913233&amp;amp;SiteID=1&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Here's one example (note that I've omitted the "&lt;em&gt;inputbuf&lt;/em&gt;" and "&lt;em&gt;executionStack&lt;/em&gt;" nodes for the sake of brevity and clarity): &lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;__________________________________________________________________&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;deadlock-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;deadlock victim=process38316d8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;process-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process3808478 schedulerid=1 kpid=216 status=suspended&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;spid=51 sbid=0 ecid=8&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process3809ac8 schedulerid=1 kpid=5672 status=suspended spid=51 sbid=0 ecid=17 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process38136d8 schedulerid=2 kpid=5644 status=suspended spid=51 sbid=0 ecid=16 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process3813828 schedulerid=2 kpid=6064 status=suspended spid=51 sbid=0 ecid=9&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process381c478 schedulerid=3 kpid=5292 status=suspended spid=51 sbid=0 ecid=10 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process381d2e8 schedulerid=3 kpid=4372 status=suspended spid=51 sbid=0 ecid=19 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process38265c8 schedulerid=4 kpid=5552 status=suspended spid=51 sbid=0 ecid=11 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process3827ac8 schedulerid=4 kpid=5716 status=suspended spid=51 sbid=0 ecid=18 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process38309b8 waittime=609 schedulerid=5 kpid=0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process38312e8 schedulerid=5 kpid=3204 status=suspended spid=51 sbid=0 ecid=6&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process38316d8 schedulerid=5 kpid=5108 status=suspended spid=51 sbid=0 ecid=13 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process383a718 schedulerid=6 kpid=5216 status=suspended spid=51 sbid=0 ecid=7&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process383ada8 waittime=609 schedulerid=6 kpid=0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process383beb8 schedulerid=6 kpid=5852 status=suspended spid=51 sbid=0 ecid=14 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process3845588 schedulerid=7 kpid=6096 status=suspended spid=51 sbid=0 ecid=15 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process38456d8 schedulerid=7 kpid=760 status=suspended&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;spid=51 sbid=0 ecid=0&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;process id=process3845c18 schedulerid=7 kpid=5992 status=suspended spid=51 sbid=0 ecid=12 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;resource-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;threadpool id=scheduleree6080&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner id=process38316d8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner id=process38312e8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter id=process38309b8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;exchangeEvent id=port80140950 nodeId=9&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner event=pending id=process383ada8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner event=pending id=process38309b8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process3813828&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process3808478&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process381c478&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process38265c8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process3845c18&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process38316d8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process383beb8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=producer id=process3845588&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=producer id=process38136d8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=producer id=process3809ac8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=producer id=process3827ac8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=producer id=process381d2e8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;exchangeEvent id=port80140690 nodeId=5&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner event=pending id=process383ada8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner event=pending id=process38309b8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=consumer id=process38456d8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;exchangeEvent id=port80140c10 nodeId=12&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner event=pending id=process383ada8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner event=pending id=process38309b8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=producer id=process38312e8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter event=e_waitPortOpen type=producer id=process383a718&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;threadpool id=scheduleref6080&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner id=process383beb8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner id=process383a718&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter id=process383ada8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;__________________________________________________________________&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: 'Calibri','sans-serif'; mso-bidi-font-size: 9.0pt; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h1 style="MARGIN: 24pt 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: large"&gt;&lt;span style="FONT-FAMILY: ; COLOR: #365f91"&gt;&lt;span style="FONT-FAMILY: Cambria"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;"exchangeEvent" Deadlock Resources&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 18pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: major-bidi"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Some terminology, to better understand the trace flag 1222 deadlock output shown above: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="text-decoration: underline;"&gt;spid&lt;/span&gt;&lt;/b&gt; = system process ID, AKA "&lt;em&gt;session_id&lt;/em&gt;" -- to oversimplify slightly, this represents a connection to SQL&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="FONT-FAMILY: Verdana"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="text-decoration: underline;"&gt;sbid&lt;/span&gt;&lt;/b&gt; = system batch ID, also called "&lt;em&gt;request_id&lt;/em&gt;" -- a query that a spid is running&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="text-decoration: underline;"&gt;ecid&lt;/span&gt;&lt;/b&gt; = execution context ID -- a worker thread running part of a query&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;There's one thing that you should note about this deadlock right off the bat: all of the participants (the "&lt;em&gt;process&lt;/em&gt;" nodes in the -T1222 output) are from the same session identifier (&lt;em&gt;spid&lt;/em&gt;) and the same batch (&lt;em&gt;sbid&lt;/em&gt;).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;They each have a different thread ID (&lt;em&gt;kpid&lt;/em&gt; and &lt;em&gt;ecid&lt;/em&gt;).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Each "&lt;em&gt;process&lt;/em&gt;" entry in this deadlock represents a different worker thread, but all of these worker threads are running part of a single large parallel query that was submitted by spid 51.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Another interesting thing about this deadlock is in the resource list: most deadlocks involve lock resources ("&lt;em&gt;pagelock&lt;/em&gt;", "&lt;em&gt;keylock&lt;/em&gt;", etc), but this one only deals with "&lt;em&gt;exchangeEvent&lt;/em&gt;" and "&lt;em&gt;threadpool&lt;/em&gt;" resources.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Deadlocks centering around &lt;em&gt;exchangeEvent&lt;/em&gt; resources have been given the name "&lt;em&gt;&lt;strong&gt;intra-query parallelism deadlock&lt;/strong&gt;&lt;/em&gt;".&amp;nbsp; (I know -- it just&amp;nbsp;drips 'sexy', doesn't it?)&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;They may be accompanied by this error message -- sent to the client app only, not logged in the SQL errorlogs: &lt;/span&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="COLOR: red"&gt;Server: Msg 8650, Level 13, State 1, Line 1 Intra-query parallelism caused your server command (process ID #51) to deadlock. Rerun the query without intra-query parallelism by using the query hint option (maxdop 1).&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/blockquote&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;An "&lt;em&gt;exchangeEvent&lt;/em&gt;" resource indicates the presence of parallelism operators in a query plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The idea is that the work for an operation like a large scan, sort, or join is divided up so that it can be executed on multiple child threads.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;There are "producer" threads that do the grunt work and feed sets of rows to "consumers".&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Intra-query parallel requires signaling between these worker threads: the consumers may have to wait on producers to hand them more data, and the producers may have to wait for consumers to finish processing the last batch of data.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Parallelism-related waits show up in SQL DMVs as &lt;em&gt;CXPACKET &lt;/em&gt;or &lt;em&gt;EXCHANGE &lt;/em&gt;wait types (note that the presence of these wait types is normal&amp;nbsp;and simply indicates the presence of parallel query execution -- by themselves, these waits don't indicate that this type or any other type of deadlock is occurring). &lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Wherever you have threads waiting for resources, there is a risk that they will end up in a circular blocking chain (thread A holding resource X and waiting for resource Y, thread B holding resource Y and waiting for resource X).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The synchronization objects used in parallel query execution are no exception; in rare cases, the threads running a single query can end up deadlocking with one another.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Most intra-query parallelism deadlocks are considered bugs, although some of them can be risky bugs to fix so a fix may not be possible.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If you run into one and you're already on the latest SQL service pack, your best bet may be to investigate workarounds.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Luckily, this type of deadlock is relatively uncommon, and in most cases it's possible to work around the problem by eliminating parallelism in the query.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Try one of these two approaches: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Workaround #1: &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="mso-bidi-font-family: Arial"&gt;Add an index or improve the query to eliminate the need for parallelism.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In most cases, the use of parallelism in a query indicates that you have a very large scan, sort, or join that isn't supported by proper indexes.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If you tune the query, you will often find that you end up with a much quicker and more efficient plan that doesn't use parallelism, and therefore isn't subject to this type of problem. Of course, in some queries (DSS/OLAP-type queries, in particular) it may be difficult to eliminate all large scans. &lt;br /&gt;&lt;br style="mso-special-character: line-break" /&gt;&lt;br style="mso-special-character: line-break" /&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Workaround #2: &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 7pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="mso-bidi-font-family: Arial"&gt;Force single-threaded execution with an "&lt;em&gt;OPTION (MAXDOP 1)&lt;/em&gt;" query hint at the end of the query.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If you can't modify the query, you can apply the hint to any query with a plan guide (assuming that you're running SQL 2005 or later). &lt;/span&gt;&lt;span style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;h1 style="MARGIN: 24pt 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: large"&gt;&lt;span style="FONT-FAMILY: ; COLOR: #365f91"&gt;&lt;span style="FONT-FAMILY: Cambria"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;"threadpool" Deadlock Resources&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 18pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: major-bidi"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;A process waiting for a "&lt;em&gt;threadpool&lt;/em&gt;" resource is actually waiting for a worker thread.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;There are a finite number of threads in SQL's thread pool, and if they are all in use, new requests must wait for an in-progress task to complete and free up a thread.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Thread pool waits (in DMVs, "&lt;em&gt;THREADPOOL&lt;/em&gt;" or "&lt;em&gt;UMSTHREAD&lt;/em&gt;" waittype) are typically a side effect of a massive resource contention problem -- most commonly, a large blocking chain.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;You should investigate what is tying up all of your worker threads, and eliminate that bottleneck.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;While it's not clear from this deadlock output alone, I suspect that in this case there may have been many other large untuned queries using lots of parallel threads, so excessive parallelism itself may have been the cause of the thread starvation.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;h1 style="MARGIN: 24pt 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: large"&gt;&lt;span style="FONT-FAMILY: ; COLOR: #365f91"&gt;&lt;span style="FONT-FAMILY: Cambria"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;"resourceWait" Deadlock Resources&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 18pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: major-bidi"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;(UPDATE: The following info on "resourceWait" was added to this post 15 Oct 2009.)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;A process waiting for a "resourceWait" resource is waiting for a "resource semaphore".&amp;nbsp; Resource semaphores are typically used to govern memory used for query sorts and hashes.&amp;nbsp; So the following wait graph:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;resource-list&lt;br /&gt;&amp;nbsp;&amp;nbsp; keylock hobtid=72057594038845440 dbid=6 objectname=XXXTABLE indexname=YYYINDEX id=lockffffffff81314cc0 mode=X associatedObjectId=72057594038845440&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; owner-list&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; owner id=processebb108 mode=X&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; waiter-list&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; waiter id=processebae38 mode=S requestType=wait&lt;br /&gt;&amp;nbsp;&amp;nbsp; resourceWait&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; owner-list&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; owner id=processebae38&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; waiter-list&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; waiter id=processebb108&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;could be read as: &lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Spid A is waiting for a shared key lock, but is blocked by Spid B, who holds an exclusive lock on this key.&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Spid B is waiting for more memory to run his query (and, eventually, to release the X key lock), but he is blocked by other spids, including Spid A, who are currently holding all of the memory available for this type of operation.&amp;nbsp; &lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;You could tackle this by looking for tuning opportunities in the queries run by both deadlock participants.&amp;nbsp; They are probably running a query plan that involves a hash or sort operation.&amp;nbsp; Remove this through indexing or query changes and you should eliminate the query's need to wait for a query memory grant.&amp;nbsp; You could also try throwing RAM at the problem, but keep in mind that query workspace memory, the memory used&amp;nbsp;for sorts and hashes, must be drawn from "visible buffer pool".&amp;nbsp; On a 32-bit box, visible bpool is limited to approximately 2GB (3GB if you are running with /3GB).&amp;nbsp; If SQL already has this much memory available to it, adding more won't help.&amp;nbsp; &lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1 style="MARGIN: 24pt 0in 0pt"&gt;&lt;span style="FONT-FAMILY: ; FONT-SIZE: large"&gt;&lt;span style="FONT-FAMILY: ; COLOR: #365f91"&gt;&lt;span style="FONT-FAMILY: Cambria"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;Caveats&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 18pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: major-bidi"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Just because you see &amp;ldquo;&lt;em&gt;exchangeEvent&lt;/em&gt;&amp;rdquo; resources in your deadlock graph doesn&amp;rsquo;t necessarily mean that you are facing an intra-query parallelism deadlock.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Sometimes the engine includes extraneous resources in the deadlock graph.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This makes it important to find out how the waiters relate to one another so that you can determine which of the resources is an essential part of the circular blocking chain.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;The post &lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;a href="http://blogs.msdn.com/bartd/archive/2006/09/09/Deadlock-Troubleshooting_2C00_-Part-1.aspx"&gt;&lt;span style="FONT-FAMILY: 'Verdana','sans-serif'; COLOR: blue"&gt;http://blogs.msdn.com/bartd/archive/2006/09/09/Deadlock-Troubleshooting_2C00_-Part-1.aspx&lt;/span&gt;&lt;/a&gt;&lt;/span&gt; steps you through a deconstruction of -T1222 output so that you can get a clearer understanding of the relationships.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;As a rule of thumb, if there are any lock resources in your deadlock output (&lt;em&gt;pagelock&lt;/em&gt;, &lt;em&gt;keylock&lt;/em&gt;, &lt;em&gt;rowlock&lt;/em&gt;) along with the exchangeEvent resources, you should suspect that the exchangeEvent resources are non-essential and that you are probably facing a &amp;ldquo;normal&amp;rdquo; deadlock.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;If you're interested in more background info on parallel query execution, there's a great presentation by Craig Freedman attached to this blog post: &lt;span style="FONT-FAMILY: 'Times New Roman','serif'"&gt;&lt;a href="http://blogs.msdn.com/craigfr/archive/2007/04/17/parallel-query-execution-presentation.aspx"&gt;&lt;span style="FONT-FAMILY: 'Verdana','sans-serif'; COLOR: blue"&gt;http://blogs.msdn.com/craigfr/archive/2007/04/17/parallel-query-execution-presentation.aspx&lt;/span&gt;&lt;/a&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8964067" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Locking/">SQL Locking</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Deadlocks/">SQL Deadlocks</category></item><item><title>Defining Complex Server "Health" Policies in SQL 2008</title><link>http://blogs.msdn.com/b/bartd/archive/2008/09/12/defining-complex-server-health-policies-in-sql-2008.aspx</link><pubDate>Fri, 12 Sep 2008 01:17:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8945620</guid><dc:creator>bartduncan</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=8945620</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=8945620</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2008/09/12/defining-complex-server-health-policies-in-sql-2008.aspx#comments</comments><description>&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;Policy Based Management (PBM) is a new feature in SQL Server 2008 that allows you to define a set of policies that capture the intended state for a group of servers.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, you could define a policy that says that your user databases should all have the &lt;i style="mso-bidi-font-style: normal"&gt;auto update statistics&lt;/i&gt; database option enabled. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;(If you&amp;rsquo;re not yet familiar with PBM, you can read more about it in Books Online or in the &lt;/span&gt;&lt;a href="http://blogs.msdn.com/sqlpbm/"&gt;&lt;span style="font-family: Verdana;"&gt;PBM blog&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Verdana;"&gt;.) &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;In SQL 2008, the focus of PBM is primarily on static aspects of server management &amp;ndash; policies covering things like schema requirements, or server and database configuration settings.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;However, there are certain more dynamic aspects of server state that are equally important, but much harder to monitor.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Server &amp;ldquo;health&amp;rdquo; monitoring (e.g. uptime, responsiveness) is one example.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I&amp;rsquo;m going to show you how you can use the &lt;b style="mso-bidi-font-weight: normal"&gt;ExecuteSql&lt;/b&gt; function to extend the normal capabilities of PBM by defining policies that ensure your servers are servicing queries effectively. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;You can use live Dynamic Management View (DMV) queries, or even query historical data that you are capturing in a Management Data Warehouse.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;IMPORTANT&lt;/i&gt;: Before going on, read through &lt;/span&gt;&lt;a href="http://blogs.msdn.com/sqlpbm/archive/2008/07/03/executesql.aspx"&gt;&lt;span style="font-family: Verdana;"&gt;this blog post&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Verdana;"&gt; for an overview of the PBM &lt;b style="mso-bidi-font-weight: normal"&gt;ExecuteSql&lt;/b&gt; function.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;Suppose you wanted to define a policy like this one: &lt;/span&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;&lt;em&gt;The average disk response time for all data and log files that have a non-trivial number of I/Os should not exceed 100ms.&amp;nbsp; &lt;/em&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;You can use a query like this one to find files that violate this policy: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;avg_ms_per_io&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;&amp;gt;&lt;/span&gt; 100 &lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: red"&gt;'Excessive disk response time ('&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;CONVERT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;avg_ms_per_io&lt;span style="COLOR: gray"&gt;))&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'ms) for file ID '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;CONVERT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;file_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;', database '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;database_name&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt; &lt;span style="COLOR: red"&gt;''&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; policy_violation_message&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;FROM&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;(&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;TOP&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;io_stall &lt;span style="COLOR: gray"&gt;/&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;num_of_reads &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; num_of_writes&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; avg_ms_per_io&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;io_stall &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; io_stall_ms&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;(&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;num_of_reads &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; num_of_writes&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; num_io&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;DB_NAME&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;database_id&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; database_name&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;file_id&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- Only check I/O stats for the current database&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; &lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_io_virtual_file_stats&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;DB_ID&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(),&lt;/span&gt; &lt;span style="COLOR: gray"&gt;null)&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- Ignore idle databases and files; we do not care about disk &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- wait time if the file has barely seen any I/O. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;(&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;num_of_reads &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; num_of_writes&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;&amp;gt;&lt;/span&gt; 500&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- PBM will use the first column of the first row to evaluate policy &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- compliance, so return the file with the longest avg I/O time.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;ORDER&lt;/span&gt; &lt;span style="COLOR: blue"&gt;BY&lt;/span&gt; 1 &lt;span style="COLOR: blue"&gt;DESC&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;)&lt;/span&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; slowest_file&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;o:p&gt;&lt;span style="font-family: Verdana;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;We&amp;rsquo;re going to wrap this query in PBM&amp;rsquo;s &lt;b style="mso-bidi-font-weight: normal"&gt;ExecuteSql()&lt;/b&gt; function, which will allow us to reference it within a policy condition.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;To permit PBM to bubble up more actionable info to the user (other than the simple fact that a database is out of policy), I&amp;rsquo;ve written the query to provide a string-type policy compliance indicator rather than a simple numeric 0/1 value.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If the query detects that a database is in policy, it returns an empty string for the [policy_violation_message] column.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;But if disk response time is found to be out of policy, the query returns a message identifying the offending file and its average disk response time.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This message will be logged with the policy results, which can help during postmortem diagnosis.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"&gt;&lt;span style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;span style="font-family: Verdana;"&gt;1.&lt;/span&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Create a new policy (in Management Studio under &lt;em&gt;Management&lt;/em&gt;\&lt;em&gt;Policies&lt;/em&gt;, right-click the &lt;em&gt;Policies&lt;/em&gt; folder and select &lt;em&gt;New Policy&lt;/em&gt;).&amp;nbsp; Name the policy &amp;ldquo;Disk Health&amp;rdquo;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"&gt;&lt;span style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;span style="font-family: Verdana;"&gt;2.&lt;/span&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;In the &lt;em&gt;Check Condition&lt;/em&gt; drop-down listbox, select &lt;em&gt;New condition&lt;/em&gt;&amp;hellip;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"&gt;&lt;span style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;span style="font-family: Verdana;"&gt;3.&lt;/span&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Name the condition &amp;ldquo;Disk Response Time is Healthy&amp;rdquo;, and set the &lt;em&gt;Facet&lt;/em&gt; drop-down listbox to &lt;em&gt;Database&lt;/em&gt;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"&gt;&lt;span style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;span style="font-family: Verdana;"&gt;4.&lt;/span&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Click the &amp;ldquo;&amp;hellip;&amp;rdquo; button next to the &lt;em&gt;Field&lt;/em&gt; textbox. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;In the &lt;em&gt;Advanced Edit&lt;/em&gt; dialog, enter the text shown below, then click OK (this is the same query as above, except that single quotes have been escaped by doubling them).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;ExecuteSql('&lt;span style="COLOR: blue"&gt;String&lt;/span&gt;', '&lt;span style="COLOR: blue"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;DECLARE @max_allowed_ms_per_io int;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;SET @max_allowed_ms_per_io = 100;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;SELECT &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CASE &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHEN MAX (avg_ms_per_io) &amp;gt; @max_allowed_ms_per_io THEN &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;''Excessive disk response time ('' &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;+ CONVERT (varchar, MAX (avg_ms_per_io)) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;+ ''ms) for file ID '' + CONVERT (varchar, MAX (file_id))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;+ '', database '' + MAX (database_name)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ELSE ''''&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;END AS policy_violation_message&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;FROM&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;(&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT TOP 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;io_stall / (num_of_reads + num_of_writes) AS avg_ms_per_io, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;io_stall AS io_stall_ms, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(num_of_reads + num_of_writes) AS num_io, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DB_NAME (database_id) AS database_name, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;file_id &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- Only check I/O stats for the current database&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM sys.dm_io_virtual_file_stats (DB_ID(), null) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- Ignore idle databases and files; we do not care about disk &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- wait time if the file has barely seen any I/O. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(num_of_reads + num_of_writes) &amp;gt; 500&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- PBM will use the first column of the first row to evaluate policy &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- compliance, so return the file with the longest avg I/O time.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ORDER BY 1 DESC&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;) AS slowest_file;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;o:p&gt;&lt;span style="font-family: Verdana;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoListParagraph" style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"&gt;&lt;span style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;span style="font-family: Verdana;"&gt;5.&lt;/span&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Set the &lt;em&gt;Operator&lt;/em&gt; to equals (=) and the &lt;em&gt;Value &lt;/em&gt;to '' (&lt;span style="text-decoration: underline;"&gt;Note&lt;/span&gt;: that&amp;rsquo;s two single quotes, not a double quote).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;Your new policy should look like this.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If it does, click OK twice to create the policy. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;span style="font-family: Verdana;"&gt;&lt;img src="http://blogs.msdn.com/bartd/attachment/678015.ashx" /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;o:p&gt;&lt;span style="font-family: Verdana;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;Now right-click on your policy and select &lt;i style="mso-bidi-font-style: normal"&gt;Evaluate&lt;/i&gt;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Click the &lt;i style="mso-bidi-font-style: normal"&gt;Evaluate&lt;/i&gt; button in the lower-right portion of the &lt;i style="mso-bidi-font-style: normal"&gt;Evaluate Policies&lt;/i&gt; dialog, and acknowledge that you realize that the policy includes a custom script (see the blog post I referenced earlier for more information about the security implications of this approach).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This will run the policy against each database on your instance, and will flag any that are suffering from poor disk response time.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In the screenshot below, you can see that my [mdw] and [testdatabase] databases are in policy, but my [AdventureWorks] database is out of policy: the average I/O response time for file 1 in this database is an embarrassingly slow 122ms. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;span style="font-family: Verdana;"&gt;&lt;img src="http://blogs.msdn.com/bartd/attachment/8945620.ashx" /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;Because &lt;b style="mso-bidi-font-weight: normal"&gt;ExecuteSql&lt;/b&gt; takes an arbitrary query, you can define a policy that monitors literally any aspect of server health that you can write a query to detect.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Below, for example, is a query that will trigger a policy violation if the value of the &amp;ldquo;&lt;i style="mso-bidi-font-style: normal"&gt;SQLServer:Buffer Manager\Page Life Expectancy&lt;/i&gt;&amp;rdquo; perfmon counter drops too low: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;ExecuteSql('&lt;span style="COLOR: blue"&gt;String&lt;/span&gt;', '&lt;span style="COLOR: blue"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;SELECT &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CASE &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHEN cntr_value &amp;lt; 300 THEN &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;''Page life expectancy (sec): ''&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;+ CONVERT (varchar, cntr_value)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ELSE ''''&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;END AS policy_violation_message&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;FROM sys.dm_os_performance_counters &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;WHERE counter_name = ''Page life expectancy'' AND object_name LIKE ''%Buffer Manager%'';&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;o:p&gt;&lt;span style="font-family: Verdana;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;span style="font-family: Cambria; color: #4f81bd; font-size: medium;"&gt;SQL Health Monitoring Policies and Management Data Warehouse&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;If you&amp;rsquo;ve spent much time trying to use SQL DMVs for server health monitoring in the past, you&amp;rsquo;ve probably run into situations where you needed to compare the live DMV data to an historical snapshot of the same data.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, consider the &amp;ldquo;slow I/O&amp;rdquo; query at the top of this post.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The I/O counters and disk &amp;ldquo;stall&amp;rdquo; time reported in &lt;b style="mso-bidi-font-weight: normal"&gt;sys.dm_os_virtual_file_stats&lt;/b&gt; are cumulative totals since the SQL instance was started.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If the server had been running a long time, these values would be very large, and any disk performance problem would have to exist for a long time before the overall average I/O response time grew high enough to trigger the threshold.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;What you typically want in a case like this is to assess &lt;i style="mso-bidi-font-style: normal"&gt;recent&lt;/i&gt; I/O performance, where &amp;ldquo;recent&amp;rdquo; means the average within the last several minutes or hours.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; Here's a more refined version of the policy we started with, that would make this type of monitoring more useful in the real world: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Verdana;"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;&lt;span style="mso-spacerun: yes"&gt;
&lt;blockquote&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;&lt;em&gt;The average disk response time for all data and log files that have a non-trivial number of I/Os should not exceed 100ms (where "average disk response time" is defined as the average I/O wait time for all&amp;nbsp;reads or writes to a file within the last 30 minutes)&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;This requires historical snapshots of the same data to compare the most recent data to.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Luckily, this happens to be exactly what the Data Collector and Management Data Warehouse (MDW) features in SQL 2008 were intended to provide.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Being able to take advantage of historical data in your health monitoring policies has a number of advantages: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo3"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&amp;middot;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;For cumulative-since-server-startup DMV data, the policy will flag problems with less delay&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo3"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&amp;middot;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Similarly, a&amp;nbsp;monitored object can go back &amp;ldquo;in policy&amp;rdquo; relatively quickly once the problem has been addressed&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo3"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&amp;middot;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;For metrics that are instantaneous measurements, you get fewer false positive policy violations if you can average a set of recent measurements instead of focusing on a single instantaneous data point that might be an atypical value&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;The query below pulls recent disk stats from a local MDW database (this DMV&amp;rsquo;s data is collected by the built-in Server Activity collection set, so you don&amp;rsquo;t need to create a custom collection set).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This assumes that (a) the monitored SQL instance is hosting its own MDW database, and (b) the MDW database&amp;rsquo;s name is &amp;ldquo;MDW&amp;rdquo;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If your MDW database is local but is named something other than "MDW", update the query text to reference the correct MDW database. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;(If you need a walkthough showing how to set up MDW, see &lt;/span&gt;&lt;a href="http://www.sql-server-performance.com/articles/per/Management_Data_Warehouse_p1.aspx"&gt;&lt;span style="font-family: Verdana;"&gt;this page&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Verdana;"&gt;.)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;ExecuteSql('&lt;span style="COLOR: blue"&gt;String&lt;/span&gt;', '&lt;span style="COLOR: blue"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;DECLARE @max_allowed_ms_per_io int;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;DECLARE @time_window_min int;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;SET @max_allowed_ms_per_io = 100;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;SET @time_window_min = 120;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;SELECT &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;CASE &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHEN MAX (recent_avg_ms_per_io) &amp;gt; @max_allowed_ms_per_io THEN &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;''Excessive disk response time ('' &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;+ CONVERT (varchar, MAX (recent_avg_ms_per_io)) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;+ ''ms) for file ID '' + CONVERT (varchar, MAX (file_id))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;+ '', database '' + MAX (database_name)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ELSE ''''&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;END AS policy_violation_message&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;FROM&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;(&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT TOP 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(recent_io_stall_read_ms + recent_io_stall_write_ms) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;/ (recent_read_count + recent_write_count + 1) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;AS recent_avg_ms_per_io, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(recent_io_stall_read_ms + recent_io_stall_write_ms) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AS recent_io_stall_ms, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(recent_read_count + recent_write_count) AS recent_io_count, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;file_id &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;file_id, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MAX (io_stall_read_ms) - MIN (io_stall_read_ms) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AS recent_io_stall_read_ms, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MAX (io_stall_write_ms) - MIN (io_stall_write_ms) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AS recent_io_stall_write_ms, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MAX (num_of_reads) - MIN (num_of_reads) AS recent_read_count, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MAX (num_of_writes) - MIN (num_of_writes) AS recent_write_count &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM mdw.snapshots.io_virtual_file_stats AS fs&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;INNER JOIN mdw.core.snapshots AS snap &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ON fs.snapshot_id = snap.snapshot_id &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- MDW data for the local instance only&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;snap.instance_name = @@SERVERNAME &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- I/O stats only for the current database&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AND fs.database_id = DB_ID()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- within the last 5 minutes&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AND fs.collection_time &amp;gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DATEADD (minute, @time_window_min, SYSDATETIMEOFFSET())&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GROUP BY database_name, file_id &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;) AS recent_io_stats&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- Ignore idle databases and files; we do not care about disk &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- wait time if the file has barely seen any I/O. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(recent_read_count + recent_write_count) &amp;gt; 500&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- PBM will use the first column of the first row to evaluate policy &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-- compliance, so return the file with the longest avg I/O time.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ORDER BY 1 DESC&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;) AS slowest_file;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-bidi-font-size: 8.0pt"&gt;')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;o:p&gt;&lt;span style="font-family: Verdana;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;I think this approach to server health monitoring could be useful, but it does have some limitations that are worth noting: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo1"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&amp;middot;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;This release of SQL doesn&amp;rsquo;t provide out-of-the-box policies of this sort.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;You have to write custom out-of-policy detection queries for any complex server health condition you want to monitor.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo1"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&amp;middot;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;Complex policies may have parameters that you might want to tweak on a per-server basis (in this example, the max allowed disk response time is one such parameter).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Unfortunately, there is no facility for this in PBM in SQL 2008.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If you really need this, you could create your own custom policy configuration table on each server with thresholds particular to that server, and join to this table in your custom condition query. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo1"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&amp;middot;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana;"&gt;When you use ExecuteSql, you can only choose &amp;ldquo;=&amp;rdquo; or &amp;ldquo;!=&amp;rdquo; for your operators.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Because you can&amp;rsquo;t use inequality operators, you must push your policy violation thresholds down into the query itself.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="font-family: Verdana;"&gt;If you&amp;rsquo;ve tried this approach to health monitoring and have experiences you can share, good or bad, let me know. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0in 0in 10pt"&gt;&lt;o:p&gt;&lt;span style="font-family: Verdana;"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8945620" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-08-94-56-20/pbm_5F00_health_5F00_eval.png" length="96843" type="image/x-png" /><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Policy+Based+Management/">Policy Based Management</category></item><item><title>Query Fingerprints and Plan Fingerprints (The Best SQL 2008 Feature That You've Never Heard Of)</title><link>http://blogs.msdn.com/b/bartd/archive/2008/09/03/query-fingerprints-and-plan-fingerprints_3A00_-the-best-new-sql-2008-feature-you_2700_ve-never-heard-of.aspx</link><pubDate>Wed, 03 Sep 2008 22:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8922629</guid><dc:creator>bartduncan</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=8922629</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=8922629</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2008/09/03/query-fingerprints-and-plan-fingerprints_3A00_-the-best-new-sql-2008-feature-you_2700_ve-never-heard-of.aspx#comments</comments><description>&lt;p&gt;&lt;span style="font-family: Verdana;"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-family: Verdana;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In versions of SQL Server before SQL Server 2008, it can be difficult to determine the cumulative cost of the queries running on a server if the workload includes unparameterized queries.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The only truly reliable method is to capture a Profiler trace during a representative time period, then post-process the trace with a utility that strips out any inline literal values from the query text.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;A number of utilities have sprung up that use this general approach: ReadTrace, SQL Nexus/TraceBuster, and Bill Graziano&amp;rsquo;s ClearTrace utility are three that I know of.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Trace-based query cost analysis is effective, but there are a number of big problems with this approach: capturing a batch- or statement-level profiler trace is expensive and sometimes can slow down your server, a trace can grow up to several GB per minute on servers that have a high transaction rate, and the capture and analysis of the data tends to be a time-consuming and labor-intensive process that is difficult to automate. &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In SQL Server 2008, the SQL Server database engine has a powerful new feature that generates an identifier for each query.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The identifier is independent of any inline parameter values, so it serves as a very effective query identifier.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This identifier &amp;ndash; sometimes called a &amp;ldquo;query fingerprint&amp;rdquo; &amp;ndash; enables a fairly robust method of identifying the most expensive queries on your server based on nothing but DMV queries.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I think the feature will eventually form the basis of a query cost analysis approach that requires a much smaller investment of DBA time, has greatly reduced risk, and scales to higher-volume workloads than a traditional trace-based analysis.&amp;nbsp;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;This is my personal favorite &amp;ldquo;sleeper feature&amp;rdquo; in SQL 2008; the query optimizer team deserves kudos for getting it done in time for the release, and for making it inexpensive enough that fingerprint generation can be on-by-default.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It was a very late addition to the release, which I think is the main reason that fingerprints have generated relatively little buzz so far.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; Plan fingerprints are used to calculate query plan cost in the new Activity Monitor tool that you can launch by right-clicking a server in SQL Server 2008's Management Studio.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;To fully appreciate query fingerprints&amp;rsquo; value, you first must understand one of the limitations of the &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats&lt;/strong&gt;&lt;/span&gt; Dynamic Management View (DMV) that was introduced in SQL Server 2005.&amp;nbsp; This DMV lists every statement query plan that is in procedure cache at that moment, along with execution stats for the query plan such as the number of executions of the plan, total CPU cost, physical and logical reads, and so on.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It was a groundbreaking addition to SQL Server because, for the first time, it allowed a DBA to examine the cost of the queries in a workload without capturing a Profiler trace.&amp;nbsp; For certain workloads, the original &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats&lt;/strong&gt;&lt;/span&gt; DMV provides a simple and powerful way to identify the most expensive queries: you capture two snapshots of the DMV, then you join the second snapshot back to the first to calculate the execution cost of each query plan in the time between the two snapshots.&amp;nbsp; But the DMV also has some limitations that constrain its usefulness.&amp;nbsp; In particular, if a query is not explicitly or implicitly parameterized and if the query text contains inline literal values, that query plan will not be reused. Every execution of the query with a different set of parameter values will generate a new compiled plan object.&amp;nbsp; You can see this by running these queries: &lt;/p&gt;
&lt;p class="CodeCxSpFirst"&gt;&lt;span style="COLOR: #00b050"&gt;&lt;span style="font-family: Courier New;"&gt;-- Run "the same" query twice, but with a different inline parameter &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: #00b050"&gt;&lt;span style="font-family: Courier New;"&gt;-- value for each execution&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;type&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; sys&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;objects &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; name &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'sysfiles1'&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;type&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; sys&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;objects &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; name &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'sysprivs'&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle"&gt;&lt;span style="COLOR: #00b050"&gt;&lt;span style="font-family: Courier New;"&gt;-- Find the query execution statistics row(s) for the query&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;sql_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; plan_handle&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; execution_count&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; sys&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;dm_exec_query_stats &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; qs&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: gray"&gt;CROSS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;APPLY&lt;/span&gt; sys&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;dm_exec_sql_text&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;qs&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;plan_handle&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; &lt;span style="COLOR: blue"&gt;sql&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;sql&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: blue"&gt;text&lt;/span&gt; &lt;span style="COLOR: gray"&gt;LIKE&lt;/span&gt; &lt;span style="COLOR: red"&gt;'SELECT type FROM sys.objects WHERE name %'&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Here's the output: &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;img src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-66-46-QueryHashStats/2605.sample_5F00_plan_5F00_handle.png" /&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;The two queries that look like &amp;ldquo;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;span style="COLOR: blue"&gt;type&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; &lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;objects&lt;/span&gt; &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; name &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'x'&lt;/span&gt;&lt;/span&gt;&amp;rdquo; are identical except for a different inline parameter value, but each query&amp;rsquo;s statistics are tracked in different rows in &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats&lt;/strong&gt;&lt;/span&gt;.&amp;nbsp; This happens because the query is not explicitly parameterized, and the different inline literal values cause SQL Server to compile a separate query plan each time the query is executed with a different parameter value.&amp;nbsp; This behavior makes it difficult to use &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats&lt;/strong&gt;&lt;/span&gt; to identify the true cumulative cost of a query.&amp;nbsp; Suppose that this query was executed frequently enough to be the most expensive query on your server, but suppose that most executions had a different search parameter value.&amp;nbsp; With the query&amp;rsquo;s cumulative cost spread out across thousands of rows in &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats&lt;/strong&gt;&lt;/span&gt;, how would you recognize that each of those thousands of rows actually represents a fraction of the total execution cost of a single query?&amp;nbsp; This is a key problem that the query fingerprint feature in SQL Server 2008 helps to address.&amp;nbsp; To see how it works, add these queries to your query window, and then re-execute the entire script: &lt;/p&gt;
&lt;p class="CodeCxSpFirst"&gt;&lt;span style="COLOR: #00b050"&gt;&lt;span style="font-family: Courier New;"&gt;-- Execute a second query that is not quite the same shape as the prior query&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;type&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; sys&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;objects &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; name &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'sysprivs'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; create_date &lt;span style="COLOR: gray"&gt;&amp;lt;&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;GETDATE&lt;/span&gt;&lt;span style="COLOR: gray"&gt;()&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle"&gt;&lt;span style="COLOR: #00b050"&gt;&lt;span style="font-family: Courier New;"&gt;-- Find the query execution statistics row(s) for the query&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;sql_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; plan_handle&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; execution_count&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; query_hash&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; query_plan_hash&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; sys&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;dm_exec_query_stats &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; qs&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: gray"&gt;CROSS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;APPLY&lt;/span&gt; sys&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;dm_exec_sql_text&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;qs&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;plan_handle&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; &lt;span style="COLOR: blue"&gt;sql&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;sql&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: blue"&gt;text&lt;/span&gt; &lt;span style="COLOR: gray"&gt;LIKE&lt;/span&gt; &lt;span style="COLOR: red"&gt;'SELECT type FROM sys.objects WHERE name %'&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;When you re-run the script, you should see output like the following: &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;img src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-66-46-QueryHashStats/0647.sample_5F00_query_5F00_hash.png" /&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Note the &lt;span class="SQLName"&gt;&lt;strong&gt;query_hash&lt;/strong&gt;&lt;/span&gt; and &lt;span class="SQLName"&gt;&lt;strong&gt;query_plan_hash&lt;/strong&gt;&lt;/span&gt; columns that are new to &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats&lt;/strong&gt;&lt;/span&gt; in SQL Server 2008.&amp;nbsp; This is one of two places where the new SQL Server 2008 query fingerprint and query plan fingerprint features are exposed (the other is the &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_requests&lt;/strong&gt;&lt;/span&gt; DMV).&amp;nbsp; Note that the two queries we ran first have the same &lt;span class="SQLName"&gt;&lt;strong&gt;query_hash&lt;/strong&gt;&lt;/span&gt; value, which indicates that if you were to strip out any inline literals, the two queries have the same &amp;ldquo;shape&amp;rdquo;.&amp;nbsp; They also have the same &lt;span class="SQLName"&gt;&lt;strong&gt;query_plan_hash&lt;/strong&gt;&lt;/span&gt; value, which means that even though they each had their own compiled plan object in the procedure cache, those two query plans also had the same general form.&amp;nbsp; However, the second query doesn&amp;rsquo;t have quite the same &amp;ldquo;shape&amp;rdquo; as the first two queries; it includes an additional filter predicate (&amp;ldquo;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;create_date &lt;span style="COLOR: gray"&gt;&amp;lt;&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;GETDATE&lt;/span&gt;&lt;span style="COLOR: gray"&gt;()&lt;/span&gt;&lt;/span&gt;&amp;rdquo;), and therefore has different &lt;span class="SQLName"&gt;&lt;strong&gt;query_hash&lt;/strong&gt;&lt;/span&gt; and &lt;span class="SQLName"&gt;&lt;strong&gt;query_plan_hash&lt;/strong&gt;&lt;/span&gt; values.&amp;nbsp; Using these two new columns, we can combine the costs of all plans that have the same shape and calculate the true cumulative cost of all executions of a query.&amp;nbsp; This approach generally works well even when faced with an unparameterized workload that features poor plan reuse.&amp;nbsp; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Below I've provided some more detailed information about query fingerprints and query plan fingerprints.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This information should be accurate as of the initial release of SQL Server 2008, but it is possible that some of the implementation details will change in subsequent releases.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In order to protect the ability to improve the feature, we&amp;rsquo;re not providing an iron-clad guarantee that a query&amp;rsquo;s fingerprint won&amp;rsquo;t change across SQL versions, although we recognize the value of an identifier that remains constant across releases and plan to avoid changes to the fingerprint calculation algorithms unless there&amp;rsquo;s a very good reason to change them. &lt;/p&gt;
&lt;h2 style="MARGIN: 0.25in 0in 0pt"&gt;&lt;a name="_Toc208196095" title="_Toc208196095"&gt;&lt;/a&gt;&lt;span style="font-family: Cambria; color: #4f81bd; font-size: medium;"&gt;Query Fingerprints&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal"&gt;A query fingerprint is also called a &amp;ldquo;query hash&amp;rdquo;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The queries below are all similar enough to have the same query fingerprint: &lt;/p&gt;
&lt;p class="CodeCxSpFirst"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'A'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; column2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'B'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'X'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; column2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'Y'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;100&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; column2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;200&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; foo.column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'X'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; foo.column2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'Y'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;On the other hand, all of the queries below are have differences that are significant enough to produce different query fingerprints: &lt;/p&gt;
&lt;p class="CodeCxSpFirst"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'X'&lt;/span&gt; &lt;span style="COLOR: red"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; foo2 &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'X'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND &lt;/span&gt;column2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'Y'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'X'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; column2 &lt;span style="COLOR: gray"&gt;IS NULL&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'X'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;OR &lt;/span&gt;column2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'Y'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; column1 &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; foo &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; column1 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'X'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;AND&lt;/span&gt; column2 &lt;span style="COLOR: gray"&gt;&amp;gt;&lt;/span&gt; &lt;span style="COLOR: red"&gt;'Y'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Here are some facts that help clarify how query fingerprints are generated: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;Query fingerprints are generated from a tree of logical operators that is used as an input to the query optimizer.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Because this tree is created prior to query optimization, a query fingerprint is influenced only by the query&amp;rsquo;s text, not by the query plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In other words, two queries may have the same query fingerprint but use two very different query plans.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;&lt;span style="mso-spacerun: yes"&gt;Similarly, because the query fingerprint is generated from an operator tree *after* parsing, two queries don't have to have the exact same text to have the same fingerprint.&amp;nbsp; Whitespace and comments don't matter, and the queries can even have some small, semantically-irrelevant differences.&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;Query fingerprints are not affected by the current database context or by the current instance name.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This means that the same query run within two different databases, or even two different SQL Server instances, will generally have the same query fingerprint value.&amp;nbsp;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;The table, view, and function names referenced by two queries must be identical for the queries to have the same fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This includes table aliases; if two queries are identical except that one of them uses a table alias and the other query refers to the table using its actual name, the two queries will not have the same fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;If a query has any table or query hints (including any hints applied via a plan guide), the hints must be identical in order to generate the same query fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;SET options may influence query fingerprints if they change the query semantics.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, the logical meaning of a predicate like &amp;ldquo;column1 = NULL&amp;rdquo; is influenced by the current ANSI_NULLS setting.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If the query includes a predicate like this, then changing the ANSI_NULLS setting may generate a different query fingerprint. &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 style="MARGIN: 0.25in 0in 0pt"&gt;&lt;a name="_Toc208196096" title="_Toc208196096"&gt;&lt;/a&gt;&lt;span style="font-family: Cambria; color: #4f81bd; font-size: medium;"&gt;Plan Fingerprints&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal"&gt;A query plan fingerprint may also be referred to as a &amp;ldquo;plan hash&amp;rdquo;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Plan fingerprints are generated from the tree of physical operators that makes up a compiled query execution plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Generally speaking, if a user would consider two plans to be different, they will have different plan fingerprint values.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In order to have the same plan fingerprint value, the trees of operators that make up the plans must have the same shape.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For each physical operator in one plan, the corresponding node in the other plan must be the same physical operator.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, if two plans have the same general shape but one plan includes a Hash Join operator where the second plan uses a Loop Join, the two plans will not have the same plan fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Certain operator attributes must also be identical in order to generate a matching plan fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, two Table Scan operator must reference the same table name, or they will not match.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;However, not every attribute of an operator is included in the query plan hash value.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, the specific number of rows that is estimated to be returned from a Table Scan operator does not influence the plan fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Two cached plans may have slightly different estimated row counts, yet have the exact same shape and the same execution characteristics, and therefore the same plan fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Like query fingerprints, a plan fingerprint is not affected by database context or SQL Server instance name, so if similarly-shaped query plans are used in two different databases, the plans will receive the same plan fingerprint.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Plan fingerprints are sensitive to object names, but an exception exists for automatically-generated primary key and unique key constraints.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;(Unfortunately, due to an issue in the initial release of SQL Server 2008, a query that references a local temporary table will generate different plan and query fingerprints for each execution.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Hopefully, this will be addressed in a subsequent Cumulative Update or Service Pack release.)&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Finally, in the initial implementation of the plan fingerprint feature, a statement&amp;rsquo;s query fingerprint value is included in the plan hash.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In other words, two different queries with different query fingerprints may result in the &amp;ldquo;same&amp;rdquo; plan, but the plan fingerprints will be different simply because the query fingerprints are different.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;h2 style="MARGIN: 0.25in 0in 0pt"&gt;&lt;span style="font-family: Cambria; color: #4f81bd; font-size: medium;"&gt;Limitations&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal"&gt;Here are some of the more important restrictions that you should be aware of: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;In SQL 2008, the query and plan fingerprints are still tied to the &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats &lt;/strong&gt;&lt;/span&gt;DMV, which means that the aggregate stats for a fingerprint are associated with cached query plans.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Query plans have a transient lifetime, and may be removed at any time in response to internal or external memory pressure on the procedure cache.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Any query statistics that are inserted and removed in the interval between two queries against the DMV will not be reflected in your query cost estimates (you can partially compensate for this by querying the DMV at a more frequent interval). &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;Also, certain types of query plan are never inserted into the procedure cache (one example is the plan for a CREATE INDEX).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Execution statistics for these types of queries may be undercounted.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;The &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_query_stats&lt;/strong&gt;&lt;/span&gt; DMV only shows statistics for completed query executions.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In-progress, long-running queries will not show up in the DMV until they finish running.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;You can merge the stats from this DMV with the the &lt;span class="SQLName"&gt;&lt;strong&gt;sys.dm_exec_requests&lt;/strong&gt;&lt;/span&gt; DMV (which also exposes the new fingerprint columns) in order to get a more complete view. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal" style="MARGIN: 0in 0in 12pt"&gt;Using fingerprints to determine cumulative query cost relies on using the new &lt;span class="SQLName"&gt;&lt;strong&gt;query_hash&lt;/strong&gt;&lt;/span&gt; and &lt;span class="SQLName"&gt;&lt;strong&gt;query_plan_hash&lt;/strong&gt;&lt;/span&gt; columns as keys that uniquely identify a particular query&amp;rsquo;s or plan&amp;rsquo;s &amp;ldquo;shape&amp;rdquo;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It is possible, though unlikely, that two different queries may end up with the same hash value, causing statistics for both of the queries to be charged to one of them.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It is also possible that two variations of the &amp;ldquo;same&amp;rdquo; query may be assigned different fingerprint hash values, in which case the cost of the query may appear to be spread out over several buckets, making it difficult to recognize the query&amp;rsquo;s true cumulative cost.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;At the time of the initial release of SQL Server 2008, we&amp;rsquo;ve only encountered one instance of this: different executions of a query that references a temp table may be assigned different &lt;span class="SQLName"&gt;&lt;strong&gt;query_hash&lt;/strong&gt;&lt;/span&gt; values.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This problem with temp tables isn&amp;rsquo;t intentional; it&amp;rsquo;s a bug, and I&amp;rsquo;m hopeful that it will be fixed in an upcoming Service Pack or Cumulative Update.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 style="MARGIN: 0.25in 0in 0pt"&gt;&lt;span style="font-family: Cambria; color: #4f81bd; font-size: medium;"&gt;Future Plans&lt;/span&gt;&lt;/h2&gt;
&lt;p class="MsoNormal"&gt;I already mentioned the new SQL 2008 Activity Monitor, which uses plan fingerprints to generate more accurate estimates of query cost than would otherwise be possible.&amp;nbsp; Looking forward, we have plans to publish a new custom collection set based on the SQL Server 2008 Data Collector that uses plan fingerprints to provide a Top N Query identification tool. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;You will be able to use this new &amp;ldquo;Query Hash Statistics&amp;rdquo; collection set as-is, or customize it to meet your needs.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The documentation that accompanies this collection set will include a subset of the info in this post, so if you&amp;rsquo;ve read to this point, you&amp;rsquo;ll be able to skim over some of the collection set docs.&amp;nbsp; (Update: &lt;a href="http://blogs.msdn.com/b/bartd/archive/2010/11/03/query-hash-statistics-a-query-cost-analysis-tool-now-available-for-download.aspx"&gt;Query Hash Statistics has shipped&lt;/a&gt;.)&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;I suspect that it won&amp;rsquo;t be terribly long before your favorite SQL monitoring tool starts to take advantage of this feature, but if you&amp;rsquo;re of the roll-your-own bent, you can start creating your own custom query analysis scripts right now using the info provided here.&amp;nbsp;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;Below I've provided&amp;nbsp;a query to get you started. It groups on &lt;strong&gt;query_plan_hash&lt;/strong&gt; to calculate query statistics for all plans with a given plan fingerprint, and pulls the statement text from a representative cached plan object that has a given fingerprint.&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;blockquote&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;TOP&lt;/span&gt;&lt;span style="color: #000000;"&gt; 100&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;query_hash&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; query_plan_hash&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;cached_plan_object_count&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;execution_count&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;total_cpu_time_ms&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_elapsed_time_ms&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;total_logical_reads&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_logical_writes&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_physical_reads&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;sample_database_name&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_object_name&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;sample_statement_text&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;FROM&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;query_hash&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; query_plan_hash&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;COUNT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(*)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; cached_plan_object_count&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;plan_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_plan_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;execution_count&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; execution_count&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;total_worker_time&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)/&lt;/span&gt;&lt;span style="color: #000000;"&gt;1000 &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_cpu_time_ms&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;total_elapsed_time&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)/&lt;/span&gt;&lt;span style="color: #000000;"&gt;1000 &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_elapsed_time_ms&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;total_logical_reads&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_logical_reads&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;total_logical_writes&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_logical_writes&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;total_physical_reads&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_physical_reads&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_exec_query_stats&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;GROUP&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BY&lt;/span&gt;&lt;span style="color: #000000;"&gt; query_hash&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; query_plan_hash&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;)&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; plan_hash_stats&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;CROSS&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;APPLY&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;TOP&lt;/span&gt;&lt;span style="color: #000000;"&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: blue"&gt;sql_handle&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_sql_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;statement_start_offset &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_statement_start_offset&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;statement_end_offset &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_statement_end_offset&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt;&lt;span style="color: #000000;"&gt; [database_id]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;value &lt;/span&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; 32768 &lt;/span&gt;&lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: red"&gt;'ResourceDb'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;DB_NAME&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;CONVERT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; [database_id]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="COLOR: gray"&gt;))&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_database_name&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;OBJECT_NAME&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;CONVERT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; [object_id]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;CONVERT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; [database_id]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="COLOR: gray"&gt;))&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_object_name&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;SUBSTRING&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;sql&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;[text]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;(&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt;qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;statement_start_offset&lt;/span&gt;&lt;span style="COLOR: gray"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;2&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; 1&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt;&lt;span style="color: #000000;"&gt; qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;statement_end_offset &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt;1 &lt;/span&gt;&lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;DATALENGTH&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;sql&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;[text]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt;&lt;span style="color: #000000;"&gt; 0 &lt;/span&gt;&lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;DATALENGTH&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;sql&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;[text]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt;&lt;span style="color: #000000;"&gt; qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;statement_end_offset &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;statement_start_offset&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)/&lt;/span&gt;&lt;span style="color: #000000;"&gt;2&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_statement_text&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;plan_hash_stats&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;sample_plan_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;sql&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;INNER&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;JOIN&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_exec_query_stats&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; qs &lt;/span&gt;&lt;span style="COLOR: blue"&gt;ON&lt;/span&gt;&lt;span style="color: #000000;"&gt; qs&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;plan_handle &lt;/span&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; plan_hash_stats&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;sample_plan_handle &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;CROSS&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;APPLY&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_exec_plan_attributes&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;plan_hash_stats&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;sample_plan_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; [object_id]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;CROSS&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;APPLY&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_exec_plan_attributes&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="color: #000000;"&gt;plan_hash_stats&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;sample_plan_handle&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; [database_id]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;span style="color: #000000;"&gt; [object_id]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;attribute &lt;/span&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: red"&gt;'objectid'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;span style="color: #000000;"&gt; [database_id]&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="color: #000000;"&gt;attribute &lt;/span&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: red"&gt;'dbid'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;)&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sample_query_text&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;ORDER&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-size: 10.0pt"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;BY&lt;/span&gt;&lt;span style="color: #000000;"&gt; total_cpu_time_ms &lt;/span&gt;&lt;span style="COLOR: blue"&gt;DESC&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/blockquote&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8922629" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-08-92-26-29/sample_5F00_query_5F00_hash.png" length="77728" type="image/x-png" /><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category></item><item><title>A simpler way to convert a hexadecimal string to varbinary</title><link>http://blogs.msdn.com/b/bartd/archive/2007/07/26/a-simpler-way-to-converting-a-hexadecimal-string-to-varbinary.aspx</link><pubDate>Thu, 26 Jul 2007 03:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4052636</guid><dc:creator>bartduncan</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=4052636</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=4052636</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2007/07/26/a-simpler-way-to-converting-a-hexadecimal-string-to-varbinary.aspx#comments</comments><description>&lt;P&gt;This isn't perf-related like most of my earlier posts, but I thought it was useful enough that I should share it.&amp;nbsp; We recently had a situation where we had to convert a hexadecimal string representation of a binary value to a true binary (e.g.&amp;nbsp;varchar value &lt;FONT color=#ff0000&gt;'0x1234abcdef'&lt;/FONT&gt;&amp;nbsp;--&amp;gt; varbinary 0x1234ABCDEF).&amp;nbsp; There's a built-in function in SQL&amp;nbsp;(fn_varbintohexstr) to convert from&amp;nbsp;varbinary to a hex-formatted&amp;nbsp;varchar value, but nothing to convert in the opposite direction.&amp;nbsp;&amp;nbsp;A smart guy on my team (Ahmed Ayad)&amp;nbsp;came up with a clever solution.&amp;nbsp; There are other solutions floating around out there that do this same conversion, but I don't think I've ever run across one as nice and tidy as this.&amp;nbsp; It takes advantage of the fact that SQL's parser already knows how to convert a hex string representiation of a binary into a "real" binary.&amp;nbsp; &lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;IF&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;OBJECT_ID&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'usp_hexstrtovarbin'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'P'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;IS&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;NOT&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;NULL&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;DROP&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;PROC&lt;/FONT&gt;&lt;FONT size=2&gt; usp_hexstrtovarbin &lt;BR&gt;GO&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;CREATE&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;PROC&lt;/FONT&gt;&lt;FONT size=2&gt; usp_hexstrtovarbin @hexstr &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;varchar&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;3990&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;),&lt;/FONT&gt;&lt;FONT size=2&gt; @bin &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;varbinary&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;4000&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;OUTPUT&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;AS&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;DECLARE&lt;/FONT&gt;&lt;FONT size=2&gt; @sql &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;nvarchar&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;4000&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;SET&lt;/FONT&gt;&lt;FONT size=2&gt; @sql &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'SET @bin='&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;+&lt;/FONT&gt;&lt;FONT size=2&gt; @hexstr&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;EXEC&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;sp_executesql&lt;/FONT&gt;&lt;FONT size=2&gt; @sql&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; N&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'@bin varbinary(4000) OUTPUT'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; @bin &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;OUTPUT&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;GO&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;FONT size=2&gt;
&lt;P&gt;Usage is straightforward: just call the proc, passing it the hex-formatted string and an output param to receive the converted value.&amp;nbsp; For example: &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;DECLARE&lt;/FONT&gt;&lt;FONT size=2&gt; @hexstr &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;varchar&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;max&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;),&lt;/FONT&gt;&lt;FONT size=2&gt; @bin &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;varbinary&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;4000&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;SET&lt;/FONT&gt;&lt;FONT size=2&gt; @hexstr &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'0x1234abcdef'&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;EM&gt;&lt;FONT color=#0000ff size=2&gt;EXEC&lt;/FONT&gt;&lt;FONT size=2&gt; usp_hexstrtovarbin @hexstr&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; @bin &lt;/FONT&gt;&lt;/EM&gt;&lt;FONT color=#0000ff size=2&gt;&lt;EM&gt;OUTPUT&lt;BR&gt;&lt;/EM&gt;&lt;BR&gt;SELECT&lt;/FONT&gt;&lt;FONT size=2&gt; @bin&lt;BR&gt;GO&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;/FONT&gt;Unfortunately, SQL won't allow you to use sp_executesql within a user-defined function, so&amp;nbsp;a disadvantage of&amp;nbsp;this approach is that you can't move this into a scalar function for use in queries like "SELECT dbo.fn_hexstrtovarbin(mycolumn) FROM mytable".&amp;nbsp; &lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4052636" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-04-05-26-36/fingerprints_5F00_screenshot_5F00_1.png" length="42904" type="image/x-png" /></item><item><title>Are you using SQL's Missing Index DMVs? </title><link>http://blogs.msdn.com/b/bartd/archive/2007/07/19/are-you-using-sql-s-missing-index-dmvs.aspx</link><pubDate>Thu, 19 Jul 2007 12:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3963080</guid><dc:creator>bartduncan</dc:creator><slash:comments>27</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=3963080</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=3963080</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2007/07/19/are-you-using-sql-s-missing-index-dmvs.aspx#comments</comments><description>&lt;p minmax_bound="true"&gt;Did you know that your SQL Server is keeping track of the indexes that it thinks you should create?&amp;nbsp; The "missing index" DMVs in SQL are a really great new feature in SQL Server 2005 that (in my opinion) seem to have been underutilized so far.&amp;nbsp; If you want to see if&amp;nbsp;this feature&amp;nbsp;can spare you the tedium of an afternoon identifying poor performing queries and tuning them, all you have to do is ask: &lt;/p&gt;&lt;font color=#0000ff size=1 minmax_bound="true"&gt;
&lt;blockquote minmax_bound="true"&gt;&lt;span style="FONT-SIZE: 7.5pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;avg_total_user_cost &lt;/font&gt;&lt;span style="COLOR: gray"&gt;*&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;avg_user_impact &lt;/font&gt;&lt;span style="COLOR: gray"&gt;/&lt;/span&gt;&lt;font color=#000000&gt; 100.0&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;*&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;user_seeks &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;user_scans&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;font color=#000000&gt; improvement_measure&lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: red"&gt;'CREATE INDEX [missing_index_'&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;CONVERT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; mig&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;index_group_handle&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;'_'&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;CONVERT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;index_handle&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;'_'&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;LEFT&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;PARSENAME&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: blue"&gt;statement&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; 1&lt;/font&gt;&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;&lt;font color=#000000&gt; 32&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;']'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;' ON '&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: blue"&gt;statement&lt;/span&gt;&lt;font color=#000000&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;' ('&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;ISNULL&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;equality_columns&lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;span style="COLOR: red"&gt;''&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;equality_columns &lt;/font&gt;&lt;span style="COLOR: gray"&gt;IS&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;NOT&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;inequality_columns &lt;/font&gt;&lt;span style="COLOR: gray"&gt;IS&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;NOT&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;','&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;''&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;font color=#000000&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;ISNULL&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;inequality_columns&lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;''&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;')'&lt;/span&gt;&lt;font color=#000000&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;ISNULL&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: red"&gt;' INCLUDE ('&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;included_columns &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;')'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;''&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;font color=#000000&gt; create_index_statement&lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.*,&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;database_id&lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;[object_id]&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;FROM&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_db_missing_index_groups&lt;/span&gt;&lt;font color=#000000&gt; mig&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;INNER&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;JOIN&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_db_missing_index_group_stats&lt;/span&gt;&lt;font color=#000000&gt; migs &lt;/font&gt;&lt;span style="COLOR: blue"&gt;ON&lt;/span&gt;&lt;font color=#000000&gt; migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;group_handle &lt;/font&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;font color=#000000&gt; mig&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;index_group_handle&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;INNER&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;JOIN&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: green"&gt;sys&lt;/span&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: green"&gt;dm_db_missing_index_details&lt;/span&gt;&lt;font color=#000000&gt; mid &lt;/font&gt;&lt;span style="COLOR: blue"&gt;ON&lt;/span&gt;&lt;font color=#000000&gt; mig&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;index_handle &lt;/font&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;font color=#000000&gt; mid&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;index_handle&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;WHERE&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;avg_total_user_cost &lt;/font&gt;&lt;span style="COLOR: gray"&gt;*&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;avg_user_impact &lt;/font&gt;&lt;span style="COLOR: gray"&gt;/&lt;/span&gt;&lt;font color=#000000&gt; 100.0&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;*&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;user_seeks &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;user_scans&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;&amp;gt;&lt;/span&gt;&lt;font color=#000000&gt; 10&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; LINE-HEIGHT: 115%; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;ORDER&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Arial','sans-serif'; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;BY&lt;/span&gt;&lt;font color=#000000&gt; migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;avg_total_user_cost &lt;/font&gt;&lt;span style="COLOR: gray"&gt;*&lt;/span&gt;&lt;font color=#000000&gt; migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;avg_user_impact &lt;/font&gt;&lt;span style="COLOR: gray"&gt;*&lt;/span&gt;&lt;span style="COLOR: blue"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;font color=#000000&gt;migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;user_seeks &lt;/font&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;font color=#000000&gt; migs&lt;/font&gt;&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;font color=#000000&gt;user_scans&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;DESC&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;/font&gt;
&lt;p minmax_bound="true" mce_keep="true"&gt;You'll want to run this after your server has been up and running a normal workload for a while.&amp;nbsp; If this returns no results, that's good news and indicates that&amp;nbsp;you're not missing any indexes that are obvious enough for the DMV to detect.&amp;nbsp; If it does return some suggestions, even better: you just improved your server's perf with almost no work. &lt;/p&gt;
&lt;p minmax_bound="true" mce_keep="true"&gt;While to me this&amp;nbsp;feature is so cool it almost seems magical, it does have a few limitations you should be aware of: &lt;/p&gt;
&lt;ul minmax_bound="true"&gt;
&lt;li minmax_bound="true"&gt;
&lt;div minmax_bound="true" mce_keep="true"&gt;It's not as smart as the Database Engine Tuning Advisor.&amp;nbsp; If you have identified a query that you know is expensive and needs&amp;nbsp;some help, don't pass up DTA just because the missing index DMVs didn't have any suggestions.&amp;nbsp; DTA might still be able to help.&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;&lt;li minmax_bound="true"&gt;
&lt;div minmax_bound="true" mce_keep="true"&gt;The missing index DMVs don't&amp;nbsp;take into account the overhead that new indexes can&amp;nbsp;create&amp;nbsp;(extra disk space, slight impact on insert/delete perf, etc). DTA does take this into account, however. &lt;/div&gt;
&lt;/li&gt;&lt;li minmax_bound="true"&gt;
&lt;div minmax_bound="true" mce_keep="true"&gt;The "improvement_measure" column in this query's output is a rough&amp;nbsp;indicator of the (estimated) improvement that might be seen if the index was created.&amp;nbsp; This is a unitless number, and has meaning only relative the same number for other indexes.&amp;nbsp; (It's a combination of the avg_total_user_cost, avg_user_impact, user_seeks, and user_scans columns in sys.dm_db_missing_index_group_stats.) &lt;/div&gt;
&lt;/li&gt;&lt;li minmax_bound="true"&gt;
&lt;div minmax_bound="true" mce_keep="true"&gt;The missing index DMVs don't make recommendation about whether a proposed index should be clustered or nonclustered.&amp;nbsp; This has workload-wide ramifications, while these DMVs focus only on the indexes that would benefit individual queries.&amp;nbsp; (DTA can do this, however.)&lt;/div&gt;
&lt;/li&gt;&lt;li minmax_bound="true"&gt;
&lt;div minmax_bound="true" mce_keep="true"&gt;Won't recommend partitioning. &lt;/div&gt;
&lt;/li&gt;&lt;li minmax_bound="true"&gt;
&lt;div minmax_bound="true" mce_keep="true"&gt;It's possible that&amp;nbsp;the DMVs&amp;nbsp;may not recommend the ideal column order for multi-column indexes. &lt;/div&gt;
&lt;/li&gt;&lt;li minmax_bound="true"&gt;
&lt;div minmax_bound="true" mce_keep="true"&gt;The DMV tracks information on no more than 500 missing indexes. &lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p minmax_bound="true" mce_keep="true"&gt;If you're a typical SQL user, you may not be&amp;nbsp;using these DMVs yet.&amp;nbsp; If you look around, though, there are a few places where they are in use.&amp;nbsp;One is&amp;nbsp;in the &lt;a class="" href="http://blogs.msdn.com/psssql/archive/2007/03/30/sql-server-2005-performance-dashboard-reports.aspx" minmax_bound="true" mce_href="http://blogs.msdn.com/psssql/archive/2007/03/30/sql-server-2005-performance-dashboard-reports.aspx"&gt;SP2 Performance Dashboard reports&lt;/a&gt;.&amp;nbsp; Another is the &lt;a class="" href="http://blogs.msdn.com/psssql/archive/2007/02/21/sql-server-2005-performance-statistics-script.aspx" minmax_bound="true" mce_href="http://blogs.msdn.com/psssql/archive/2007/02/21/sql-server-2005-performance-statistics-script.aspx"&gt;Perf Stats Script&lt;/a&gt; that SQL PSS uses.&amp;nbsp; And if you think the missing index DMVs are useful, check out &lt;a class="" href="http://blogs.msdn.com/queryoptteam/archive/2006/06/01/613516.aspx" minmax_bound="true" mce_href="http://blogs.msdn.com/queryoptteam/archive/2006/06/01/613516.aspx"&gt;this set of scripts&lt;/a&gt; that builds on the missing index DMVs&amp;nbsp;to simulate an "auto create index" feature.&amp;nbsp; Also, you should be aware there is similar missing index info output in the new XML showplan format in SQL 2005.&amp;nbsp; If you are already focused on a poorly-performing query, I would start with the plan view of missing indexes (followed by DTA) rather than the DMVs.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3963080" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>Deadlock Troubleshooting, Part 3</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/25/deadlock-troubleshooting-part-3.aspx</link><pubDate>Mon, 25 Sep 2006 21:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:770928</guid><dc:creator>bartduncan</dc:creator><slash:comments>41</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=770928</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=770928</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/09/25/deadlock-troubleshooting-part-3.aspx#comments</comments><description>&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Here’s an example of the classic scenario that is usually used to introduce the concept of a deadlock in a database: &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;table class=MsoNormalTable style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN: auto auto auto 30.6pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellSpacing=0 cellPadding=0 border=1 class="MsoNormalTable"&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 21.3pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=28&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-STYLE: italic; FONT-FAMILY: Arial; mso-bidi-font-style: normal"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/i&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #ddeeff; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 132.1pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt; mso-border-right-alt: solid windowtext .5pt" vAlign=top width=176 bgColor=#ddeeff&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-STYLE: italic; FONT-FAMILY: Arial; mso-bidi-font-style: normal"&gt;Process A&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/i&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #ddeeff; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 135pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt" vAlign=top width=180 bgColor=#ddeeff&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-STYLE: italic; FONT-FAMILY: Arial; mso-bidi-font-style: normal"&gt;Process B&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/i&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 27pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=36&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-STYLE: italic; FONT-FAMILY: Arial; mso-bidi-font-style: normal"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/i&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 1"&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 21.3pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=28&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 132.1pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt; mso-border-right-alt: solid windowtext .5pt" vAlign=top width=176 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;1. Begin Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 135pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt" vAlign=top width=180 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;1. Begin Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 27pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=36&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 2"&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 21.3pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=28&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 132.1pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt; mso-border-right-alt: solid windowtext .5pt" vAlign=top width=176 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;2. Update Part table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 135pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt" vAlign=top width=180 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;2. Update Supplier table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 27pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=36&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3"&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 21.3pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=28&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;font face=Wingdings color=red size=2&gt;&lt;span style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: Wingdings; mso-bidi-font-weight: normal; mso-bidi-font-family: Arial; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; text-shadow: auto; mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;&lt;span style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;à&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;font face=Arial color=red size=2&gt;&lt;span style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: Arial; mso-bidi-font-weight: normal; text-shadow: auto"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 132.1pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt; mso-border-right-alt: solid windowtext .5pt" vAlign=top width=176 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;3. Update &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-WEIGHT: bold; mso-bidi-font-weight: normal"&gt;Supplier&lt;/span&gt;&lt;/b&gt; table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 135pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt" vAlign=top width=180 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;3. Update &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-WEIGHT: bold; mso-bidi-font-weight: normal"&gt;Part&lt;/span&gt;&lt;/b&gt; table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 27pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=36&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;font face=Wingdings color=red size=2&gt;&lt;span style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: Wingdings; mso-bidi-font-weight: normal; mso-bidi-font-family: Arial; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; text-shadow: auto; mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;&lt;span style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;ß&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;font face=Arial color=red size=2&gt;&lt;span style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: Arial; mso-bidi-font-weight: normal; text-shadow: auto"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 4; mso-yfti-lastrow: yes"&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 21.3pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=28&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 132.1pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt; mso-border-right-alt: solid windowtext .5pt" vAlign=top width=176 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;4. Commit Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; BACKGROUND: #fffff5; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 135pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt" vAlign=top width=180 bgColor=#fffff5&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;4. Commit Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" style="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 27pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" vAlign=top width=36&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;If Process A and Process B each reached step #3 in their respective transactions at approximately the same time, it’s easy to see how they could end up blocking each other.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The most obvious solution to this deadlock is to change the order of the UPDATE statements in one of the transactions, so that lock resources are acquired in a consistent order.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Instead of this overly simplistic deadlock, let’s take a closer look at the deadlock scenario demonstrated in &lt;a href="http://blogs.msdn.com/bartd/archive/2006/09/13/751343.aspx" mce_href="http://blogs.msdn.com/bartd/archive/2006/09/13/751343.aspx"&gt;Deadlock Troubleshooting, Part 2&lt;/a&gt;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In that case, these two stored procedures ended up deadlocked: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;CREATE&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt;&lt;/font&gt; &lt;span style="FONT-WEIGHT: bold; mso-bidi-font-weight: normal"&gt;p1&lt;/span&gt; @p1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;/font&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;/font&gt; c2&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/font&gt; c3 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt;&lt;/font&gt; t1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;/font&gt; c2 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;BETWEEN&lt;/span&gt;&lt;/font&gt; @p1 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;/font&gt; @p1&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/font&gt;1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;CREATE&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt;&lt;/font&gt; &lt;span style="FONT-WEIGHT: bold; mso-bidi-font-weight: normal"&gt;p2&lt;/span&gt; @p1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;UPDATE&lt;/span&gt;&lt;/font&gt; t1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt;&lt;/font&gt; c2 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/font&gt; c2&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/font&gt;1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;/font&gt; c1 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/font&gt; @p1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;UPDATE&lt;/span&gt;&lt;/font&gt; t1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt;&lt;/font&gt; c2 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/font&gt; c2&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;-&lt;/span&gt;&lt;/font&gt;1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;/font&gt; c1 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/font&gt; @p1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;There’s no UPDATE, DELETE, or INSERT in the first proc; it consists of a single SELECT.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;And even the two UPDATE statements in the second proc aren’t wrapped in an outer BEGIN TRAN/COMMIT TRAN.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Both UPDATEs ran within their own autocommit transaction, which means that only one of them could have been involved in the deadlock.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Clearly this doesn’t fit the stereotypical “modify A then modify B / modify B then modify A” deadlock model described above.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This isn’t an edge case, by the way. We actually see this type of deadlock – where one or both of the participants are in the middle a single-query, autocommit transaction – more often than easy-to-understand deadlock scenarios involving two multi-statement transactions that just modify two tables in a different order.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;So, what would you do if DTA hadn’t automagically recommended a new index that prevented this deadlock?&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;To craft your own solution by hand, you need a deeper understanding of the deadlock than we have at the moment.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;font face=Arial size=3&gt;&lt;span style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; FONT-FAMILY: Arial; mso-bidi-font-weight: normal"&gt;What caused this deadlock? &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;We’ll need to refer back to the deadlock summary that was distilled from the -T1222 output (see &lt;a href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx" mce_href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx"&gt;Deadlock Troubleshooting, Part 1&lt;/a&gt; for a refresher on decoding -T1222): &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Arial"&gt;Spid X is running this query (line 2 of proc [p1], inputbuffer “… EXEC p1 4 …”): &lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Spid Y is running this query (line 2 of proc [p2], inputbuffer “EXEC p2 4”): &lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;UPDATE t1 SET c2 = c2+1 WHERE c1 = @p1&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;The SELECT is waiting for a Shared KEY lock on index t1.cidx.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The UPDATE holds a conflicting X lock. &lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;The UPDATE is waiting for an eXclusive KEY lock on index t1.idx1.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The SELECT holds a conflicting S lock.&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;First, let’s look at the query plan for the SELECT query.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;To view the plan, execute “SET STATISTICS &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:place w:st="on"&gt;&lt;st1:city w:st="on"&gt;PROFILE&lt;/st1:city&gt; &lt;st1:state w:st="on"&gt;ON&lt;/st1:state&gt;&lt;/st1:place&gt;”, then run “EXEC p1 4”.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Nested Loops(Inner Join, OUTER REFERENCES:([Uniq1002], [t1].[c1]))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Index Seek(OBJECT:([t1].[idx1]), SEEK:([t1].[c2] &amp;gt;= [@p1] AND [t1].[c2] &amp;lt;= [@p1]+(1)) ORDERED FORWARD)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Clustered Index Seek(OBJECT:([t1].[cidx]), SEEK:([t1].[c1]=[t1].[c1] AND [Uniq1002]=[Uniq1002]) LOOKUP ORDERED FORWARD)&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial color=#333399 size=2&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;A Nested Loop join executes its first child operator once, and executes the second child operator for each row returned by the first child (see &lt;a href="http://blogs.msdn.com/craigfr/archive/2006/07/26/679319.aspx" mce_href="http://blogs.msdn.com/craigfr/archive/2006/07/26/679319.aspx"&gt;this post&lt;/a&gt; for details). &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;In this case, the first child is a nonclustered Index Seek to find the rows “WHERE c2 BETWEEN @p1 AND @p1+1”. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;For each qualifying row in the nonclustered index, a second seek is done on the clustered index to look up the whole data row.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This clustered index seek is necessary because the nonclustered index does not cover the query. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;If you’re running SQL 2000, you’ll see a different-looking plan: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Bookmark Lookup(BOOKMARK:([Bmk1000]), OBJECT:([t1]))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Index Seek(OBJECT:([t1].[idx1]), SEEK:([t1].[c2] &amp;gt;= [@p1] AND [t1].[c2] &amp;lt;= [@p1]+1) ORDERED FORWARD)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;For practical purposes, these two plans are identical. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;The purpose of the Bookmark Lookup operator in the SQL 2000 plan is to visit the clustered index to retrieve the full set of columns for a row identified by a nonclustered index. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;In SQL 2005 this same operation is expressed as a loop join between the nonclustered index and the clustered index. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;For this deadlock, it’s simply important to note that both plans calls for a seek from the nonclustered index, &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-STYLE: italic; mso-bidi-font-style: normal"&gt;then&lt;/span&gt;&lt;/i&gt; a seek from the clustered index. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Now let’s look at the UPDATE: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;UPDATE t1 SET c2 = c2+1 WHERE c1 = @p1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Clustered Index Update(OBJECT:([t1].[cidx]), OBJECT:([t1].[idx1]), SET:([t1].[c2] = [Expr1004]))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Compute Scalar(DEFINE:([Expr1013]=[Expr1013]))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Compute Scalar(DEFINE:([Expr1004]=[t1].[c2]+(1), [Expr1013]=CASE WHEN CASE WHEN ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Top(ROWCOUNT est 0)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial color=#333399 size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|--Clustered Index Seek(OBJECT:([t1].[cidx]), SEEK:([t1].[c1]=[@p1]) ORDERED FORWARD)&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial color=#333399 size=2&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #333399; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The UPDATE has a fairly simple query plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The two most significant operators are the first and the last one.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The Clustered Index Seek locates the rows that quality for the “WHERE c1 = @p1” predicate.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Once a qualifying row has been found, the Clustered Index Update operator acquires an eXclusive key lock on the clustered index and modifies the row.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;We now have a full understanding of how the UPDATE blocks the SELECT: the UPDATE acquires an X lock on a clustered index key, and that lock blocks the SELECT’s bookmark lookup on the clustered index.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;But the other half of the deadlock – the reason that the SELECT blocks the UPDATE – isn’t quite so obvious.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The -T1222 told us “&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Arial"&gt;The UPDATE is waiting for an eXclusive KEY lock on index t1.idx1.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The SELECT holds a conflicting S lock.&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;”&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It’s not very apparent from the plan, but the UPDATE needs an X lock on the nonclustered index [idx1] because &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-STYLE: italic; mso-bidi-font-style: normal"&gt;the column it is updating ([c2]) is one of the non-clustered index’s key columns&lt;/span&gt;&lt;/i&gt;.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Any change to an index key column means that a row in the index must be relocated, and that relocation requires an X lock.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This is a key point to remember when trying to understand many deadlocks: the access path to find the qualifying rows is important, but index updates implied by the columns being modified can be just as important.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;To make things more confusing, sometimes you’ll see explicit “Index Update” or “Index Delete” operators in the plan for each nonclustered index that needs to be updated, while other times these don’t show up in the plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;(For more info on this check out &lt;a href="http://blogs.msdn.com/bartd/archive/2006/07/27/680518.aspx" mce_href="http://blogs.msdn.com/bartd/archive/2006/07/27/680518.aspx"&gt;Wide vs. Narrow Plans&lt;/a&gt;.)&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;To summarize: the SELECT used the nonclustered index to find a qualifying row.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;While holding a Shared lock on the nonclustered index, it needs to jump over to the clustered index and retrieve some columns that aren’t part of the nonclustered index.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;While it’s doing this, the UPDATE is busy doing a seek on the clustered index.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It finds a row, locks it and modifies it.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;But because one of the columns being modified is a key column in the nonclustered index, it then has to move to the nonclustered index and update that index, too.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This requires a second X key lock on the nonclustered index.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;So, the SELECT ends up blocked waiting for the UPDATE to release his X lock on the clustered index, while the UPDATE winds up blocked and waiting for the SELECT to release his S lock on the nonclustered index.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Hopefully it’s clear that even though each participant in this deadlock is just a single query, this is still a problem caused by out-of-order resource access patterns.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The SELECT statement locks a key in the nonclustered index, then locks a key in the clustered index.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The problem is that the UPDATE needs to lock the same two resources, but because of its query plan, it tries to lock them in the opposite order.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In a sense, it’s really the same problem as the simple deadlock scenario described at the beginning of this post.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The locks acquired by a query aren’t acquired all at once.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;A query plan is like a little program.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It wouldn’t be terribly inaccurate, for example, to think of a nested loop join as a FOR loop.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Each iteration of the loop acquires a key lock on the outer table, then holds that lock while looking up (and locking) matching rows in the inner table.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Deadlocks like this one are a little harder to figure out because the order of resource access within a single query depends on the query plan, and can’t be determined just by looking at the T-SQL.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;font face=Arial size=3&gt;&lt;span style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; FONT-FAMILY: Arial; mso-bidi-font-weight: normal"&gt;How did DTA’s new index avoid the deadlock?&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Here’s an index that will prevent this deadlock: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color=blue size=1&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;CREATE INDEX &lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;idx2&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt; ON&lt;/span&gt;&lt;/font&gt; t1 (c2, c3)&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This index “covers” the query “SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1”, which is just another way of saying that the index includes all of the columns referenced by the query.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;SQL will use this index instead of the [idx1] index because the plan based on the covering index is cheaper. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;The fact that the index covers the query means that the bookmark lookup against the clustered index is no longer necessary. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;And since the SELECT no longer needs to access the clustered index, it won’t get blocked by the UPDATE’s lock on the clustered index. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;font face=Arial size=3&gt;&lt;span style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; FONT-FAMILY: Arial; mso-bidi-font-weight: normal"&gt;What other solutions are available? &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;All deadlocks boil down to out-of-order resource access patterns.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In the simple deadlock scenario described at the beginning of this post, the solution is obvious: just reverse the two UPDATE statements in one of the transactions, and you won’t end up deadlocked.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;But in the more complex scenario that we just explored, it’s not so clear how to change the order in which locks are acquired.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Each deadlock participant is running a single-query, autocommit transaction, so you can’t just swap the order of two queries to acquire resources in a different order.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;SQL is a language designed to express high-level set operations; the specifics of how the database should go about retrieving and updating the specified set of data is generally left up to the SQL engine, with good reason.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;However, you do have some options for either influencing&amp;nbsp;which lock resources a query needs, or modifying the order in which it acquires the locks. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;Below are six different possible solutions to this deadlock.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Some of these are not ideal for this particular deadlock, but they are still worth exploring since the approach to deadlock avoidance that they illustrate may be the best possible solution for some other deadlock you encounter. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;ul style="MARGIN-TOP: 0in" type=disc&gt;
&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The new index is arguably the simplest and most elegant solution.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This deadlock occurs because two queries take different paths to the same resource.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The new index avoids the deadlock by eliminating any need for the SELECT to access the row in the clustered index. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;As a happy side effect, it also speeds up the SELECT query. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;CREATE INDEX &lt;/span&gt;&lt;/font&gt;idx2&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt; ON&lt;/span&gt;&lt;/font&gt; t1 (c2, c3) &lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;
&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;If you’re running SQL 2005, you could use the new SNAPSHOT or READ_COMMITTED_SNAPSHOT isolation levels. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;ALTER&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;DATABASE&lt;/span&gt;&lt;/font&gt; deadlocktest &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt;&lt;/font&gt; READ_COMMITTED_SNAPSHOT &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;ON&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;
&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Adding a NOLOCK hint to the SELECT will avoid the deadlock, but be cautious of this solution -- dirty reads can cause runtime errors and will expose you to uncommitted data.&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;ALTER&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt;&lt;/font&gt; p1 @p1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;/font&gt; &lt;br /&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;/font&gt; c2&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/font&gt; c3 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt;&lt;/font&gt; t1 &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;u&gt;&lt;font color=blue&gt;&lt;span style="FONT-WEIGHT: bold; COLOR: blue; mso-bidi-font-weight: normal"&gt;WITH&lt;/span&gt;&lt;/font&gt; (NOLOCK)&lt;/u&gt;&lt;/b&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;/font&gt; c2 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;BETWEEN&lt;/span&gt;&lt;/font&gt; @p1 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;/font&gt; @p1&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/font&gt;1&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;
&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;As mentioned above, this deadlock occurs because two queries take different paths to the same resource.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;By forcing one of the queries to use the same index as the other query, you can prevent the deadlock. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;However, SQL chose query plans that used two different indexes because those were the most efficient plans available for the two queries.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;By forcing a different index path, you are actually slowing down one of the queries.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This may be OK since it does avoid the deadlock, but you should test to make sure the cost is acceptable. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;ALTER&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt;&lt;/font&gt; p1 @p1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;/font&gt; &lt;br /&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;/font&gt; c2&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/font&gt; c3 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt;&lt;/font&gt; t1 &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;u&gt;&lt;font color=blue&gt;&lt;span style="FONT-WEIGHT: bold; COLOR: blue; mso-bidi-font-weight: normal"&gt;WITH&lt;/span&gt;&lt;/font&gt; (&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;INDEX&lt;/span&gt;&lt;/font&gt;=cidx)&lt;/u&gt;&lt;/b&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;/font&gt; c2 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;BETWEEN&lt;/span&gt;&lt;/font&gt; @p1 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;/font&gt; @p1&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/font&gt;1&lt;br /&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;If this query was coming from an application as an ad hoc query (not part of a stored proc), you could either modify the app to specify the index hint or use a plan guide with OPTION (USE PLAN...) if modifying the app wasn't possible.&amp;nbsp; Plan guides are available in SQL 2005 and later. &lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;
&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;One way to look at this deadlock is as a problem that arises because there’s an index on a frequently-updated column. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;Dropping the nonclustered index [idx1] will avoid the deadlock by (a) depriving the SELECT of its alternate access path to the row, and (b) preventing the UPDATE from having to update the nonclustered index row when it updates the [c2] column.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Like the prior solution, however, this will slow down the SELECT and any other queries that use this index.&amp;nbsp;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;br /&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;DROP INDEX &lt;/span&gt;&lt;/font&gt;t1.idx1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;
&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;You could force one of the transactions to block at an earlier point, before it has had an opportunity to acquire the lock that ends up blocking the other transaction. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;In the example below, the SELECT proc has been modified to run a new query that acquires and holds a lock on the clustered index &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-STYLE: italic; mso-bidi-font-style: normal"&gt;before&lt;/span&gt;&lt;/i&gt; it accesses the nonclustered index. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;In effect, this changes the order of resource access from (nonclustered, clustered) to (clustered, nonclustered). &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;Since that’s the same order that the UPDATE uses, the deadlock is no longer an issue. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size=1&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;ALTER&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;PROC&lt;/span&gt;&lt;/font&gt; p1 @p1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;AS&lt;/span&gt;&lt;/font&gt; &lt;br /&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;TRAN&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;DECLARE&lt;/span&gt;&lt;/font&gt; @x &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;int&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-WEIGHT: bold; mso-bidi-font-weight: normal"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;u&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;/font&gt; @x &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/font&gt; &lt;font color=fuchsia&gt;&lt;span style="COLOR: fuchsia"&gt;COUNT&lt;/span&gt;&lt;/font&gt;&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;(*)&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt;&lt;/font&gt; t1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WITH&lt;/span&gt;&lt;/font&gt; &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/font&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;HOLDLOCK&lt;/span&gt;&lt;/font&gt;&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/font&gt; UPDLOCK&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;/font&gt; c1 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/font&gt; @p1&lt;br /&gt;&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;/font&gt; c2&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/font&gt; c3 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt;&lt;/font&gt; t1 &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt;&lt;/font&gt; c2 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;BETWEEN&lt;/span&gt;&lt;/font&gt; @p1 &lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;AND&lt;/span&gt;&lt;/font&gt; @p1&lt;font color=gray&gt;&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/font&gt;1&lt;br /&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;COMMIT&lt;/span&gt;&lt;/font&gt; &lt;font color=blue&gt;&lt;span style="COLOR: blue"&gt;TRAN&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;font face=Arial size=2&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;If you can think of any other solutions, please share them in a comment. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;script src="http://www.google-analytics.com/urchin.js" type=text/javascript&gt;
&lt;/script&gt;

&lt;script type=text/javascript&gt;
_uacct = "UA-1763868-1";
urchinTracker();
&lt;/script&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=770928" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Locking/">SQL Locking</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Deadlocks/">SQL Deadlocks</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>Deadlock Troubleshooting, Part 2</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/13/deadlock-troubleshooting_2C00_-part-2.aspx</link><pubDate>Wed, 13 Sep 2006 03:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:751343</guid><dc:creator>bartduncan</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=751343</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=751343</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/09/13/deadlock-troubleshooting_2C00_-part-2.aspx#comments</comments><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;In this post I’ll look at an actual deadlock, then troubleshoot it using the &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;steps I described in &lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;A href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx" mce_href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx"&gt;Deadlock Troubleshooting, Part 1&lt;/A&gt; so you can see them in action.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;This is a simplified version of a deadlock scenario that an internal customer here at Microsoft called us for help with.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;To set up the scenario, run this: &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;-- Batch #1&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DATABASE&lt;/SPAN&gt; deadlocktest&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;USE&lt;/SPAN&gt; deadlocktest&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;SET&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;NOCOUNT&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;ON&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;DBCC&lt;/SPAN&gt; TRACEON &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;1222&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;-&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;IF&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;OBJECT_ID&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;'t1'&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;IS&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;NULL&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DROP&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;TABLE&lt;/SPAN&gt; t1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;IF&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;OBJECT_ID&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;'p1'&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;IS&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;NULL&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DROP&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;PROC&lt;/SPAN&gt; p1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;IF&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;OBJECT_ID&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;'p2'&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;IS&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;NULL&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DROP&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;PROC&lt;/SPAN&gt; p2&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;TABLE&lt;/SPAN&gt; t1 &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;c1 &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; c2 &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; c3 &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; c4 &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;5000&lt;SPAN style="COLOR: gray"&gt;))&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;DECLARE&lt;/SPAN&gt; @x &lt;SPAN style="COLOR: blue"&gt;int&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;SET&lt;/SPAN&gt; @x &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;WHILE&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@x &lt;SPAN style="COLOR: gray"&gt;&amp;lt;=&lt;/SPAN&gt; 1000&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;INSERT&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;INTO&lt;/SPAN&gt; t1 &lt;SPAN style="COLOR: blue"&gt;VALUES&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@x&lt;SPAN style="COLOR: gray"&gt;*&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; @x&lt;SPAN style="COLOR: gray"&gt;*&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; @x&lt;SPAN style="COLOR: gray"&gt;*&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; @x&lt;SPAN style="COLOR: gray"&gt;*&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;SET&lt;/SPAN&gt; @x &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; @x &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;CLUSTERED&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;INDEX&lt;/SPAN&gt; cidx &lt;SPAN style="COLOR: blue"&gt;ON&lt;/SPAN&gt; t1 &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;c1&lt;SPAN style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;NONCLUSTERED&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;INDEX&lt;/SPAN&gt; idx1 &lt;SPAN style="COLOR: blue"&gt;ON&lt;/SPAN&gt; t1 &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;c2&lt;SPAN style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;PROC&lt;/SPAN&gt; p1 @p1 &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;SELECT&lt;/SPAN&gt; c2&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; c3 &lt;SPAN style="COLOR: blue"&gt;FROM&lt;/SPAN&gt; t1 &lt;SPAN style="COLOR: blue"&gt;WHERE&lt;/SPAN&gt; c2 &lt;SPAN style="COLOR: gray"&gt;BETWEEN&lt;/SPAN&gt; @p1 &lt;SPAN style="COLOR: gray"&gt;AND&lt;/SPAN&gt; @p1&lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt;1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;PROC&lt;/SPAN&gt; p2 @p1 &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;UPDATE&lt;/SPAN&gt; t1 &lt;SPAN style="COLOR: blue"&gt;SET&lt;/SPAN&gt; c2 &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; c2&lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt;1 &lt;SPAN style="COLOR: blue"&gt;WHERE&lt;/SPAN&gt; c1 &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; @p1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;UPDATE&lt;/SPAN&gt; t1 &lt;SPAN style="COLOR: blue"&gt;SET&lt;/SPAN&gt; c2 &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; c2&lt;SPAN style="COLOR: gray"&gt;-&lt;/SPAN&gt;1 &lt;SPAN style="COLOR: blue"&gt;WHERE&lt;/SPAN&gt; c1 &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; @p1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;Now, run this from another connection: &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;-- Batch #2&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;USE&lt;/SPAN&gt; deadlocktest&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;SET&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;NOCOUNT&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;ON&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;WHILE&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;EXEC&lt;/SPAN&gt; p2 4&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;Finally, leave that one running while you run this from a third connection: &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;-- Batch #3&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;USE&lt;/SPAN&gt; deadlocktest&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;SET&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;NOCOUNT&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;ON&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;TABLE&lt;/SPAN&gt; #t1 &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;c2 &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; c3 &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;WHILE&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;INSERT&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;INTO&lt;/SPAN&gt; #t1 &lt;SPAN style="COLOR: blue"&gt;EXEC&lt;/SPAN&gt; p1 4&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;TRUNCATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;TABLE&lt;/SPAN&gt; #t1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;This will cause a deadlock; you should see one of the batches aborted by a 1205 error.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;Now that we have a reproducible deadlock, I’ll follow the troubleshooting steps that I posted in &lt;A href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx" mce_href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx"&gt;Deadlock Troubleshooting, Part 1&lt;/A&gt;. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL style="MARGIN-TOP: 0in" type=1&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;Turn on trace flag 1222&lt;/SPAN&gt;&lt;/I&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: Arial"&gt;The setup script already turned this on for you as a global flag (the “-1” in the dbcc traceon command is critical).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;Get the -T1222 output&lt;/SPAN&gt;&lt;/I&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Look at your errorlog now and you should see the trace flag 1222 output describing the deadlock. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT face=Arial size=2&gt;Decode the -T1222 output&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/I&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT face=Arial size=2&gt;.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Read through &lt;/FONT&gt;&lt;A href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx" mce_href="http://blogs.msdn.com/bartd/archive/2006/09/09/747119.aspx"&gt;&lt;FONT face=Arial size=2&gt;Deadlock Troubleshooting, Part 1&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt; again if you need more information about how to interpret -T1222 or -T1204 output. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Here’s what you should end up with after sifting through the -T1222 details and extracting the most important tidbits: &lt;BR&gt;&lt;BR&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="FONT-SIZE: 8pt; mso-bidi-font-family: Arial"&gt;Spid X is running this query (line 2 of proc [p1], inputbuffer “… EXEC p1 4 …”): &lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1&lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Spid Y is running this query (line 2 of proc [p2], inputbuffer “EXEC p2 4”): &lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;UPDATE t1 SET c2 = c2+1 WHERE c1 = @p1&lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Spid X is waiting for a Shared KEY lock on index t1.cidx.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Spid Y holds a conflicting X lock. &lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Spid Y is waiting for an eXclusive KEY lock on index t1.idx1.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Spid X holds a conflicting S lock. &lt;BR style="mso-special-character: line-break"&gt;&lt;BR style="mso-special-character: line-break"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;Run the queries through Database Tuning Advisor&lt;/SPAN&gt;&lt;/I&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;The -T1222 output tell us what inputbuffer we were running at the time of the deadlock (“&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;EXEC p1 4&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT face=Arial size=2&gt;” and “&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;EXEC p2 4&lt;/SPAN&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;”).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Tune each of these queries in DTA using the steps I discussed in Part 1. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;DTA will recommend a new index for Batch 3.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; Create the index by selecting "Apply Recommendations" from the Action drop-down menu. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;IMG src="http://blogs.msdn.com/bartd/attachment/751343.ashx" mce_src="http://blogs.msdn.com/bartd/attachment/751343.ashx"&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;At this point, if you re-run Batch 2 and Batch 3, you’ll find that the deadlock has been solved. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;You didn’t even have to use steps 5-8 or the list of other deadlock avoidance strategies that I listed in Part 1 of this series of posts.&amp;nbsp;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="mso-spacerun: yes"&gt;In a subsequent post I'll look at the details of the query plans involved in this particular deadlock to understand what caused the deadlock and why DTA's proposed index fixed it.&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: Arial; mso-bidi-font-size: 10.0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-spacerun: yes" minmax_bound="true"&gt;(This post series is continued in &lt;A class="" href="http://blogs.msdn.com/bartd/archive/2006/09/25/770928.aspx" mce_href="http://blogs.msdn.com/bartd/archive/2006/09/25/770928.aspx"&gt;Deadlock Troubleshooting, Part 3&lt;/A&gt;.)&lt;/SPAN&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=751343" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-00-75-13-43/DTA.PNG" length="15245" type="image/x-png" /><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Locking/">SQL Locking</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Deadlocks/">SQL Deadlocks</category></item><item><title>Deadlock Troubleshooting, Part 1</title><link>http://blogs.msdn.com/b/bartd/archive/2006/09/08/deadlock-troubleshooting_2C00_-part-1.aspx</link><pubDate>Sat, 09 Sep 2006 00:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:747119</guid><dc:creator>bartduncan</dc:creator><slash:comments>74</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=747119</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=747119</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/09/08/deadlock-troubleshooting_2C00_-part-1.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;A deadlock is a circular blocking chain, where two or more threads are each blocked by the other so that no one can proceed.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;When the deadlock monitor thread in SQL Server detects a circular blocking chain, it selects one of the participants as a victim, cancels that spid&amp;rsquo;s current batch, and rolls backs his transaction in order to let the other spids continue with their work.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;The deadlock victim will get a 1205 error: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Verdana; color: red;"&gt;Transaction (Process ID 52) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;A deadlock is a special type of blocking scenario, but blocking and deadlocking are not the same thing.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Sometimes we have people report that they are experiencing "deadlocking" when they are really only seeing blocking. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;With very few exceptions, deadlocks are a natural side effect of blocking, not a SQL Server bug.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;The typical deadlock solution is either a stored proc/app code tweak, or a schema/indexing change.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;Here&amp;rsquo;s how to troubleshoot deadlocks.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;These steps apply to most deadlocks, and they&amp;rsquo;ll allow you to resolve many of them without even having to dig into query plans or other nitty gritty details.&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;What&amp;rsquo;s that?&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;You &lt;i style="mso-bidi-font-style: normal;"&gt;like&lt;/i&gt; digging into query plans, and have nitty grits for breakfast every morning? &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;OK then, we&amp;rsquo;ll look at a deadlock scenario from the inside out a bit later. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;But first, here are the basics: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;ol type="1" style="margin-top: 0in;"&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;Turn on trace flag 1222&lt;/b&gt; with &amp;ldquo;DBCC TRACEON (1222, -1)&amp;rdquo; or by adding &amp;ldquo;-T1222&amp;rdquo; as a SQL startup parameter.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; This trace flag is a new trace flag in SQL 2005, a much improved version of the tried-and-true -T1204.&amp;nbsp; If you&amp;rsquo;re running SQL 2005, you should be using 1222 instead of 1204 unless you have deep-seated masochistic tendencies. Alternatives to 1222: &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;ul&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;If you are using SQL 2000 or SQL 7.0, you&amp;rsquo;ll have no choice but to fall back on the older -T1204.&amp;nbsp;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;There&amp;rsquo;s a &amp;ldquo;Deadlock graph&amp;rdquo; Profiler trace event that provides the same info as -T1222. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Feel free to use this instead of -T1222 if you&amp;rsquo;re on SQL 2005. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;But don&amp;rsquo;t waste your time with the &amp;ldquo;Lock:Deadlock&amp;rdquo; and &amp;ldquo;Lock:Deadlock Chain&amp;rdquo; trace events that are in SQL 2000, as they provide an unacceptably incomplete picture of the deadlock.&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;br /&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;Get the -T1222 output &lt;/b&gt;from the SQL errorlog after the deadlock has occurred. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;You&amp;rsquo;ll see output that looks like this: &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;deadlock-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;deadlock victim=processdceda8&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;process-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;process id=processdceda8&lt;/span&gt; taskpriority=0 logused=0 waitresource=KEY: 2:72057594051493888 (0400a4427a09) waittime=5000 ownerId=24008914 transactionname=SELECT lasttranstarted=2006-09-08T15:54:22.327 XDES=0x8fd9a848 lockMode=S schedulerid=1 kpid=4404 status=suspended &lt;span style="background: yellow; mso-highlight: yellow;"&gt;spid=54&lt;/span&gt; sbid=0 ecid=0 priority=0 transcount=0 lastbatchstarted=2006-09-08T15:54:22.293 lastbatchcompleted=2006-09-08T15:54:22.293 clientapp=OSQL-32 hostname=BARTD2 hostpid=3408 loginname=bartd isolationlevel=read committed (2) xactid=24008914 currentdb=2 lockTimeout=4294967295 clientoption1=538968096 clientoption2=128056&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;executionStack&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;frame procname=&lt;span style="background: yellow; mso-highlight: yellow;"&gt;tempdb.dbo.p1&lt;/span&gt; line=2 stmtstart=60 sqlhandle=0x03000200268be70bd&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;SELECT c2, c3 FROM t1 WHERE c2 = @p1&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;frame procname=adhoc line=2 stmtstart=32 stmtend=52 sqlhandle=0x020000008a4df52d3&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;EXEC p1 3&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;inputbuf&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;EXEC p1 3&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;process id=process3c54c58&lt;/span&gt; taskpriority=0 logused=16952 waitresource=KEY: 2:72057594051559424 (0900fefcd2fe) waittime=5000 ownerId=24008903 transactionname=UPDATE lasttranstarted=2006-09-08T15:54:22.327 XDES=0x802ecdd0 lockMode=X schedulerid=2 kpid=4420 status=suspended &lt;span style="background: yellow; mso-highlight: yellow;"&gt;spid=55&lt;/span&gt; sbid=0 ecid=0 priority=0 transcount=2 lastbatchstarted=2006-09-08T15:54:22.327 lastbatchcompleted=2006-09-08T15:54:22.310 clientapp=OSQL-32 hostname=BARTD2 hostpid=2728 loginname=bartd isolationlevel=read committed (2) xactid=24008903 currentdb=2 lockTimeout=4294967295 clientoption1=538968096 clientoption2=128056&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;executionStack&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;frame procname=&lt;span style="background: yellow; mso-highlight: yellow;"&gt;tempdb.dbo.p2&lt;/span&gt; line=2 stmtstart=58 sqlhandle=0x030002005fafdb0c&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;UPDATE t1 SET c1 = FLOOR (c1), c2 = FLOOR (c2) WHERE c1 = @p1&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;frame procname=adhoc line=2 stmtstart=32 stmtend=52 sqlhandle=0x020000006f878816&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;EXEC p2 3&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;inputbuf&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;EXEC p2 3&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;resource-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;keylock&lt;/span&gt; hobtid=72057594051559424 dbid=2 &lt;span style="background: yellow; mso-highlight: yellow;"&gt;objectname=tempdb.dbo.t1 indexname=idx1&lt;/span&gt; id=lock83642a00 mode=S associatedObjectId=72057594051559424&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner id=&lt;span style="background: yellow; mso-highlight: yellow;"&gt;processdceda8 mode=S&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter id=&lt;span style="background: yellow; mso-highlight: yellow;"&gt;process3c54c58 mode=X&lt;/span&gt; requestType=wait&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;keylock&lt;/span&gt; hobtid=72057594051493888 dbid=2 &lt;span style="background: yellow; mso-highlight: yellow;"&gt;objectname=tempdb.dbo.t1 indexname=cidx&lt;/span&gt; id=lock83643780 mode=X associatedObjectId=72057594051493888&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;owner id=&lt;span style="background: yellow; mso-highlight: yellow;"&gt;process3c54c58 mode=X&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter-list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: 8pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;waiter id=&lt;span style="background: yellow; mso-highlight: yellow;"&gt;processdceda8 mode=S&lt;/span&gt; requestType=wait&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial; font-size: x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;ol start="3" type="1" style="margin-top: 0in;"&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;&amp;ldquo;Decode&amp;rdquo; the -T1222 output&lt;/b&gt; to better understand the deadlock scenario.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;The deadlock is summarized by a &amp;ldquo;process-list&amp;rdquo; and a &amp;ldquo;resource-list&amp;rdquo;.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;A &amp;ldquo;process&amp;rdquo; is a spid or worker thread that participates in the deadlock.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Each process is assigned an identifier, like &amp;ldquo;processdceda8&amp;rdquo;.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;A resource is a resource that one of the participants owns (usually a lock) that the other participant is waiting on.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: small;"&gt;I like to use a format like the one below to summarize the deadlock. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;You can skip this step if you want, but I never do; I find it really helps me understand the deadlock situation more clearly.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;I&amp;rsquo;ve highlighted in yellow each of the data points within the 1222 output that you would need to reconstruct this summary on your own. &lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: 8pt;"&gt;Spid 54 is running this query (line 2 of proc [p1]): &lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT c2, c3 FROM t1 WHERE c2 = @p1&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Spid 55 is running this query (line 2 of proc [p2]): &lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;UPDATE t1 SET c1 = FLOOR (c1), c2 = FLOOR (c2) WHERE c1 = @p1&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Spid 54 is waiting for a Shared KEY lock on index t1.cidx.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;/span&gt;&lt;/span&gt;Spid 55 holds a conflicting X lock.)&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Spid 55 is waiting for an eXclusive KEY lock on index t1.idx1.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Spid 54 holds a conflicting S lock.)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For most lock types (including KEY locks, as shown in this example), SQL will directly identify the index by name in the output.&amp;nbsp; For some lock types, though, you'll get an "associatedObjectId", but no object name.&amp;nbsp; An example:&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 115%; font-family: 'Arial','sans-serif'; font-size: 8pt; mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;pagelock fileid=1 pageid=95516 &lt;span style="background: aqua; mso-highlight: aqua;"&gt;dbid=9&lt;/span&gt; objectname="" id=lock177a9e280 mode=IX &lt;span style="background: aqua; mso-highlight: aqua;"&gt;associatedObjectId=72057596554838016&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;br /&gt;The attribute "associatedObjectId" isn't the type of Object ID that you're probably familiar with; it's actually a partition ID.&amp;nbsp; You can determine the database name by running "SELECT DB_NAME(9)", where&amp;nbsp;the "9" in this example comes from the "dbid" attribute, highlighted in blue.&amp;nbsp; Then you can determine the index and table name by looking up the associatedObjectId/PartitionId in the indicated database:&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT OBJECT_NAME(i.object_id), i.name&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM sys.partitions AS p&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INNER JOIN sys.indexes AS i ON i.object_id = p.object_id AND i.index_id = p.index_id &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE p.partition_id = &lt;span style="line-height: 115%; font-family: 'Arial','sans-serif'; background: aqua; font-size: 10pt; mso-highlight: aqua;"&gt;72057596554838016&lt;/span&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;For those of you on SQL 2005 who think that the -T1222 output is a bit overwhelming, you're right.&amp;nbsp; But you may also want to count your blessings and be thankful that you don&amp;rsquo;t have to wade through -T1204 output, which is a lot more difficult to interpret than -T1222 and doesn&amp;rsquo;t provide nearly as much useful information about the deadlock.&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Check out the file "&lt;span id="_ctl0__ctl0_TaskRegion_Editor1_serverfilename"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-00-74-71-19/SQL2000_5F00_Deadlocks_5F00_T1204.htm" title="Decoding trace flag 1204 output"&gt;Decoding_T1204_Output.htm&lt;/a&gt;" attached to this post &lt;/span&gt;for annotated -T1204 output. &lt;br /&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;Run the queries involved in the deadlock through Database Tuning Advisor.&lt;/b&gt; &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Plop the query in a Management Studio query window, change db context to the&amp;nbsp;correct database, right-click the query text and select &amp;ldquo;Analyze Query in DTA&amp;rdquo;. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Don&amp;rsquo;t skip this step; more than half of the deadlock issues we see are resolved simply by adding an appropriate index so that one of the queries runs more quickly and with a smaller lock footprint.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If DTA recommends indexes (it'll say &amp;ldquo;Estimated Improvement: &amp;lt;some non-zero&amp;gt;%&amp;rdquo;), create them and monitor to see if the deadlock persists.&amp;nbsp; You can s&lt;span style="font-family: Arial; font-size: 10pt; mso-fareast-font-family: PMingLiU; mso-ansi-language: EN-US; mso-fareast-language: ZH-TW; mso-bidi-language: AR-SA;"&gt;elect &amp;ldquo;Apply Recommendations&amp;rdquo; from the Action drop-down menu to create the index immediately, or save the CREATE INDEX commands as a script to create them during a maintenance window.&amp;nbsp; &lt;em&gt;Be sure to t&lt;/em&gt;&lt;/span&gt;&lt;em&gt;une each of the queries separately.&lt;/em&gt;&amp;nbsp; &lt;br /&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;Make sure the query is using the minimum necessary transaction isolation level &lt;/b&gt;(-T1222 will tell you this &amp;ndash; search the output for &amp;ldquo;isolationlevel&amp;rdquo;).&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Queries run by transactional COM+ components will default to serializable, which is usually overkill.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;This can be reduced by query hints (&amp;ldquo;...FROM tbl1 WITH (READCOMMITTED)...&amp;rdquo;), a SET TRANSACTION ISOLATION LEVEL command, or, in Windows 2003 and later, by configuring the object in the Component Services MMC plugin. &lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;Make sure that your transactions are as brief as they can be&lt;/b&gt; while still meeting the relevant business constraints. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Try not to use implicit transactions, as this model of transaction management encourages unnecessarily long transactions.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;br /&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;Look for other opportunities to improve the efficiency of the queries &lt;/b&gt;involved in the deadlock, either through query changes or through indexing improvements.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;A query that locks the minimum number of resources will be much less likely to deadlock with another query.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Table scans, index scans, and large hashes or large sorts in the query plan may indicate opportunities for improvement. &lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;If one or both spids is running a multi-statement transaction, you may need to capture a profiler trace &lt;/b&gt;that spans the deadlock in order to identify the full set of queries that were involved in the deadlock. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Unfortunately, both -T1204 and -T1222 only print out the two queries that &amp;ldquo;closed the loop&amp;rdquo;, and it&amp;rsquo;s possible that one of the blocking locks was acquired by an earlier query run within the same transaction. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;These are all general recommendations that you can apply to any deadlock without having to really roll up your sleeves and get dirty. &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;If after doing all of this you haven&amp;rsquo;t resolved it, though, you&amp;rsquo;ll have to dive a bit deeper and tailor a solution to the specifics of the scenario.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Here&amp;rsquo;s a menu of some common techniques that you can choose from when deciding how best to tackle a deadlock: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;ul type="disc" style="margin-top: 0in;"&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;Access objects in the same order.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Consider the following two batches: &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;table cellpadding="0" cellspacing="0" border="1" class="MsoTableGrid" style="margin: auto auto auto 53.6pt; border-collapse: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext;"&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes;"&gt;
&lt;td width="192" valign="top" style="padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2in; padding-right: 5.4pt; padding-top: 0in; mso-border-alt: solid windowtext .5pt; border: windowtext 1pt solid;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;1. Begin Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="216" valign="top" style="border-bottom: windowtext 1pt solid; border-left: #d4d0c8; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2.25in; padding-right: 5.4pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;1. Begin Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 1;"&gt;
&lt;td width="192" valign="top" style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2in; padding-right: 5.4pt; border-top: #d4d0c8; border-right: windowtext 1pt solid; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;2. Update Part table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="216" valign="top" style="border-bottom: windowtext 1pt solid; border-left: #d4d0c8; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2.25in; padding-right: 5.4pt; border-top: #d4d0c8; border-right: windowtext 1pt solid; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;2. Update Supplier table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 2;"&gt;
&lt;td width="192" valign="top" style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2in; padding-right: 5.4pt; border-top: #d4d0c8; border-right: windowtext 1pt solid; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;3. Update &lt;b style="mso-bidi-font-weight: normal;"&gt;Supplier&lt;/b&gt; table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="216" valign="top" style="border-bottom: windowtext 1pt solid; border-left: #d4d0c8; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2.25in; padding-right: 5.4pt; border-top: #d4d0c8; border-right: windowtext 1pt solid; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;3. Update &lt;b style="mso-bidi-font-weight: normal;"&gt;Part&lt;/b&gt; table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3; mso-yfti-lastrow: yes;"&gt;
&lt;td width="192" valign="top" style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2in; padding-right: 5.4pt; border-top: #d4d0c8; border-right: windowtext 1pt solid; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;4. Commit Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="216" valign="top" style="border-bottom: windowtext 1pt solid; border-left: #d4d0c8; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 2.25in; padding-right: 5.4pt; border-top: #d4d0c8; border-right: windowtext 1pt solid; padding-top: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt;"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;4. Commit Transaction&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;These two batches may deadlock frequently.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If both are about to execute step 3, they may each end up blocked by the other because they both need access to a resource that the other connection locked in step 2.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul type="disc" style="margin-top: 0in;"&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;If both deadlock participants are using the same index, consider adding an index that can provide an alternate access path to one of the spids.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;For example, adding a covering nonclustered index for a SELECT involved in a deadlock may prevent the problem (assuming that none of the covering index keys are modified by the other deadlock participant). &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;On the other hand, if the spids are deadlocking because they took alternate paths (indexes) to a common required data row or page, consider whether one of the indexes can be removed or an index hint used to force both queries to share an access path.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Be cautious of potential performance hits as a result of this approach. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;Deadlocks are a special type of blocking where two spids both end up blocking the other.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Sometimes the best way to prevent a deadlock is to force the blocking to occur at an earlier point in one of the two transactions.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;For example, if you force spid A to be blocked by spid B at the very beginning of A&amp;rsquo;s transaction, it may not have a chance to acquire the lock resource that later ends up blocking spid B.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Doesn&amp;rsquo;t this means you are deliberately causing blocking?&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Yes, but remember that you already have blocking or you wouldn&amp;rsquo;t be in a deadlock situation, and simple blocking is a big improvement over a deadlock.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;As soon as B commits his transaction, A will be able to proceed.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;HOLDLOCK and UPDLOCK hints can be useful for this. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;If a high priority process is being selected as a victim in a deadlock with a lower priority process, the lower priority process could be modified to SET DEADLOCK_PRIORITY LOW.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Spids that set this will offer themselves up as the sacrificial lamb in any deadlock they encounter.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;Avoid placing clustered indexes on columns that are frequently updated. Updates to clustered index key columns will require locks on the clustered index (to move the row) and all nonclustered indexes (since the leaf level of NC indexes reference rows by clustered index key value).&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;In some cases it may be appropriate to add a NOLOCK hint, assuming that one of the queries is a SELECT statement.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;While this is a tempting path because it is a quick and easy solution for many deadlocks, approach it with caution as it carries with it all the usual caveats surrounding read uncommitted isolation level (a query could return a transactionally inconsistent view of the data).&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;If you are unfamiliar with the risks, read the "SET TRANSACTION ISOLATION LEVEL" topic in SQL Books Online.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;In SQL 2005 you could consider the new SNAPSHOT isolation level.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;This will avoid most blocking while avoiding the risks of NOLOCK.&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;An even cooler new feature IMHO is the new READ COMMITTED SNAPSHOT database option (see ALTER DATABASE), which allows you to use a variant of snapshot isolation level without changing your app.&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt; &lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;If one or both locks involved in the deadlock are S/X TAB (table) locks, lock escalation may be involved.&amp;nbsp; You can reduce the likelihood of lock escalation by enabling trace flag 1224 (SQL 2005 and later) or 1211 (see KB &lt;a href="http://support.microsoft.com/kb/323630"&gt;323630&lt;/a&gt;).&amp;nbsp; Note that this does not apply to "intent" TAB locks, which have a capital "I" prefix (e.g. IS&amp;nbsp;/ IX TAB locks). &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="margin: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;If the deadlock is intermittent, sometimes the simplest solution is to add deadlock retry logic.&amp;nbsp;The retry logic could be in T-SQL, as long as (a) you're on SQL 2005 or later so that you can use BEGIN TRY, and (b) your transaction is wholly-contained within a single stored proc or batch. See &lt;a href="http://msdn.microsoft.com/en-us/library/aa175791(v=sql.80).aspx" title="SQL Essentials: Using TRY/CATCH to Resolve Deadlocks in SQL Server 2005"&gt;this article&lt;/a&gt; for details. If the deadlock transaction spans multiple batches you can still add deadlock retry logic, but it would need to be moved out to the client app code. If you can only add deadlock retry logic to one of the participants in the deadlock, you can use SET DEADLOCK_PRIORITY LOW to ensure that the engine prefentially aborts the transaction of the guy that has the retry logic. &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;In a follow-up post I&amp;rsquo;ll look at a fairly typical deadlock in detail.&amp;nbsp; This will provide an example of what you'd have to do if the 8 high-level steps listed above fail you, forcing you to &lt;/span&gt;&lt;span style="font-family: Arial;"&gt;understand the scenario at a deeper level so that you can craft a custom solution.&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;(This post series is continued in &lt;a href="http://blogs.msdn.com/bartd/archive/2006/09/13/751343.aspx"&gt;Deadlock Troubleshooting, Part 2&lt;/a&gt;.)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;&lt;/script&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=747119" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-00-74-71-19/SQL2000_5F00_Deadlocks_5F00_T1204.htm" length="27971" type="application/octet-stream" /><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Locking/">SQL Locking</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Deadlocks/">SQL Deadlocks</category></item><item><title>Joins Dissected on CraigFr's blog</title><link>http://blogs.msdn.com/b/bartd/archive/2006/08/16/joins-dissected-on-craigfr-s-blog.aspx</link><pubDate>Wed, 16 Aug 2006 22:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:702906</guid><dc:creator>bartduncan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=702906</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=702906</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/08/16/joins-dissected-on-craigfr-s-blog.aspx#comments</comments><description>&lt;div&gt;&lt;font face=Arial size=2&gt;CraigFr has a great series of posts in his blog describing the difference between the various logical and physical join types, the details of how SQL Server implements these joins, and the things that the query optimizer takes into account when selecting a join type.&amp;nbsp; These&amp;nbsp;five posts are&amp;nbsp;a wonderful read.&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face=Arial size=2&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href="http://blogs.msdn.com/craigfr/archive/2006/07/19/671712.aspx"&gt;&lt;font face=Arial size=2&gt;Overview of Logical Join Types (Inner/Outer/Cross/Semi/Anti-Semi/Cross Apply)&lt;/font&gt;&lt;/a&gt;&lt;font face=Arial size=2&gt; &lt;/font&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://blogs.msdn.com/craigfr/archive/2006/07/26/679319.aspx"&gt;&lt;font face=Arial size=2&gt;Nested Loop Joins Dissected&lt;/font&gt;&lt;/a&gt;&lt;font face=Arial size=2&gt; &lt;/font&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://blogs.msdn.com/craigfr/archive/2006/08/03/687584.aspx"&gt;&lt;font face=Arial size=2&gt;Merge Joins Dissected&lt;/font&gt;&lt;/a&gt;&lt;font face=Arial size=2&gt; &lt;/font&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://blogs.msdn.com/craigfr/archive/2006/08/10/687630.aspx"&gt;&lt;font face=Arial size=2&gt;Hash Joins Dissected&lt;/font&gt;&lt;/a&gt;&lt;font face=Arial size=2&gt; &lt;/font&gt;
&lt;/li&gt;&lt;li&gt;&lt;a href="http://blogs.msdn.com/craigfr/archive/2006/08/16/702828.aspx"&gt;&lt;font face=Arial size=2&gt;How SQL chooses which physical join operator to use&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face=Arial size=2&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font face=Arial size=2&gt;
&lt;div&gt;&lt;font face=Arial size=2&gt;"Joins" Post Category: &lt;a href="http://blogs.msdn.com/craigfr/archive/category/14122.aspx"&gt;http://blogs.msdn.com/craigfr/archive/category/14122.aspx&lt;/a&gt;&lt;/font&gt;&lt;/div&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=702906" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>SELECT from a view slower than "equivalent" SELECT from the base table</title><link>http://blogs.msdn.com/b/bartd/archive/2006/08/14/select-from-a-view-slower-than-equivalent-select-from-the-base-table.aspx</link><pubDate>Tue, 15 Aug 2006 01:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:700271</guid><dc:creator>bartduncan</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=700271</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=700271</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/08/14/select-from-a-view-slower-than-equivalent-select-from-the-base-table.aspx#comments</comments><description>&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Sometime we get complaints that a query is slower than it could be because a filter isn’t pushed very deeply down into a plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, consider this hypothetical poor performance scenario (my apologies in advance for the lack of normalization): &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;USE tempdb&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;IF OBJECT_ID ('Sales') IS NOT NULL DROP TABLE Sales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;IF OBJECT_ID ('SalesSummary') IS NOT NULL DROP VIEW SalesSummary&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;CREATE TABLE Sales (&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;SalesPerson varchar(30) NOT NULL, &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;SalesAmount money NOT NULL, Comments char(200))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;INSERT INTO Sales VALUES ('Green ', 19011.87, '')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;INSERT INTO Sales VALUES ('Green', 2478.42, '')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;INSERT INTO Sales VALUES ('Green ', 1975.11, '')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;INSERT INTO Sales VALUES ('White', 3007.01, '')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;INSERT INTO Sales VALUES ('White', 5312.44, '')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;INSERT INTO Sales VALUES ('Brown', 843.20, '')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;CREATE INDEX idx ON Sales (SalesPerson, SalesAmount)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;CREATE VIEW &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:place w:st="on"&gt;&lt;st1:city w:st="on"&gt;SalesSummary&lt;/st1:city&gt; &lt;st1:state w:st="on"&gt;AS&lt;/st1:state&gt;&lt;/st1:place&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SELECT SalesPerson, SUM (SalesAmount) AS TotalSales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;FROM Sales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GROUP BY SalesPerson&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SET STATISTICS PROFILE ON&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GO&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;-- This query uses an index seek to retrieve only the rows &lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;/span&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;-- where SalesPerson = 'Green'&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SELECT * &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;FROM SalesSummary &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;WHERE SalesPerson = 'Green'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;-- This query uses an index scan, then filters the rows later. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SELECT * &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;FROM SalesSummary &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;WHERE SalesPerson LIKE 'Green'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GO&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SET STATISTICS PROFILE OFF&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;GO&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The first query filters on “SalesPerson&lt;font style="BACKGROUND-COLOR: #ffff00"&gt; = &lt;/font&gt;'Green'”. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;The second filters on “SalesPerson&lt;font style="BACKGROUND-COLOR: #ffff00"&gt; LIKE &lt;/font&gt;'Green'”.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Here’s the first query’s plan: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font size=1&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;|--Stream Aggregate(DEFINE:([Expr1004]=SUM([Sales].[SalesAmount]), [Sales].[SalesPerson]=ANY([Sales].[SalesPerson])))&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font size=1&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([Sales].[idx]), SEEK:([Sales].[SalesPerson]='Green') ORDERED FORWARD)&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;You can see that the first thing the plan does is a very efficient index seek to narrow the set of rows down to those that pass the “SalesPerson = ‘Green’” filter.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Then a Stream Aggregate operator computes the SUM(SalesAmount) expression for each SalesPerson returned by the index seek.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The second plan, though, scans every row in the table and computes the SUM for every SalesPerson.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Only after it has scanned and aggregated every row does it filter out those values that don’t survive the LIKE predicate.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font size=1&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;|--Filter(WHERE:([Sales].[SalesPerson] like 'Green'))&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font size=1&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Stream Aggregate(GROUP BY:([Sales].[SalesPerson]) DEFINE:([Expr1004]=SUM([Sales].[SalesAmount])))&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font size=1&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Scan(OBJECT:([Sales].[idx]), ORDERED FORWARD)&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Here’s a general rule off thumb you can follow when looking for tuning opportunities in a query plan: for best performance, you usually want to push the most selective predicates as deeply as possible into the plan.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;If you do the most selective operation first, the remaining operators have fewer rows to process, and that means faster overall query execution.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Based on this rule of thumb, the first plan here is clearly preferable from a performance perspective.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;So what gives?&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Why does the use of “LIKE” instead of “=” make SQL refuse to push the filter down?&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Even more baffling, why do you get the more efficient plan when you bypass the view and select directly from the table, even if you use "LIKE"? &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;Run this and you’ll see what I mean: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SELECT SalesPerson, SUM (SalesAmount) AS TotalSales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;FROM Sales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;WHERE SalesPerson LIKE 'Green'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GROUP BY SalesPerson&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This uses an efficient index seek-based plan, just like the first query.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;There are several things going on here: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-TOP: 0in" type=disc&gt;
&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Views must behave like a table&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt; 
&lt;/li&gt;&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The “LIKE” and “=” operators use subtly different rules for string matching&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt; 
&lt;/li&gt;&lt;li class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;GROUP BY uses the same string comparison rules as “=” for the purposes of determining which rows end up in the same bucket&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;What I mean by “views must behave like a table” is that the output of a select from a view must be the same as what you could get by materializing the view (e.g. selecting it into a temp table), then querying the materialized view.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In this case, the GROUP BY in the view could return a different total sales amount for a SalesPerson if SQL chose a plan that pushed a LIKE predicate below the Stream Aggregate. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;Here’s proof: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SELECT * &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;FROM SalesSummary &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;WHERE SalesPerson LIKE 'Green '&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;-- Query 1 output: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SalesPerson&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;TotalSales&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;------------------------------ --------------------- &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;Green&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;23465.4000&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SELECT SalesPerson, SUM (SalesAmount) AS TotalSales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;FROM Sales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;WHERE SalesPerson LIKE 'Green '&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;GROUP BY SalesPerson&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;-- Query 2 output: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SalesPerson&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;TotalSales&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;------------------------------ --------------------- &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;Green&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;20986.9800&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The first query selects from the view, while the second moves the view logic into the query and selects directly from the base table.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Other than that, they are identical, yet the SUM(SalesAmount) calculation is different.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Recall that I mentioned that “=” and “LIKE” have different string comparison semantics.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;In particular, LIKE considers trailing blanks in the right-hand operand to be significant, while the “=” operator ignores trailing blanks.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For the set of three rows with group ID “Green”, only two will qualify for the filter “WHERE SalesPerson LIKE ‘Green ‘” because they have trailing blanks.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The third “Green” row doesn’t have any trailing blanks and won’t survive the LIKE filter.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;When you push this LIKE filter below the aggregate, you end up SUMming a different set of rows.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;That’s not allowed if the GROUP BY is part of a view; if a WHERE clause applied to a view can change a property of a row instead of just filtering it out, it would mean that the view didn’t behave like a materialized table.&amp;nbsp;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;Put another way, a filter on a view that includes a GROUP BY is only allowed to eliminate entire groups; it's not legal for it to eliminate some base rows in a group but retain others, changing the group's membership.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;It’s therefore by design that the slower scan-based plan is selected for the select from the view with LIKE.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; In contrast, the query from the view with the "SalesPerson='Green'" filter can be pushed because GROUP BY uses the same string comparison rules as the "=" operator.&amp;nbsp; It's safe for the QO to assume that pushing the "=" filter below the Stream Aggregate will not change the view's semantics.&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This isn’t just about trailing blanks – you can see&amp;nbsp;the attached script for a couple of examples that demonstrate the exact same thing (pushing LIKE below an aggregate changes the output of the aggregate) for a couple of interesting non-blank characters.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;And it isn’t only about “LIKE” vs. “=”, either; this is just the example that was close at hand when I wrote this&amp;nbsp;(we have a case open for this scenario right now).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Finally, be aware that derived tables (and CTEs)&amp;nbsp;also provide the same guarantee.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example, note that this query selects from the base table but also does a full scan followed by filter, just like the above select from the view: &lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;SELECT * &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;FROM (&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT SalesPerson, SUM (SalesAmount) AS TotalSales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM Sales&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GROUP BY SalesPerson) AS t&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;WHERE SalesPerson LIKE 'Green'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;So, to net out all of this: Generally you want your filters to be pushed deep into the query plan --&amp;nbsp;as deeply as possible.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;But when you’re selecting from a view, there will be some limits to what can be pushed.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Some filters can’t be pushed beneath parts of the view without changing the view’s semantics, and that would break a contract that SQL is required to maintain.&amp;nbsp;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;strong&gt;&lt;u&gt;UPDATE&lt;/u&gt;&lt;/strong&gt; (2 March 2009): Fabiano Amorim pointed out that the Query #1 and Query #2 use the same plan on SQL 2008.&amp;nbsp; He's right; a new performance optimization causes the LIKE predicate to be pushed below the GROUP BY's aggregate operator.&amp;nbsp; I think this is actually a bug -- it does result in a faster plan, but it breaks the "views behave like tables" rule that SQL follows in all other cases.&amp;nbsp; The general rule stands: not all predicates can be pushed below a view's GROUP BY, even in SQL 2008 with this fairly aggressive performance optimization.&amp;nbsp; (And don't be surprised if this optimization gets removed in a future release. ;)&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=700271" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-00-70-02-71/like_5F00_views_5F00_nopush.sql" length="2056" type="application/octet-stream" /><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>Wide vs. Narrow Plans</title><link>http://blogs.msdn.com/b/bartd/archive/2006/07/27/wide-vs-narrow-plans.aspx</link><pubDate>Thu, 27 Jul 2006 17:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:680518</guid><dc:creator>bartduncan</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=680518</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=680518</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/07/27/wide-vs-narrow-plans.aspx#comments</comments><description>&lt;div&gt;&lt;font size=2 face=Arial&gt;Here's another&amp;nbsp;case where you might see intermittently poor performance that is "by design".&amp;nbsp; &lt;/font&gt;&lt;font size=2 face=Arial&gt;Suppose you see that a delete, insert, or update query in a stored proc usually runs quickly, but occasionally the query takes much longer to complete.&amp;nbsp; You captured a detailed profiler trace of both the good and bad periods, including the Showplan Statistics event.&amp;nbsp; The offending delete is in a stored proc, and it couldn't be simpler: "DELETE FROM t1 WHERE c1 = @p1". &amp;nbsp;&amp;nbsp;One of the plans you see for this statement looks like this:&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;font color=#000000 size=1 face="Courier New"&gt;DELETE FROM t1 WHERE c1 = @p1&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Clustered Index Delete(OBJECT:([mydb].[dbo].[t1].[idx1]), WHERE:([t1].[c1]=[@p1]))&amp;nbsp; &lt;br /&gt;&lt;/font&gt;&lt;br /&gt;And the other plan looks something like this:&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;font size=1 face="Courier New"&gt;DELETE FROM t1 WHERE c1 = @p1&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Sequence&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Index Delete(OBJECT:([mydb].[dbo].[t1].[idx2]))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Sort(ORDER BY:([t1].[c2] ASC, [t1].[c1] ASC, [Bmk1000] DESC))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Table Spool&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Clustered Index Delete(OBJECT:([mydb].[dbo].[t1].[idx1]), WHERE:([t1].[c1]=[@p1]))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Index Delete(OBJECT:([mydb].[dbo].[t1].[idx3]))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Sort(ORDER BY:([t1].[c3] ASC, [t1].[c1] ASC, [Bmk1000] DESC))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Table Spool&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Index Delete(OBJECT:([mydb].[dbo].[t1].[idx4]))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Sort(ORDER BY:([t1].[c2] ASC, [t1].[c3] ASC, [t1].[c1] ASC, [Bmk1000] DESC))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |--Table Spool&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;(The number of branches may vary -- there will be one for each nonclustered index.)&lt;br /&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;These are two very different-looking plans for the same, very simple (DELETE FROM t1 WHERE c1 = @p1) query.&amp;nbsp; What's going on here?&amp;nbsp; &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;When a SQL query plan needs to delete a clustered index row, it can let the storage engine take care of cleaning up (deleting) the corresponding rows in all the nonclustered indexes. (Similarly, in the case of an insert or update, the storage engine is capable of automatically inserting or updating rows in the affected nonclustered indexes.)&amp;nbsp; &lt;/font&gt;&lt;font size=2 face=Arial&gt;This is a simple approach, but the problem with this is that the nonclustered index rows are deleted in &lt;em&gt;clustered&lt;/em&gt; &lt;em&gt;index key order&lt;/em&gt;, not nonclustered key order, which means that SQL has to jump all around the nonclustered index in a more or less random fashion deleting one row at a time here, a second one way over there, etc.&amp;nbsp; If there are a large number of rows being deleted, the disk seeks to reposition the head back and forth across the nonclustered indexes can be very expensive.&amp;nbsp; In these cases (when SQL knows that it has to modify a lot of rows from a clustered index), the&amp;nbsp;query optimizer&amp;nbsp;has the option of choosing to include the logic needed to delete all the corresponding nonclustered index rows in the query plan as explicit Index Delete operators (see the second plan above for an example).&amp;nbsp; In this case the QO can do fancier things than the storage engine knows how to do, like sorting the data set in nonclustered index key order before the Index Delete; this means that all the nonclustered index keys can be removed in a single, ordered pass over the nonclustered index, which means less drive head movement and therefore a smaller amount of disk wait time.&amp;nbsp; This is a neat trick, but the overhead of setting up and performing all the sorts and having the NC index deletes happen at a higher level in the engine has a certain amount of overhead, so it only makes sense for the QO to handle index maintenance itself if a large number of rows will be deleted.&amp;nbsp; (There are other factors that also come into play like the number of nonclustered indexes on the table, but for a fixed schema and query, the key variable is the number of rows that SQL estimates will be affected by the query.)&amp;nbsp;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;In this particular case, we have a parameterized DELETE query.&amp;nbsp; The number of rows that will be deleted depends entirely on the specific parameter value that is passed into the query.&amp;nbsp; Whether the QO will choose to compile a plan that does the index maintenance in the plan (this is called the "&lt;em&gt;wide plan&lt;/em&gt;") or leaves this up to the storage engine (a "&lt;em&gt;narrow plan&lt;/em&gt;") depends on the number of rows that will be deleted.&amp;nbsp; If a parameter value that only qualifies a small number of rows is passed in when the plan for the proc isn't in cache, the QP will select a narrow plan.&amp;nbsp; This is most likely the most efficient plan for that particular parameter value.&amp;nbsp; Unfortunately it may not be an efficient plan for other parameter values that may delete thousands of rows, but because the query is in a stored proc, the plan will be reused for subsequent executions until something kicks the plan out of cache (that something could be a reindex, a schema change to a table, a DBCC FREEPROCCACHE, or just normal cache maintenance to free up memory for new plans).&amp;nbsp;&amp;nbsp;Plan dependency on the parameters that are passed in for the execution that triggers the compile is called &lt;em&gt;parameter sniffing&lt;/em&gt;.&amp;nbsp; (Param sniffing can lead to a host of common perf issues -- I may&amp;nbsp;talk more about some of these in future entries.)&amp;nbsp; &lt;br /&gt;&lt;br /&gt;So, the basic problem here is the result of &lt;/font&gt;&lt;a class=ms-missinglink href="http://mswikis/SQLTroubleshooting/_layouts/CreateWebPage.aspx?List={39eca866%2Da681%2D4e7d%2D86fa%2D1be4c7891915}&amp;amp;RootFolderUrl=%2FSQLTroubleshooting%2FPages&amp;amp;Name=SQLPerfParameterSniffing" mce_href="http://mswikis/SQLTroubleshooting/_layouts/CreateWebPage.aspx?List={39eca866%2Da681%2D4e7d%2D86fa%2D1be4c7891915}&amp;amp;RootFolderUrl=%2FSQLTroubleshooting%2FPages&amp;amp;Name=SQLPerfParameterSniffing"&gt;&lt;a title=#h2 name=#h2&gt;&lt;/a&gt;&lt;font size=2 face=Arial&gt;parameter sniffing&lt;/font&gt;&lt;/a&gt;&lt;font size=2 face=Arial&gt; combined with the choice of wide vs. narrow plans.&amp;nbsp; A few possible solutions (definitely not an exhaustive list): &lt;/font&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;font size=2 face=Arial&gt;Use local variables instead of parameters in the INSERT/DELETE/UPDATE query so that the plan selected doesn't depend on param value. &lt;/font&gt;
&lt;/li&gt;&lt;li&gt;&lt;font size=2 face=Arial&gt;Define the proc WITH RECOMPILE (or use an OPTION(RECOMPILE) query hint if you're on SQL 2005) so that everyone gets a plan tailored to their particular parameter. &lt;/font&gt;&lt;/li&gt;
&lt;li&gt;Use an "OPTIMIZE FOR (@p1=&amp;lt;a "typical" param value&amp;gt;)" query hint to tell the optimizer to always optimize for a parameter value that is typical of the type that will most commonly be passed in&lt;/li&gt;
&lt;li&gt;Use a USE PLAN hint, possibly with a plan guide, to force one of the two plan possibilities&lt;/li&gt;&lt;/ul&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;Below is a simple script that shows the two types of index maintenance plans; you can play&amp;nbsp;around with this to see them in action.&amp;nbsp;&amp;nbsp; After looking at the plans, try omitting the DBCC FREEPROCCACHE to see how perf is affected when a wide plan is reused to modify a small number of rows, or when a narrow plan is reused to modify a large number of rows.&amp;nbsp; &lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New"&gt;USE tempdb&lt;br /&gt;GO&lt;br /&gt;IF @@TRANCOUNT &amp;gt; 0 ROLLBACK TRAN&lt;br /&gt;IF OBJECT_ID ('t1') IS NOT NULL DROP TABLE t1&lt;br /&gt;IF OBJECT_ID ('myproc') IS NOT NULL DROP PROC myproc&lt;br /&gt;GO&lt;br /&gt;CREATE TABLE t1 (c1 int, c2 int, c3 int)&lt;br /&gt;GO&lt;br /&gt;SET NOCOUNT ON&lt;br /&gt;GO&lt;br /&gt;DECLARE @x int&lt;br /&gt;SET @x = 1&lt;br /&gt;BEGIN TRAN&lt;br /&gt;WHILE @x &amp;lt;= 100000&lt;br /&gt;BEGIN&lt;br /&gt;&amp;nbsp; IF @x % 250 &amp;lt; 100&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; INSERT INTO t1 (c1, c2, c3) &lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VALUES (0, @x % 400, @x % 1000)&lt;br /&gt;&amp;nbsp; ELSE&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; INSERT INTO t1 (c1, c2, c3) &lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VALUES (@x % 1000, @x % 400, @x % 1000)&lt;br /&gt;&amp;nbsp; IF @x % 5000 = 0 &lt;br /&gt;&amp;nbsp; BEGIN &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAISERROR ('Inserted %d rows...', 0, 1, @x) WITH NOWAIT&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; COMMIT TRAN &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN TRAN &lt;br /&gt;&amp;nbsp; END&lt;br /&gt;&amp;nbsp; SET @x = @x + 1&lt;br /&gt;END&lt;br /&gt;WHILE @@TRANCOUNT &amp;gt; 0 COMMIT TRAN&lt;br /&gt;GO&lt;br /&gt;CREATE CLUSTERED INDEX idx1 ON t1 (c1)&lt;br /&gt;CREATE INDEX idx2 ON t1 (c2)&lt;br /&gt;CREATE INDEX idx3 ON t1 (c3)&lt;br /&gt;CREATE INDEX idx4 ON t1 (c2, c3)&lt;br /&gt;GO&lt;br /&gt;CREATE PROC myproc @p1 int AS &lt;br /&gt;UPDATE t1 SET c1 = c1 WHERE c1 = @p1&lt;br /&gt;GO&lt;br /&gt;DBCC FREEPROCCACHE&lt;br /&gt;GO&lt;br /&gt;-- First delete a small number of rows: let storage &lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;font face="Courier New"&gt;-- engine take care of index maintenance&lt;br /&gt;SET STATISTICS PROFILE ON&lt;br /&gt;EXEC myproc @p1 = 126&lt;br /&gt;SET STATISTICS PROFILE OFF&lt;br /&gt;GO&lt;br /&gt;-- Be sure to flush the proc cache or we will reuse the plan &lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;font face="Courier New"&gt;-- that was compiled for a much smaller number of rows on the &lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;font face="Courier New"&gt;-- last execution. &lt;br /&gt;DBCC FREEPROCCACHE&lt;br /&gt;GO&lt;br /&gt;-- Delete a larger number of rows: handle index maintenance &lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face=Arial&gt;&lt;font size=2&gt;&lt;font face="Courier New"&gt;-- explicitly in query plan&lt;br /&gt;SET STATISTICS PROFILE ON&lt;br /&gt;EXEC myproc @p1 = 0&lt;br /&gt;SET STATISTICS PROFILE OFF&lt;br /&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size=2 face=Arial&gt;If you look closely at the wide plan you might notice something curious: only one of the Table Spool operators in the plan&amp;nbsp;has a child.&amp;nbsp; The purpose of a table spool is to store a set of rows in an intermediate place so that they can be used more than once in the plan without having to execute the same query subtree over and over.&amp;nbsp; Spools are typically used either for a perf boost or for Halloween protection; in this example, the spool fulfills both of these needs.&amp;nbsp; Table spools &lt;em&gt;require &lt;/em&gt;a child -- remember that the purpose of a spool is to cache rows, so they must have an input that provides the rows to cache.&amp;nbsp; Any time you see a spool without a visible child, it's a spool that was created elsewhere in the plan, and&amp;nbsp;is being reused.&amp;nbsp; In this plan, only the first appearance of the first spool shows the spool's&amp;nbsp;child -- the content of the spool is then reused by the subsequent index delete operators.&amp;nbsp; (Unfortunately, if there are multiple spools in a plan, there's no obvious indicator about which one is referenced by each of the "childless" spools.) &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;If you examine the wide update plan from the script above, you might notice a Split operator.&amp;nbsp; This guy is poorly documented, but pretty cool --&amp;nbsp;he provides both Halloween protection and a performance boost (the benefits may sound similar to those of the Table Spool, but the mechanism is completely different).&amp;nbsp; If I have time, in a future post I may explain how Split works to provide these benefits. &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font size=2 face=Arial&gt;A final footnote: In the comments above I&amp;nbsp;mention that the&amp;nbsp;storage engine is responsible for index maintenance when executing a narrow plan.&amp;nbsp; Technically, this is only true in SQL 2000.&amp;nbsp; In SQL 2005, both wide and narrow plans are performed by layers above the storage engine.&amp;nbsp; The different plan types still exist in SQL 2005, however, and the concepts and terms "wide plan" and "narrow plan" still apply to SQL 2005.&amp;nbsp; 
&lt;div&gt;&lt;font size=2&gt;&lt;font face=Arial&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=680518" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/Query+Tuning/">Query Tuning</category></item><item><title>Limited Statistics Granularity</title><link>http://blogs.msdn.com/b/bartd/archive/2006/07/25/limited-statistics-granularity.aspx</link><pubDate>Tue, 25 Jul 2006 19:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:678019</guid><dc:creator>bartduncan</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=678019</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=678019</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/07/25/limited-statistics-granularity.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;To set up this scenario, run the script below: &lt;/span&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;span style="font-size: xx-small;"&gt;USE tempdb&lt;br /&gt;GO&lt;br /&gt;IF OBJECT_ID ('test1') IS NOT NULL DROP TABLE test1&lt;br /&gt;GO&lt;br /&gt;CREATE TABLE test1 (c1 tinyint, c2 smallint)&lt;br /&gt;DECLARE @x int&lt;br /&gt;DECLARE @msg varchar(1000)&lt;br /&gt;SET @x = 1&lt;br /&gt;SET NOCOUNT ON&lt;br /&gt;BEGIN TRAN&lt;br /&gt;WHILE (@x &amp;lt;= 1000000)&lt;br /&gt;BEGIN&lt;br /&gt;&amp;nbsp; INSERT INTO test1 (c1, c2) VALUES (@x % 255, CASE WHEN @x % 1000 = 500 THEN 1000 ELSE @x % 1000 END)&lt;br /&gt;&amp;nbsp; IF @x % 5000 = 0 &lt;br /&gt;&amp;nbsp; BEGIN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; COMMIT TRAN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN TRAN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET @msg = 'Inserted ' + CONVERT(varchar(20), @x)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;span style="font-size: xx-small;"&gt;+ ' rows ...'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAISERROR (@msg, 0, 1) WITH NOWAIT&lt;br /&gt;&amp;nbsp; END&lt;br /&gt;&amp;nbsp; SET @x = @x + 1&lt;br /&gt;END&lt;br /&gt;WHILE (@@TRANCOUNT &amp;gt; 0) COMMIT TRAN&lt;br /&gt;SET NOCOUNT OFF&lt;br /&gt;GO&lt;br /&gt;CREATE INDEX idx1 ON test1 (c2)&lt;br /&gt;UPDATE STATISTICS test1 WITH FULLSCAN&lt;br /&gt;DBCC SHRINKDATABASE (db2)&lt;br /&gt;-- DBCC SHOW_STATISTICS ('test1', 'idx1')&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;span style="font-size: xx-small;"&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The hypothetical scenario here is that your users complain that the first of the two queries below&amp;nbsp;runs very slowly. You find that if you force SQL to use index [idx1] with an index hint,&amp;nbsp;the query executes much more quickly.&amp;nbsp; But why should you change your app to compensate for what seems to be a SQL Server bug?&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;:)&lt;/span&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;span style="font-family: Courier New;"&gt;USE db2&lt;br /&gt;GO&lt;br /&gt;SET STATISTICS PROFILE ON&lt;br /&gt;SET STATISTICS TIME ON&lt;br /&gt;-- Query #1 (slow)&lt;br /&gt;SELECT c1, c2 FROM test1 WHERE c2 = 500&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;
&lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;span style="font-family: Courier New;"&gt;-- Query #2 (fast)&lt;br /&gt;SELECT c1, c2 FROM test1 WITH (INDEX = idx1) WHERE c2 = 500&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;span style="font-family: Courier New;"&gt;SET STATISTICS TIME OFF&lt;br /&gt;SET STATISTICS PROFILE OFF&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Courier New;"&gt;GO&lt;/span&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/blockquote&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;To me, this suggests two ques&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;tions: 1) Why isn't the fast plan chosen by default? 2) Are there any possible solutions that don't require modifying code to supply an index hint? &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Despite appearances, this&amp;nbsp;actually isn't a bug.&amp;nbsp; Here's the explanation: &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This table demonstrates uneven data distribution.&amp;nbsp; The [c2] column holds values ranging from 0 to 999.&amp;nbsp; There are exactly 1000 rows with the same [c2] value for each of the integers between 0 and 999. However, the value 500 is an anomaly; there are 0 rows with this value.&amp;nbsp; Run the following query to show the number of rows with each [c2] value and you'll see that there are none with [c2]=500: &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SELECT c2, COUNT(*) AS cnt FROM test1 GROUP BY c2&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;c2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;------ ----------- &lt;br /&gt;&amp;nbsp;...&lt;br /&gt;&amp;nbsp;498&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;499&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;501&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;502&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;...&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;When the server builds a &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql2k/html/statquery.asp"&gt;histogram&lt;/a&gt; for the statistics on a column, it attempts to intelligently select range endpoints so that a given step of the histogram represents a range of values with similar density.&amp;nbsp; If you run DBCC SHOW_STATISTICS on the index you can see the histogram:&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" dir="ltr"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;-- DBCC SHOW_STATISTICS ('test1', 'idx1')&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="ltr"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;RANGE_HI_KEY RANGE_ROWS EQ_ROWS DISTINCT_RANGE_ROWS AVG_RANGE_ROWS&lt;br /&gt;&amp;nbsp;------------ ---------- ------- ------------------- --------&lt;br /&gt;&amp;nbsp;0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.0&lt;br /&gt;&amp;nbsp;499&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 498000.0&amp;nbsp;&amp;nbsp; 1000.0&amp;nbsp; 498&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&lt;br /&gt;&amp;nbsp;503&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2000.0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&lt;br /&gt;&amp;nbsp;1000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 496000.0&amp;nbsp;&amp;nbsp; 1000.0&amp;nbsp; 496&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;RANGE_HI_KEY is the value that sets the upper bound for the range of values represented in each histogram step.&amp;nbsp; The lower bound is RANGE_HI_KEY+1 for the preceding step.&amp;nbsp; For example, consider this row: &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;br /&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&amp;nbsp;499&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 498000.0&amp;nbsp;&amp;nbsp; 1000.0&amp;nbsp; 498&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Arial;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This indicates that there are 498000 rows (RANGE_ROWS) with a value between 1 and 498, inclusive.&amp;nbsp; There are 1000 rows with the exact value of 499 (EQ_ROWS).&amp;nbsp; AVG_RANGE_ROWS tells us that the typical value that falls within the range 1-498 shows up in 1000 rows.&amp;nbsp; Now consider the next step in the histogram: &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" dir="ltr"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;RANGE_HI_KEY RANGE_ROWS EQ_ROWS DISTINCT_RANGE_ROWS AVG_RANGE_ROWS&lt;br /&gt;&amp;nbsp;------------ ---------- ------- ------------------- --------&lt;br /&gt;&amp;nbsp;503&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2000.0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000.0&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This indicates that there are 2000 rows (RANGE_ROWS) with a value between 500 and 502, inclusive; and there are 1000 rows (EQ_ROWS) with the exact value of 503.&amp;nbsp;The typical c2 value in the range 500-502 shows up average of 1000 times in the table.&amp;nbsp; Recall what we know about the actual number of rows in this range: &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;c2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;------ ----------- &lt;br /&gt;&amp;nbsp;...&lt;br /&gt;&amp;nbsp;499&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;501&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;502&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;503&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;br /&gt;&amp;nbsp;...&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This histogram step describes its range of c2 values exactly.&amp;nbsp; What the histogram doesn't tell us, however, is exactly how many times a particular value in the 500-502 range appears.&amp;nbsp; Remember that each histogram step only summarizes the values within a given range; it doesn't tell you exactly how many of each particular value there are (that is, unless the table's domain&amp;nbsp;is small enough for every distinct value to have its own step in the histogram).&amp;nbsp; SQL knows it needs to search for rows where [c2]=500, so it locates this step in the statistics' histogram, finds that the typical value in this range shows up in 1000 rows, and uses this as its rowcount estimate. &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial; font-size: x-small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;This is a key point: if a value being searched for falls in the middle of a histogram step, the AVG_RANGE_ROWS for that step is used to estimate the number of matches that will be found.&amp;nbsp; SQL is always optimistic and assumes that the value it is searching for is actually one of the range rows.&amp;nbsp; In this case that assumption is incorrect.&amp;nbsp; The QP ends up overestimating the number of rows that will be returned and choosing a table scan when an index seek would have been more efficient. &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;span style="font-family: Arial; font-size: x-small;"&gt;&lt;/span&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;So if this isn't a bug, what could you do to fix the problem?&amp;nbsp; One solution would be to make the nonclustered index a covering index.&amp;nbsp; Alternately, you could make [idx1] a clustered index.&amp;nbsp; And, if you're using SQL 2005, you could use a plan guide with a USE PLAN hint to force the fast plan without changing the query text.&amp;nbsp; All of these solutions should cause SQL to select&amp;nbsp;the fast index seek plan.&amp;nbsp; The bad cardinality estimate will still be there, but the perf problem won't. &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;(Update: A blog post from Jack Li describing a similar problem can be found &lt;a href="http://blogs.msdn.com/b/psssql/archive/2010/07/09/sampling-can-produce-less-accurate-statistics-if-the-data-is-not-evenly-distributed.aspx" title="psssql blog"&gt;here&lt;/a&gt;.)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=678019" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category></item><item><title>Why a bad plan isn't necessarily a bug</title><link>http://blogs.msdn.com/b/bartd/archive/2006/07/25/why-a-bad-plan-isn-t-necessarily-a-bug.aspx</link><pubDate>Tue, 25 Jul 2006 18:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:678015</guid><dc:creator>bartduncan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=678015</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=678015</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/07/25/why-a-bad-plan-isn-t-necessarily-a-bug.aspx#comments</comments><description>&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;Everyone that has worked with databases for long enough has run into situations where the query optimizer doesn't select the best possible plan.&amp;nbsp; You may find that you can force SQL to use an index, choose a different join algorithm or join order, or&amp;nbsp;use some other query hint&amp;nbsp;to&amp;nbsp;get a much faster plan.&amp;nbsp; But you're reluctant to try to push through a change in your app in order to compensate for what seems to be a clear SQL Server bug. &lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;We see lots of instances where people have the expectation that if SQL chose a demonstrably&amp;nbsp;inefficient plan, it must be&amp;nbsp;a bug.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face=Arial size=2&gt;Unfortunately, it's not quite that simple.&amp;nbsp; At the end of the day, the&amp;nbsp;query processor is just a guessing engine that tries to pick the best possible plan based off statistics that summarize the data in a table.&amp;nbsp; There are some significant limits to how accurately the&amp;nbsp;query optimizer&amp;nbsp;can model the real world when it is costing plans.&amp;nbsp;&amp;nbsp;For starters, the information available to it in statistics only summarizes&amp;nbsp;column data&amp;nbsp;at a fairly high level; it doesn't tell the QO all that there is to know about the distribution of values in a column, or the relationships between data in two or more columns.&amp;nbsp;&amp;nbsp;And there's a tradeoff between the complexity of the QO's modeling&amp;nbsp;rules and the amount of time that it takes to cost the hundreds of thousands of plan possibilities that exist for even modestly complex queries.&amp;nbsp; Even if the QO had full knowledge of everything there was to know about your data, trying to take all of those facts into account when&amp;nbsp;compiling plans would lead to more situations where&amp;nbsp;the optimizer&amp;nbsp;spent more time trying to find that perfect plan than it would have taken to actually execute the query.&amp;nbsp;&amp;nbsp;There are definitely QO bugs out there, but&amp;nbsp;the cases where the QO picks a less-than-ideal plan are not necessarily bugs; some are simply the result of these limits.&amp;nbsp; In fact, our experience is that a majority of bad plan issues fall into the "unfortunate, but by design" bucket.&amp;nbsp; &lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;Dubious?&amp;nbsp; That's OK -- I'll try to illustrate some of the more common "by design" situations where you might see poor plan selections here.&amp;nbsp; Even if you end up taking the perfectly defensible position that the QO should be able to handle some of these situations more gracefully, it'll still benefit you to be able to recognize them.&amp;nbsp; &lt;/FONT&gt;&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=678015" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-00-67-80-15/pbm_5F00_health_5F00_create.png" length="89309" type="image/x-png" /><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Server/">SQL Server</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Optimizer/">SQL Optimizer</category><category domain="http://blogs.msdn.com/b/bartd/archive/tags/SQL+Performance/">SQL Performance</category></item><item><title>Yet Another Blog</title><link>http://blogs.msdn.com/b/bartd/archive/2006/06/09/yet-another-blog.aspx</link><pubDate>Sat, 10 Jun 2006 00:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:630132</guid><dc:creator>bartduncan</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/rsscomments.aspx?WeblogPostID=630132</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/bartd/commentapi.aspx?WeblogPostID=630132</wfw:comment><comments>http://blogs.msdn.com/b/bartd/archive/2006/06/09/yet-another-blog.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;I have resisted creating a blog for two reasons: 1) there are&amp;nbsp;never enough hours in the day, and 2) the areas that I know well enough to say something interesting about&amp;nbsp;-- SQL Server performance and some other engine issues,&amp;nbsp;mainly -- are esoteric, not exactly People magazine-type fare that would entertain the masses.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Recently, though, I stumbled across the SQL Query Optimizer team's blog (&lt;A href="http://blogs.msdn.com/queryoptteam" mce_href="http://blogs.msdn.com/queryoptteam"&gt;http://blogs.msdn.com/queryoptteam&lt;/A&gt;) and Ian Jose's blog (&lt;A href="http://blogs.msdn.com/ianjo/" mce_href="http://blogs.msdn.com/ianjo/"&gt;http://blogs.msdn.com/ianjo/&lt;/A&gt;).&amp;nbsp; These are both good reads and really informative.&amp;nbsp; These blogs are primarily about the SQL QO, and&amp;nbsp;both manage to get very useful information out there that&amp;nbsp;might otherwise remain locked up in Microsoft.&amp;nbsp; I think the typical database developer or DBA would benefit from reading them, and that made me feel better about my second concern.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Which left my concern about not having enough time to do something new.&amp;nbsp; But then I heard that &lt;/FONT&gt;&lt;FONT face=Arial size=2&gt;Ken Henderson has somehow found the time to hold down a full-time development job, write best-selling books, and still maintain three different blogs.&amp;nbsp; I was forced to abandon my theory that the gods have conspired to make our days too short, and concluded that maybe my first concern really boils down to crappy time management.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;I guess we'll see how it goes.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;SCRIPT src="http://www.google-analytics.com/urchin.js" type=text/javascript&gt;
&lt;/SCRIPT&gt;

&lt;SCRIPT type=text/javascript&gt;
_uacct = "UA-1763868-1";
urchinTracker();
&lt;/SCRIPT&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=630132" width="1" height="1"&gt;</description></item></channel></rss>