<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BlogmyQuery - BMQ &#187; Sqlserver</title>
	<atom:link href="http://blogmyquery.com/index.php/category/db/sqlserver/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogmyquery.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 Sep 2010 18:07:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Update Multiple Tables with one Statement</title>
		<link>http://blogmyquery.com/index.php/2009/07/update-multiple-tables-with-one-statement/</link>
		<comments>http://blogmyquery.com/index.php/2009/07/update-multiple-tables-with-one-statement/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 21:02:22 +0000</pubDate>
		<dc:creator>kranthi</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Update Multiple Tables]]></category>

		<guid isPermaLink="false">http://blogmyquery.com/?p=605</guid>
		<description><![CDATA[In real time applications there might be situation where you need update details of one table using details of another [...]]]></description>
			<content:encoded><![CDATA[<p>In real time applications there might be situation where you need update details of one table using details of another or update two or tables using one statement. Below is Example of how you can do that in SQL Server </p>
<pre class="brush: plain;">
Update t1 Set t1.col1 = t2.col1 from Table t1 inner join Table t2 on t1.col = t2.col where Condtn
</pre>
<p>Below is MYSql Example</p>
<pre class="brush: plain;">
Update Table t1 inner join Table t2 on t1.col = t2.col Set t1.col1 = t2.col1 where Condtn
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2009/07/update-multiple-tables-with-one-statement/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DateFirst in SQL Server</title>
		<link>http://blogmyquery.com/index.php/2009/07/datefirst-in-sql-server/</link>
		<comments>http://blogmyquery.com/index.php/2009/07/datefirst-in-sql-server/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 20:07:28 +0000</pubDate>
		<dc:creator>kranthi</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[DateFirst]]></category>
		<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://blogmyquery.com/?p=600</guid>
		<description><![CDATA[Select @@DateFirst

returns the current value, for a session, of SET DATE FIRST, i.e it returns first day of week, TinyInt Value from 1 through 7 representing Monday to Sunday as shown Below. ]]></description>
			<content:encoded><![CDATA[<pre class="brush: plain;">
Select @@DateFirst
</pre>
<p>returns the current value, for a session, of SET DATE FIRST, i.e it returns first day of week, TinyInt Value from 1 through 7 representing Monday to Sunday as shown Below. </p>
<pre class="brush: plain;">
1 for Monday
2 for Tuesday
3 for Wednesday
4 for Thursday
5 for Friday
6 for Saturday
7 for Sunday
</pre>
<p>For Example, If the first day of week is Sunday (US) , Then @@DateFirst would return 7.</p>
<p>If the first day of week is Monday (Europe), Then @@DateFirst Would return 1.</p>
<pre class="brush: plain;">
SET DateFirst = 3
</pre>
<p>The above statement would set Wednesday as first day of the week for the current Session.</p>
<p>&#8216;July 1st 2009 is a wednesday</p>
<pre class="brush: plain;">
SET DateFirst = 1 -- or SET LANGUAGE Italian;
Select DATEPART(dw, '20090701')
</pre>
<p>would return 3 (since Wednesday is 3rd day from Monday)</p>
<pre class="brush: plain;">
SET DateFirst = 7 -- or SET LANGUAGE us_english;
Select DATEPART(dw, '20090701')
</pre>
<p>would return 4 (since Wednesday is 4th day from Sunday)</p>
<p>DATEPART return the weekday according @@DateFirst.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2009/07/datefirst-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculate businessdays between two dates</title>
		<link>http://blogmyquery.com/index.php/2009/07/calculate-businessdays-between-two-dates/</link>
		<comments>http://blogmyquery.com/index.php/2009/07/calculate-businessdays-between-two-dates/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 18:55:21 +0000</pubDate>
		<dc:creator>jackjeetu1</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Calc Business Days]]></category>
		<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://blogmyquery.com/?p=593</guid>
		<description><![CDATA[This Example shows how to calculate the calculate the workdays between two given dates.
CREATE FUNCTION dbo.GetBusinessDays
(
@startDate SMALLDATETIME,
@endDate SMALLDATETIME
)
RETURNS INT
AS
BEGIN
DECLARE @dateDiff [...]]]></description>
			<content:encoded><![CDATA[<p>This Example shows how to calculate the calculate the workdays between two given dates.</p>
<p><span style="color: #800000;">CREATE FUNCTION dbo.GetBusinessDays<br />
(<br />
@startDate SMALLDATETIME,<br />
@endDate SMALLDATETIME<br />
)<br />
RETURNS INT<br />
AS<br />
BEGIN<br />
DECLARE @dateDiff INT;</span></p>
<p><span style="color: #800000;">SET @dateDiff = DATEDIFF(DAY, @startDate, @endDate)+1;</span></p>
<p><span style="color: #800000;">RETURN<br />
(<br />
SELECT @dateDiff/ 7 * 5 + CASE </span><span style="color: #800000;">WHEN </span><span style="color: #800000;">(@dateDiff % 7) = 0 THEN 0</span></p>
<p><span style="color: #800000;">ELSE<br />
</span></p>
<p><span style="color: #800000;"> CASE<br />
When (DATEPART(WEEKDAY, @endDate ) + @@DateFirst -1)%7 = 0 and </span><span style="color: #800000;">(@dateDiff % 7) =1 </span><span style="color: #800000;">Then </span><span style="color: #800000;">(@dateDiff % 7)-1</span><span style="color: #800000;"> &#8211;If it is sunday</span></p>
<p><span style="color: #800000;"> When (DATEPART(WEEKDAY, @endDate ) + @@DateFirst -1)%7 = 0 and </span><span style="color: #800000;">(@dateDiff % 7) &lt;&gt;1 </span><span style="color: #800000;"> </span><span style="color: #800000;"> </span><span style="color: #800000;">Then </span><span style="color: #800000;">(@dateDiff % 7)-2</span><span style="color: #800000;"> &#8211;If it is sunday<br />
</span></p>
<p><span style="color: #800000;"> When (DATEPART(WEEKDAY, @endDate ) </span><span style="color: #800000;">+ @@DateFirst</span><span style="color: #800000;"> -1)%7 =6 </span><span style="color: #800000;"> Then (@dateDiff % 7) &#8211; 1   &#8211;If it is Saturday</span></p>
<p><span style="color: #800000;">ELSE (@dateDiff % 7)<br />
END &#8212; Inner Case<br />
</span></p>
<p><span style="color: #800000;">END<br />
</span></p>
<p><span style="color: #800000;">);<br />
END</span></p>
<p><span style="color: #800000;"><span id="more-593"></span></span></p>
<p><span style="color: #000000;">There are many other ways of achieving this , you can also look at the following examples</span></p>
<p><span style="color: #800000;"><br />
</span></p>
<p><span style="color: #800000;">CREATE FUNCTION dbo.GetWorkingDays<br />
(<br />
@startDate SMALLDATETIME,<br />
@endDate   SMALLDATETIME<br />
)<br />
RETURNS INT<br />
AS<br />
BEGIN<br />
DECLARE @range INT;</span></p>
<p><span style="color: #800000;">SET @range = DATEDIFF(DAY, @startDate, @endDate)+1;</span></p>
<p><span style="color: #800000;">RETURN<br />
(<br />
SELECT<br />
@range / 7 * 5 + @range % 7 -<br />
(<br />
SELECT COUNT(*)<br />
FROM<br />
(<br />
SELECT 1 AS d<br />
UNION ALL SELECT 2<br />
UNION ALL SELECT 3<br />
UNION ALL SELECT 4<br />
UNION ALL SELECT 5<br />
UNION ALL SELECT 6<br />
UNION ALL SELECT 7<br />
) weekdays<br />
WHERE d &lt;= @range % 7<br />
AND DATENAME(WEEKDAY, @endDate &#8211; d + 1)<br />
IN<br />
(<br />
&#8216;Saturday&#8217;,<br />
&#8216;Sunday&#8217;<br />
)<br />
)<br />
);<br />
END<br />
GO</span></p>
<p><span style="color: #800000;"><span style="color: #000000;">or this that i found on one of the forums</span></span></p>
<pre><span style="color: #800000;">DECLARE @start_date DATETIME;
DECLARE @end_date DATETIME;

SET @start_date = '20080820';
SET @end_date = '20080825';

SELECT ((total_days / 7) * 5 + total_days % 7 -
CASE WHEN 6 BETWEEN start_weekday AND end_weekday
THEN 1 ELSE 0 END -
CASE WHEN 7 BETWEEN start_weekday AND end_weekday
THEN 1 ELSE 0 END)
FROM (SELECT total_days, start_weekday,
start_weekday + total_days % 7 - 1
FROM (SELECT DATEDIFF(day, @start_date, @end_date) + 1,
DATEPART(WEEKDAY, @start_date + @@DATEFIRST - 1)
) AS T(total_days, start_weekday)
) AS D(total_days, start_weekday, end_weekday);</span>
</pre>
<p><span style="color: #000000;">NOTE : This example doesnot holidays other than saturday and sunday into account.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2009/07/calculate-businessdays-between-two-dates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Migrating from MS SQL Server to MYSQL</title>
		<link>http://blogmyquery.com/index.php/2009/02/migrating-from-ms-sql-server-to-mysql/</link>
		<comments>http://blogmyquery.com/index.php/2009/02/migrating-from-ms-sql-server-to-mysql/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 17:04:26 +0000</pubDate>
		<dc:creator>kranthi</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[Sqlserver]]></category>

		<guid isPermaLink="false">http://blogmyquery.com/index.php/2009/02/migrating-from-ms-sql-server-to-mysql/</guid>
		<description><![CDATA[Migrating from MS SQL to MYSQL is a big pain. there are no good tools to use for migration of [...]]]></description>
			<content:encoded><![CDATA[<p>Migrating from MS SQL to MYSQL is a big pain. there are no good tools to use for migration of the all the datbase objects, right from tables , data to stored procedures. We all know that Microsoft SQL server has a very good User Interface but when we come back to MYSQL we donot enjoy the same previlages as we do in SQL server. </p>
<p>Coming to the tools available to download for a good GUI, we have very less options, MYSQL query , PHPadmin are some of them. There are some tools which can be used to migrate the tables and data from the MSSQL to MYSQL. The MYSQL migration tool is also available to migrate the tables. But iam not sure that we can also migrate the stored procedures, but when i tried to migrate a small application i totally failed to shift the stored procedures to MYSQL. I have to migrate them manually. I have to check the syntax manually for each and every line and then execute the procedure.<br />
There are some points which we have to keep in mind while migrating the database from MSSQL to MYSQL. The MYSQL statements has to end with semi-colon , even the ending of the for loop has to closed by a semi-colon.  </p>
<p>When we migrate the tables from MSSQL to MYSQL we do not get the perfect script which defines the primary keys and the default values. We have to turn them on manually.</p>
<p>So please be carefull while doing the application and choosing the database, the migration of the database is same as a small project. <img src='http://blogmyquery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>wish you happy programmin</p>
]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2009/02/migrating-from-ms-sql-server-to-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get Number of Days in a Month Function</title>
		<link>http://blogmyquery.com/index.php/2009/01/get-number-of-days-in-a-month-function/</link>
		<comments>http://blogmyquery.com/index.php/2009/01/get-number-of-days-in-a-month-function/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 03:35:40 +0000</pubDate>
		<dc:creator>kranthi</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Number of days in month]]></category>

		<guid isPermaLink="false">http://blogmyquery.com/?p=89</guid>
		<description><![CDATA[This article shows the ways to get the number of days in a month in both sqlserver and mysql.
Getting the [...]]]></description>
			<content:encoded><![CDATA[<p>This article shows the ways to get the number of days in a month in both sqlserver and mysql.</p>
<p>Getting the number of days in a month is quite easy because it is going to be either 30 or 31 days, with the exception of February, which can either have 28 or 29 days depending if it is a leap year or not.</p>
<ul>
<li> January,March,May,July,August,October,December &#8212; 31 Days</li>
<li> April,June,September,November &#8211; 30 Days</li>
<li> February &#8211; 28 Days (Non Leap Year), 29 (Leap Year)</li>
</ul>
<p>Leap year should be divisible by 4, should not be divisible by 100 or should be divisible by 400.</p>
<p><span id="more-89"></span></p>
<pre class="brush: css;">
CREATE FUNCTION [dbo].[udf_GetDaysInMonth] ( @pDate    DATETIME )
RETURNS INT
AS
BEGIN

RETURN CASE WHEN MONTH(@pDate) IN (1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN MONTH(@pDate) IN (4, 6, 9, 11) THEN 30
ELSE CASE WHEN (YEAR(@pDate) % 4    = 0 AND
YEAR(@pDate) % 100 != 0) OR
(YEAR(@pDate) % 400  = 0)
THEN 29
ELSE 28
END
END

END
GO
</pre>
<p>Here&#8217;s yet another way of determining the number of days in a month without knowing if it is a leap year.  It is computed by getting the lastday of the month.</p>
<pre class="brush: css;">
CREATE FUNCTION [dbo].[udf_GetDaysInMonth] ( @pDate    DATETIME )
RETURNS INT
AS
BEGIN
SET @pDate = @pDate - DAY(@pDate) + 1 -- Get the first date of the month
RETURN day(dateadd(dd,-1,@pDate))
END
GO
</pre>
<p>Get the number of days in month in <strong>MYSQL</strong></p>
<pre class="brush: css;">

select DAY(LAST_DAY('2004-02-05'))
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2009/01/get-number-of-days-in-a-month-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A few maximum limitations for Sqlserver and MySql</title>
		<link>http://blogmyquery.com/index.php/2009/01/sqlmaximums/</link>
		<comments>http://blogmyquery.com/index.php/2009/01/sqlmaximums/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 04:50:46 +0000</pubDate>
		<dc:creator>jvanamali</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[Sqlserver]]></category>

		<guid isPermaLink="false">http://blogmyquery.com/?p=23</guid>
		<description><![CDATA[Often people in newsgroups ask about maximum size of various objects defined in databases.
The following tables shows the maximum sizes [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: Verdana; font-size: x-small;">Often people in newsgroups ask about maximum size of various objects defined in databases.</span></p>
<p><span style="font-family: Verdana; font-size: x-small;">The following tables shows the maximum sizes and numbers of various objects defined in MYSql, SQL Server.<br />
</span></p>
<p><span style="font-family: Verdana; font-size: x-small;"><br />
</span></p>
<div>
<table id="tbl" border="0" width="100%">
<caption>MySql Maximums</caption>
<tbody>
<tr>
<td>Char</td>
<td>255 Bytes</td>
</tr>
<tr>
<td>Varchar</td>
<td>65,535 Bytes</td>
</tr>
<tr>
<td>Binary</td>
<td>255 Bytes</td>
</tr>
<tr>
<td>Varbinary</td>
<td>65,535 Bytes</td>
</tr>
<tr>
<td>TinyBlob</td>
<td>255 Bytes</td>
</tr>
<tr>
<td>TinyText</td>
<td>255 Bytes</td>
</tr>
<tr>
<td>Blob</td>
<td>65,535 Bytes (2^16 -1)</td>
</tr>
<tr>
<td>Text</td>
<td>65,535 Bytes (2^16 -1)</td>
</tr>
<tr>
<td>Medium Blob</td>
<td>16,777,215 Bytes (2^24 -1)</td>
</tr>
<tr>
<td>Medium Text</td>
<td>16,777,215 Bytes (2^24 -1)</td>
</tr>
<tr>
<td>LongBlog</td>
<td>4,294,967,295 Bytes (2^32 -1)</td>
</tr>
<tr>
<td>LongText</td>
<td>4,294,967,295 Bytes (2^32 -1)</td>
</tr>
<tr>
<td>Bit</td>
<td>Range from 1 to 64</td>
</tr>
<tr>
<td>Tinyint</td>
<td>The signed range is -128 to 127. The unsigned range is 0 to 255.</td>
</tr>
<tr>
<td>BOOL, BOOLEAN</td>
<td>true or false</td>
</tr>
<tr>
<td>SmallInt</td>
<td>The signed range is -32768 to 32767. The unsigned range is 0 to 65535.</td>
</tr>
<tr>
<td>MediumInt</td>
<td>The signed range is -8388608 to 8388607. The unsigned range is 0 to 16777215.</td>
</tr>
<tr>
<td>Int/Integer</td>
<td>The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.</td>
</tr>
<tr>
<td>BigInt</td>
<td>The signed range is -9223372036854775808 to 9223372036854775807. The unsigned range<br />
is 0 to 18446744073709551615.</td>
</tr>
<tr>
<td>Float</td>
<td>Allowable values are -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38<br />
to 3.402823466E+38.</td>
</tr>
<tr>
<td>Double</td>
<td>Allowable values are -1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and<br />
2.2250738585072014E-308 to 1.7976931348623157E+308.</td>
</tr>
</tbody>
</table>
</div>
<p><span id="more-23"></span></p>
<div>
<table id="tbl" border="0" width="100%">
<caption>Sqlserver Maximums </caption>
<tbody>
<tr>
<td>char</td>
<td>8,000 Char</td>
</tr>
<tr>
<td>nchar</td>
<td>4,000 char</td>
</tr>
<tr>
<td>varchar</td>
<td>8,000 char</td>
</tr>
<tr>
<td>nvarchar</td>
<td>8,000 char</td>
</tr>
<tr>
<td>varchar(max)</td>
<td>1,073,741,824 char</td>
</tr>
<tr>
<td>nvarchar(max)</td>
<td>536,870,912 char</td>
</tr>
<tr>
<td>Tinyint</td>
<td>The range is 0 to 255.</td>
</tr>
<tr>
<td>SmallInt</td>
<td>The signed range is -32768 to 32767.</td>
</tr>
<tr>
<td>BigInt</td>
<td>The signed range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.</td>
</tr>
<tr>
<td>Int</td>
<td>The signed range is -2,147,483,648 to 2,147,483,647.</td>
</tr>
<tr>
<td>money</td>
<td>The signed range is -922,337,203,685,477.5808 to 922,337,203,685,477.5807.</td>
</tr>
<tr>
<td>smallmoney</td>
<td>Allowable values are -214,748.3648 to 214,748.3647.</td>
</tr>
<tr>
<td>bit</td>
<td>single bit.</td>
</tr>
<tr>
<td>binary</td>
<td>8,000 bytes.</td>
</tr>
<tr>
<td>varbinary</td>
<td>8,000 bytes.</td>
</tr>
<tr>
<td>varbinary(max)</td>
<td>2 GB Data.</td>
</tr>
<tr>
<td>image</td>
<td>2 GB Data.</td>
</tr>
</tbody>
</table>
</div>
<div>
<table id="tbl" border="0" width="100%">
<caption>Other Helpful Sql Maximums</caption>
<tbody>
<tr>
<td align="left">Bytes per short string column</td>
<td align="left">8,000</td>
</tr>
<tr>
<td align="left">Bytes per GROUP BY, ORDER BY</td>
<td align="left">8,060</td>
</tr>
<tr>
<td align="left">Columns in GROUP BY, ORDER BY</td>
<td align="left">Limited only by number of bytes</td>
</tr>
<tr>
<td align="left">Bytes per index key</td>
<td align="left">900</td>
</tr>
<tr>
<td align="left">Bytes per foreign key</td>
<td align="left">900</td>
</tr>
<tr>
<td align="left">Bytes per primary key</td>
<td align="left">900</td>
</tr>
<tr>
<td align="left">Bytes per row</td>
<td align="left">8,060</td>
</tr>
<tr>
<td align="left">Bytes per varchar(max), varbinary(max), xml, text, or image column</td>
<td align="left">2^31-1</td>
</tr>
<tr>
<td align="left">Characters per ntext or nvarchar(max) column</td>
<td align="left">2^30-1</td>
</tr>
<tr>
<td align="left">Clustered indexes per table</td>
<td align="left">1</td>
</tr>
<tr>
<td align="left">Columns per index key</td>
<td align="left">16</td>
</tr>
<tr>
<td align="left">Columns per foreign key</td>
<td align="left">16</td>
</tr>
<tr>
<td align="left">Columns per primary key</td>
<td align="left">16</td>
</tr>
<tr>
<td align="left">Columns per base table</td>
<td align="left">1,024</td>
</tr>
<tr>
<td align="left">Columns per SELECT statement</td>
<td align="left">4,096</td>
</tr>
<tr>
<td align="left">Columns per INSERT statement</td>
<td align="left">1,024</td>
</tr>
<tr>
<td align="left">Connections per client</td>
<td align="left">32,767</td>
</tr>
<tr>
<td align="left">Database size</td>
<td align="left">1,048,516 terabytes</td>
</tr>
<tr>
<td align="left">Databases per instance of SQL Server</td>
<td align="left">32,767</td>
</tr>
<tr>
<td align="left">Filegroups per database</td>
<td align="left">32,767</td>
</tr>
<tr>
<td align="left">Files per database</td>
<td align="left">32,767</td>
</tr>
<tr>
<td align="left">File size (data)</td>
<td align="left">16 terabytes</td>
</tr>
<tr>
<td align="left">File size (log)</td>
<td align="left">2 terabytes</td>
</tr>
<tr>
<td align="left">Foreign key table references per table</td>
<td align="left">253</td>
</tr>
<tr>
<td align="left">Identifier length (in characters)</td>
<td align="left">128</td>
</tr>
<tr>
<td align="left">Instances per computer</td>
<td align="left">50 (Workgroup Edition only 16)</td>
</tr>
<tr>
<td align="left">Locks per connection</td>
<td align="left">Maximum locks per server</td>
</tr>
<tr>
<td align="left">Locks per instance of SQL Server</td>
<td align="left">Up to 2,147,483,647</td>
</tr>
<tr>
<td align="left">Nested stored procedure levels</td>
<td align="left">32</td>
</tr>
<tr>
<td align="left">Nested subqueries</td>
<td align="left">32</td>
</tr>
<tr>
<td align="left">Nested trigger levels</td>
<td align="left">32</td>
</tr>
<tr>
<td align="left">Nonclustered indexes per table</td>
<td align="left">249</td>
</tr>
<tr>
<td align="left">Parameters per stored procedure</td>
<td align="left">2,100</td>
</tr>
<tr>
<td align="left">Parameters per user-defined function</td>
<td align="left">2,100</td>
</tr>
<tr>
<td align="left">REFERENCES per table</td>
<td align="left">253</td>
</tr>
<tr>
<td align="left">Rows per table</td>
<td align="left">Limited by available storage</td>
</tr>
<tr>
<td align="left">Tables per database</td>
<td align="left">Limited by number of objects in a database</td>
</tr>
<tr>
<td align="left">Partitions per partitioned table or index</td>
<td align="left">1,000</td>
</tr>
<tr>
<td align="left">Statistics on non-indexed columns</td>
<td align="left">2,000</td>
</tr>
<tr>
<td align="left">Tables per SELECT statement</td>
<td align="left">256</td>
</tr>
<tr>
<td align="left">Triggers per table</td>
<td align="left">Limited by number of objects in a database</td>
</tr>
<tr>
<td align="left">UNIQUE indexes or constraints per table</td>
<td align="left">249 nonclustered and 1 clustered</td>
</tr>
<tr>
<td align="left">User connections</td>
<td align="left">32,767</td>
</tr>
<tr>
<td align="left">XML indexes</td>
<td align="left">249</td>
</tr>
</tbody>
</table>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2009/01/sqlmaximums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Read Statistics Profile</title>
		<link>http://blogmyquery.com/index.php/2006/08/how-to-read-statistics-profile/</link>
		<comments>http://blogmyquery.com/index.php/2006/08/how-to-read-statistics-profile/#comments</comments>
		<pubDate>Tue, 29 Aug 2006 19:56:00 +0000</pubDate>
		<dc:creator>QueryOptTeam</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[How to Read Statistics Profile]]></category>

		<guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:730521</guid>
		<description><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt">(2006-09-01 added a paragraph on parallel query plans)</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">&#160;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">In SQL Server, “Statistics Profile” is a mode in which a query is run where you can see the number of invocations of each physical plan operator in the tree.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Instead of running a query and just printing the output rows, this mode also collects and returns per-operator row counts.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Statistics Profile is used by the SQL Query Optimizer Team to identify issues with a plan which can cause the plan to perform poorly.<SPAN style="mso-spacerun: yes">&#160; </SPAN>For example, it can help identify a poor index choice or poor join order in a plan.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Oftentimes, it can help identify the needed solution, such as updating statistics (as in the histograms and other statistical information used during plan generation) or perhaps adding a plan hint.<SPAN style="mso-spacerun: yes">&#160; </SPAN>This document describes how to read the statistics profile information from a query plan so that you can also debug plan issues.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">A simple example query demonstrates how to retrieve the statistics profile output from a query:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">use nwind<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">set statistics profile on<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">select * from customers c inner join orders o on c.customerid = o.customerid;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">The profile output has a number of columns and is a bit tricky to print in a regular document.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The key pieces of information that it prints are the plan, which looks like this:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">StmtText<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------------------------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c inner join orders o on c.customerid = o.customerid<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160; </SPAN>&#124;--Hash Match(Inner Join, HASH:([c].[CustomerID])=([o].[CustomerID]),<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(OBJECT:([nwind].[dbo].[Customers].[aaaaa_PrimaryKey] AS [c]))<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(OBJECT:([nwind].[dbo].[Orders].[aaaaa_PrimaryKey] AS [o]))<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">Other pieces of useful information are the estimated row count and the actual row count for each operator and the estimated and actual number of invocations of this operator.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Note that the actual rows and # of executions are physically listed as early columns, while the other columns are listed later in the output column list (so you typically have to scroll over to see them).</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>Executes<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- --------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">91<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">EstimateRows<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------ <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1051.5834<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1051.5834<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">91.0<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078.0<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">EstimateExecutions<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------ <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">NULL<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1.0<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1.0<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1.0<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">Other fields, such as the estimated cost of the subtree, the output columns, the average row size, also exist in the output (but are omitted for space in this document).</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><I style="mso-bidi-font-style: normal">Note: The output from statistics profile is typically easiest to read if you set the output from your client (Query Analyzer or SQL Server Management Studio) to output to text, using a fixed-width font.<SPAN style="mso-spacerun: yes">&#160; </SPAN>You can then see the columns pretty easily and you can even move the results into a tool like Excel if you want to cluster the estimates and actual results near each other by rearranging the columns (SSMS also can let you do this).<o:p></o:p></I></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">There are a few key pieces of information needed to understand the output from Statistics profile.<SPAN style="mso-spacerun: yes">&#160; </SPAN>First, query plans are generally represented as trees.<SPAN style="mso-spacerun: yes">&#160; </SPAN>In the output, children are printed below their parents and are indented:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">StmtText<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------------------------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c inner join orders o on c.customerid = o.customerid<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160; </SPAN>&#124;--Hash Match(Inner Join, HASH:([c].[CustomerID])=([o].[CustomerID]),<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(OBJECT:([nwind].[dbo].[Customers].[aaaaa_PrimaryKey] AS [c]))<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(OBJECT:([nwind].[dbo].[Orders].[aaaaa_PrimaryKey] AS [o]))<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">In this example, the scan of Customers and Orders are both below a hash join operator.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Next, the “first” child of the operator is listed first.<SPAN style="mso-spacerun: yes">&#160; </SPAN>So, the Customers Scan is the first child of the hash join.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Subsequent operators follow, in order.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Finally, query execution plans are executed in this “first” to “last” order.<SPAN style="mso-spacerun: yes">&#160; </SPAN>So, for this plan, the very first row is returned from the Customers table.<SPAN style="mso-spacerun: yes">&#160; </SPAN>(A more detailed discussion of operators will happen later in the article).<SPAN style="mso-spacerun: yes">&#160; </SPAN>Notice that the output from the graphical showplan is similar but slightly different:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><o:lock aspectratio="t" v:ext="edit"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype></SPAN></o:lock></v:shapetype></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>(see attachment&#160;tree.jpg).</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">In this tree representation, both Scans are printed to the right on the screen, and the first child is above the other operators.<SPAN style="mso-spacerun: yes">&#160; </SPAN>In most Computer Science classes, trees are printed in a manner transposed from this:</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>Hash Join<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-FAMILY: 'Courier New'"><SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>/<SPAN style="mso-tab-count: 1">&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>\<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-FAMILY: 'Courier New'"><SPAN style="mso-spacerun: yes">&#160;</SPAN>Customers<SPAN style="mso-spacerun: yes">&#160; </SPAN>Orders<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-FAMILY: 'Courier New'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">The transposition makes printing trees in text easier.<SPAN style="mso-spacerun: yes">&#160; </SPAN>In this classical view, the tree is evaluated left to right with rows flowing bottom to top.<SPAN style="mso-spacerun: yes">&#160; </SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">With an understanding of the nuances of the query plan display, it is possible to understand what happens during the execution of this query plan.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The query returns 1078 rows.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Not coincidentally, there are also 1078 orders in this database.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Since there’s a Foreign Key relationship between Orders and Customers, it requires that a match exist for each order to each customer.<SPAN style="mso-spacerun: yes">&#160; </SPAN>So, the 91 rows in Customers match the 1078 rows in Orders to return the result.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">The query estimates that the join will return <SPAN style="mso-no-proof: yes">1051.5834 rows.<SPAN style="mso-spacerun: yes">&#160; </SPAN>First, this is a bit less than the actual (1078) but is not a substantial difference.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Given that the Query Optimizer is making educated guesses based on sampled statistical information that may itself be out-of-date, this estimate is actually pretty good.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Second, the number is not an integer because we use floating point for our estimates to improve accuracy on estimates we make.<SPAN style="mso-spacerun: yes">&#160; </SPAN>For this query, the number of executions is 1 for both the estimate and actual.<SPAN style="mso-spacerun: yes">&#160; </SPAN>This won’t always be the case, but it happens to be true for this query because of the way hash joins work.<SPAN style="mso-spacerun: yes">&#160; </SPAN>In a hash join, the first child is scanned and a hash table is built.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Once the hash join is built, the second child is then scanned and each row probes the hash table to see if there is a matching row.<SPAN style="mso-spacerun: yes">&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">Loops join does not work this way, as we’ll see in a slightly modified example.<SPAN style="mso-spacerun: yes">&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c <B style="mso-bidi-font-weight: normal">with (index=1)</B> inner <B style="mso-bidi-font-weight: normal">loop</B> join orders o <B style="mso-bidi-font-weight: normal">with (index=1)</B> on c.customerid = o.customerid<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">In this example, I’ve forced a loop join and the use of clustered indexes for each table.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The plan now looks like this:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">StmtText<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-----------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c with (index=1) inner loop join orders o<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160; </SPAN>&#124;--Nested Loops(Inner Join, WHERE:([nwind].[dbo].[Orders].[Cust<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(OBJECT:([nwind].[dbo].[Customers].<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Table Spool<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(OBJECT:([nwind].[dbo].[Orders<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">Beyond the different join algorithm, you’ll notice that there is now a table spool added to the plan.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The spool is on the second child (also called the “inner” child for loops join because it is usually invoked multiple times).<SPAN style="mso-spacerun: yes">&#160; </SPAN>The spool scans rows from its child and can store them for future invocations of the inner child.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The actual row count and execution count from the statistics profile is a bit different from the previous plan:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>Executes<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- -------------------- <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#60;--- <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place w:st="on">Loop</st1:place> Join<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">91<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#60;--- Scan of Customers<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">98098<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>91<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </SPAN>&#60;--- Spool<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;</SPAN>&#60;--- Scan of Orders<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">In this plan, the second child of the loop join is scanned 91 times returning a total number of 98098 rows.<SPAN style="mso-spacerun: yes">&#160; </SPAN>For the actual executions, the total number of rows is to sum of all invocations of that operator, so it is 91*1078=98098.<SPAN style="mso-spacerun: yes">&#160; </SPAN>This means that the inner side of this tree is scanned 91 times.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Nested Loops joins require rescans of the inner subtree (Hash Joins do not, as you saw in the first example).<SPAN style="mso-spacerun: yes">&#160; </SPAN>Note that the spool causes only one scan of the Orders table, and it only has one execution as a result.<SPAN style="mso-spacerun: yes">&#160; </SPAN>It isn’t hard to see that there are far more rows touched in this plan compared to the hash join, and thus it shouldn’t be a huge surprise that this plan runs more slowly.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><I style="mso-bidi-font-style: normal"><SPAN style="mso-no-proof: yes">Note: When comparing the estimated vs. actual number of rows, it is important to remember that the actual counts need to be divded by the actual number of executions to get a value that is comparable to the estimated number of rows returned.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The estimate is the per-invocation estimate.<o:p></o:p></SPAN></I></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes">As a more complicated example, we can try something with a few more operators and see how things work on one of the TPC-H benchmark queries (Query 8, for those who are interested, on a small-scale 100MB database):<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">SELECT<SPAN style="mso-tab-count: 1"> </SPAN>O_YEAR,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>SUM(CASE<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160; </SPAN>WHEN<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>NATION<SPAN style="mso-tab-count: 1"> </SPAN>= '<st1:country-region w:st="on"><st1:place w:st="on">MOROCCO</st1:place></st1:country-region>' <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>THEN<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>VOLUME <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>ELSE<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>0 <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>END) / SUM(VOLUME)<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>AS MKT_SHARE<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">FROM<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>(<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160; </SPAN>SELECT<SPAN style="mso-tab-count: 1"> </SPAN>datepart(yy,O_ORDERDATE)<SPAN style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>AS O_YEAR,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>L_EXTENDEDPRICE * (1-L_DISCOUNT)<SPAN style="mso-tab-count: 1">&#160; </SPAN>AS VOLUME,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>N2.N_NAME<SPAN style="mso-tab-count: 4">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>AS NATION<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>FROM<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>PART, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>SUPPLIER, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>LINEITEM, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>ORDERS, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>CUSTOMER, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>NATION N1, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>NATION N2, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>REGION<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>WHERE<SPAN style="mso-tab-count: 1">&#160; </SPAN>P_PARTKEY<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>= L_PARTKEY AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>S_SUPPKEY<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>= L_SUPPKEY AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>L_ORDERKEY<SPAN style="mso-tab-count: 1">&#160;&#160;&#160; </SPAN>= O_ORDERKEY AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>O_CUSTKEY<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>= C_CUSTKEY AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>C_NATIONKEY<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>= N1.N_NATIONKEY AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>N1.N_REGIONKEY<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>= R_REGIONKEY AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>R_NAME<SPAN style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>= '<st1:place w:st="on">AFRICA</st1:place>' AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>S_NATIONKEY<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>= N2.N_NATIONKEY AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>O_ORDERDATE<SPAN style="mso-tab-count: 1">&#160;&#160; </SPAN>BETWEEN '1995-01-01' AND '1996-12-31' AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>P_TYPE<SPAN style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>= 'PROMO BURNISHED NICKEL' AND<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>L_SHIPDATE<SPAN style="mso-tab-count: 1">&#160;&#160;&#160; </SPAN>&#62;= CONVERT(datetime,(1156)*(30),121) AND<SPAN style="mso-tab-count: 4">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>L_SHIPDATE<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#60;<SPAN style="mso-spacerun: yes">&#160; </SPAN>CONVERT(datetime,((1185)+(1))*(30),121)<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>)<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160; </SPAN>AS<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>ALL_NATIONS<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">GROUP<SPAN style="mso-tab-count: 1">&#160; </SPAN>BY<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>O_YEAR<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ORDER<SPAN style="mso-tab-count: 1">&#160; </SPAN>BY<SPAN style="mso-tab-count: 1">&#160;&#160;&#160;&#160; </SPAN>O_YEAR<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">As the queries get more complex, it gets harder to print them in a standard page of text.<SPAN style="mso-spacerun: yes">&#160; </SPAN>So, I’ve truncated the plan somewhat in this example.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Notice that the same tree format still exists, and the main operators in this query are Scans, Seeks, Hash Joins, Stream Aggregates, a Sort, and a Loop Join.<SPAN style="mso-spacerun: yes">&#160; </SPAN>I’ve also included the actual number of rows and actual number of executions columns as well.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<SPAN style="mso-spacerun: yes">&#160; </SPAN>Executes<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>Plan<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">0<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </SPAN>0 Compute Scalar(DEFINE:([Expr1028]=[Expr1026]/[Expr1027]))<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">2<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>&#124;--Stream Aggregate(GROUP BY:([Expr1024]) DEFINE:([Expr1026]=SUM([par<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">2<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Nested Loops(Inner Join, OUTER REFERENCES:([N1].[N_REGIONKEY]<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">10<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Stream Aggregate(GROUP BY:([Expr1024], [N1].[N_REGIONKEY<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Sort(ORDER BY:([Expr1024] ASC, [N1].[N_REGIONKEY] A<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Hash Match(Inner Join, HASH:([N2].[N_NATIONKEY<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">25<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(OBJECT:([tpch100M].[<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Hash Match(Inner Join, HASH:([N1].[N_NATI<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">25<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Index Scan(OBJECT:([tpch100M].[dbo].<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Hash Match(Inner Join, HASH:([tpch10<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Index Scan(OBJECT:([tpch100M].[<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Hash Match(Inner Join, HASH:([t<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Hash Match(Inner Join, HAS<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1432<SPAN style="mso-spacerun: yes">&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Hash Match(Inner Join<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">126<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Clustered Index <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">0<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </SPAN>0<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Compute Scalar(D<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">224618 1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered I<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">0<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </SPAN>0<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Compute Scalar(DEFINE<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">45624<SPAN style="mso-spacerun: yes">&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">15000<SPAN style="mso-spacerun: yes">&#160; </SPAN>1<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Index Scan(OBJECT:([tpch10<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">2<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </SPAN>10<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Seek(OBJECT:([tpch100M].[dbo].[REGION].[<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">I’ll point out a few details about the statistics profile output.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Notice that the Compute Scalars (also called Projects) return zero for both columns.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Since Compute Scalar always returns exactly as many rows as it is given from its child, there isn’t any logic to count rows again in this operator simply for performance reasons.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The zeros can be safely ignored, and the values for its child can be used instead.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Another interesting detail can be seen in the last operator in this printout (the Seek into the Region table).<SPAN style="mso-spacerun: yes">&#160; </SPAN>In this operator, there are 10 executions but only 2 rows returned.<SPAN style="mso-spacerun: yes">&#160; </SPAN>This means that even though there were 10 attempts to find rows in this index, only two rows were ever found.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The parent operator (the Nested Loops near the top) has 10 rows coming from its first (left) child and only 2 rows output by the operator, which matches what you see in the seek.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Another interesting tidbit can be found if you look at the estimates for the Seek operator:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Est.# rows<SPAN style="mso-spacerun: yes">&#160; </SPAN>Est. #executes <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 87pt; TEXT-INDENT: -87pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 87.0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: 'Courier New'"><SPAN style="mso-list: Ignore">1.0<SPAN style="FONT: 7pt 'Times New Roman'">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN></SPAN></SPAN></B><SPAN style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes">20.106487 <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">The SQL Server Query Optimizer will estimate a minimum of one row coming out of a seek operator.<SPAN style="mso-spacerun: yes">&#160; </SPAN>This is done to avoid the case when a very expensive subtree is picked due to an cardinality underestimation.<SPAN style="mso-spacerun: yes">&#160; </SPAN>If the subtree is estimated to return zero rows, many plans cost about the same and there can be errors in plan selection as a result.<SPAN style="mso-spacerun: yes">&#160; </SPAN>So, you’ll notice that the estimation is “high” for this case, and some errors could result.<SPAN style="mso-spacerun: yes">&#160; </SPAN>You also might notice that we estimate 20 executions of this branch instead of the actual 10.<SPAN style="mso-spacerun: yes">&#160; </SPAN>However, given the number of joins that have been evaluated before this operator, being off by a factor of 2 (10 rows) isn’t considered to be too bad.<SPAN style="mso-spacerun: yes">&#160; </SPAN>(Errors can increase exponentially with the number of joins).<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></SPAN></P><SPAN style="mso-no-proof: yes"><o:p>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">SQL Server supports executing query plans in parallel.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Parallelism can add complexity to the statistics profile output as there are different kinds of parallelism that have different impacts on the counters for each operator.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Parallel Scans exist at the leaves of the tree, and these will count all rows from the table into each thread even through each thread only returns a fraction of the rows.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The number of executions (the second column in the output) will also have 1 execution for each thread.<SPAN style="mso-spacerun: yes">&#160; </SPAN>So, it is typical to just divide the number of threads into the total number of rows to see how many rows were actually returned by the table.<SPAN style="mso-spacerun: yes">&#160; </SPAN>Parallel zones higher in the tree usually work the same way.<SPAN style="mso-spacerun: yes">&#160; </SPAN>These will have N (where N is the degree of parallelism) more executions than the equivalent non-parallel query.<SPAN style="mso-spacerun: yes">&#160; </SPAN>There are a few cases where we will broadcast one row to multiple threads.<SPAN style="mso-spacerun: yes">&#160; </SPAN>If you examine the type of the parallelism exchange operation, you can identify these cases and notice that one row becomes multiple rows through the counts in the statistics profile results.<o:p></o:p></SPAN></P></o:p></SPAN>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-no-proof: yes"><o:p></o:p></SPAN>&#160;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes">The most common use of the statistics profile output is to identify areas where the Optimizer may be seeing and using incomplete or incorrect information.<SPAN style="mso-spacerun: yes">&#160; </SPAN>This is often the root cause of many performance problems in queries.<SPAN style="mso-spacerun: yes">&#160; </SPAN>If you can identify areas where the estimated and actual cardinality values are far apart, then you likely have found a reason why the Optimizer is not returning the “best” plan.<SPAN style="mso-spacerun: yes">&#160; </SPAN>The reasons for the estimate being incorrect can vary, but it can include missing or out-of-date statistics, too low of a sample rate on those statistics, correlations between data columns, or use of operators outside of the optimizer’s statistical model, to name a few common cases.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="mso-no-proof: yes"><o:p>&#160;</o:p></]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">(2006-09-01 added a paragraph on parallel query plans)</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">In SQL Server, “Statistics Profile” is a mode in which a query is run where you can see the number of invocations of each physical plan operator in the tree.<span style="mso-spacerun: yes"> </span>Instead of running a query and just printing the output rows, this mode also collects and returns per-operator row counts.<span style="mso-spacerun: yes"> </span>Statistics Profile is used by the SQL Query Optimizer Team to identify issues with a plan which can cause the plan to perform poorly.<span style="mso-spacerun: yes"> </span>For example, it can help identify a poor index choice or poor join order in a plan.<span style="mso-spacerun: yes"> </span>Oftentimes, it can help identify the needed solution, such as updating statistics (as in the histograms and other statistical information used during plan generation) or perhaps adding a plan hint.<span style="mso-spacerun: yes"> </span>This document describes how to read the statistics profile information from a query plan so that you can also debug plan issues.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">A simple example query demonstrates how to retrieve the statistics profile output from a query:</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">use nwind</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">set statistics profile on</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">select * from customers c inner join orders o on c.customerid = o.customerid;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <span id="more-199"></span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">The profile output has a number of columns and is a bit tricky to print in a regular document.<span style="mso-spacerun: yes"> </span>The key pieces of information that it prints are the plan, which looks like this:</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">StmtText<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------------------------------------------------------------------------------------</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c inner join orders o on c.customerid = o.customerid<span style="mso-spacerun: yes"> </span></span></p>

<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join, HASH:([c][/c]


.[CustomerID])=([o].[CustomerID]),&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style=&quot;MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none&quot;&gt;&lt;SPAN style=&quot;FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes&quot;&gt;&lt;SPAN style=&quot;mso-spacerun: yes&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;|--Clustered Index Scan(OBJECT:([nwind].[dbo].[Customers].[aaaaa_PrimaryKey] AS [c][/c]

))<span style="mso-spacerun: yes"> </span>

</span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Clustered Index Scan(OBJECT:([nwind].[dbo].[Orders].[aaaaa_PrimaryKey] AS [o]))<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">Other pieces of useful information are the estimated row count and the actual row count for each operator and the estimated and actual number of invocations of this operator.<span style="mso-spacerun: yes"> </span>Note that the actual rows and # of executions are physically listed as early columns, while the other columns are listed later in the output column list (so you typically have to scroll over to see them).</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<span style="mso-spacerun: yes"> </span>Executes<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- --------------------</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">91<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">EstimateRows<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------ </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1051.5834<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1051.5834<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">91.0<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078.0<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">EstimateExecutions<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------ </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">NULL</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1.0</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1.0</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1.0</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">Other fields, such as the estimated cost of the subtree, the output columns, the average row size, also exist in the output (but are omitted for space in this document).</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><em style="mso-bidi-font-style: normal">Note: The output from statistics profile is typically easiest to read if you set the output from your client (Query Analyzer or SQL Server Management Studio) to output to text, using a fixed-width font.<span style="mso-spacerun: yes"> </span>You can then see the columns pretty easily and you can even move the results into a tool like Excel if you want to cluster the estimates and actual results near each other by rearranging the columns (SSMS also can let you do this).</em></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">There are a few key pieces of information needed to understand the output from Statistics profile.<span style="mso-spacerun: yes"> </span>First, query plans are generally represented as trees.<span style="mso-spacerun: yes"> </span>In the output, children are printed below their parents and are indented:</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">StmtText<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">------------------------------------------------------------------------------------------------------</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c inner join orders o on c.customerid = o.customerid<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span></span></p>

<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join, HASH:([c][/c]


.[CustomerID])=([o].[CustomerID]),&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style=&quot;MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none&quot;&gt;&lt;SPAN style=&quot;FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes&quot;&gt;&lt;SPAN style=&quot;mso-spacerun: yes&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;|--Clustered Index Scan(OBJECT:([nwind].[dbo].[Customers].[aaaaa_PrimaryKey] AS [c][/c]

))<span style="mso-spacerun: yes"> </span>

</span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 7pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Clustered Index Scan(OBJECT:([nwind].[dbo].[Orders].[aaaaa_PrimaryKey] AS [o]))<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">In this example, the scan of Customers and Orders are both below a hash join operator.<span style="mso-spacerun: yes"> </span>Next, the “first” child of the operator is listed first.<span style="mso-spacerun: yes"> </span>So, the Customers Scan is the first child of the hash join.<span style="mso-spacerun: yes"> </span>Subsequent operators follow, in order.<span style="mso-spacerun: yes"> </span>Finally, query execution plans are executed in this “first” to “last” order.<span style="mso-spacerun: yes"> </span>So, for this plan, the very first row is returned from the Customers table.<span style="mso-spacerun: yes"> </span>(A more detailed discussion of operators will happen later in the article).<span style="mso-spacerun: yes"> </span>Notice that the output from the graphical showplan is similar but slightly different:</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">(see attachment tree.jpg).</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">In this tree representation, both Scans are printed to the right on the screen, and the first child is above the other operators.<span style="mso-spacerun: yes"> </span>In most Computer Science classes, trees are printed in a manner transposed from this:</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-FAMILY: 'Courier New'"><span style="mso-tab-count: 1"> </span>Hash Join</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-FAMILY: 'Courier New'"><span style="mso-tab-count: 1"> </span>/<span style="mso-tab-count: 1"> </span><span style="mso-spacerun: yes"> </span>\</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-FAMILY: 'Courier New'"><span style="mso-spacerun: yes"> </span>Customers<span style="mso-spacerun: yes"> </span>Orders</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-FAMILY: 'Courier New'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">The transposition makes printing trees in text easier.<span style="mso-spacerun: yes"> </span>In this classical view, the tree is evaluated left to right with rows flowing bottom to top.<span style="mso-spacerun: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">With an understanding of the nuances of the query plan display, it is possible to understand what happens during the execution of this query plan.<span style="mso-spacerun: yes"> </span>The query returns 1078 rows.<span style="mso-spacerun: yes"> </span>Not coincidentally, there are also 1078 orders in this database.<span style="mso-spacerun: yes"> </span>Since there’s a Foreign Key relationship between Orders and Customers, it requires that a match exist for each order to each customer.<span style="mso-spacerun: yes"> </span>So, the 91 rows in Customers match the 1078 rows in Orders to return the result.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">The query estimates that the join will return <span style="mso-no-proof: yes">1051.5834 rows.<span style="mso-spacerun: yes"> </span>First, this is a bit less than the actual (1078) but is not a substantial difference.<span style="mso-spacerun: yes"> </span>Given that the Query Optimizer is making educated guesses based on sampled statistical information that may itself be out-of-date, this estimate is actually pretty good.<span style="mso-spacerun: yes"> </span>Second, the number is not an integer because we use floating point for our estimates to improve accuracy on estimates we make.<span style="mso-spacerun: yes"> </span>For this query, the number of executions is 1 for both the estimate and actual.<span style="mso-spacerun: yes"> </span>This won’t always be the case, but it happens to be true for this query because of the way hash joins work.<span style="mso-spacerun: yes"> </span>In a hash join, the first child is scanned and a hash table is built.<span style="mso-spacerun: yes"> </span>Once the hash join is built, the second child is then scanned and each row probes the hash table to see if there is a matching row.<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">Loops join does not work this way, as we’ll see in a slightly modified example.<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c <strong style="mso-bidi-font-weight: normal">with (index=1)</strong> inner <strong style="mso-bidi-font-weight: normal">loop</strong> join orders o <strong style="mso-bidi-font-weight: normal">with (index=1)</strong> on c.customerid = o.customerid</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">In this example, I’ve forced a loop join and the use of clustered indexes for each table.<span style="mso-spacerun: yes"> </span>The plan now looks like this:</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">StmtText<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-----------------------------------------------------------------</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select * from customers c with (index=1) inner loop join orders o</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Nested Loops(Inner Join, WHERE:([nwind].[dbo].[Orders].[Cust</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Clustered Index Scan(OBJECT:([nwind].[dbo].[Customers].</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Table Spool<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-spacerun: yes"> </span>|--Clustered Index Scan(OBJECT:([nwind].[dbo].[Orders</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">Beyond the different join algorithm, you’ll notice that there is now a table spool added to the plan.<span style="mso-spacerun: yes"> </span>The spool is on the second child (also called the “inner” child for loops join because it is usually invoked multiple times).<span style="mso-spacerun: yes"> </span>The spool scans rows from its child and can store them for future invocations of the inner child.<span style="mso-spacerun: yes"> </span>The actual row count and execution count from the statistics profile is a bit different from the previous plan:</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<span style="mso-spacerun: yes"> </span>Executes<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- -------------------- </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>&lt;--- Loop Join</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">91<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>&lt;--- Scan of Customers</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">98098<span style="mso-spacerun: yes"> </span>91<span style="mso-spacerun: yes"> </span>&lt;--- Spool</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1078<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>&lt;--- Scan of Orders</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">In this plan, the second child of the loop join is scanned 91 times returning a total number of 98098 rows.<span style="mso-spacerun: yes"> </span>For the actual executions, the total number of rows is to sum of all invocations of that operator, so it is 91*1078=98098.<span style="mso-spacerun: yes"> </span>This means that the inner side of this tree is scanned 91 times.<span style="mso-spacerun: yes"> </span>Nested Loops joins require rescans of the inner subtree (Hash Joins do not, as you saw in the first example).<span style="mso-spacerun: yes"> </span>Note that the spool causes only one scan of the Orders table, and it only has one execution as a result.<span style="mso-spacerun: yes"> </span>It isn’t hard to see that there are far more rows touched in this plan compared to the hash join, and thus it shouldn’t be a huge surprise that this plan runs more slowly.</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><em style="mso-bidi-font-style: normal"><span style="mso-no-proof: yes">Note: When comparing the estimated vs. actual number of rows, it is important to remember that the actual counts need to be divded by the actual number of executions to get a value that is comparable to the estimated number of rows returned.<span style="mso-spacerun: yes"> </span>The estimate is the per-invocation estimate.</span></em></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes">As a more complicated example, we can try something with a few more operators and see how things work on one of the TPC-H benchmark queries (Query 8, for those who are interested, on a small-scale 100MB database):</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">SELECT<span style="mso-tab-count: 1"> </span>O_YEAR,</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 1"> </span>SUM(CASE<span style="mso-tab-count: 1"> </span>WHEN<span style="mso-tab-count: 1"> </span>NATION<span style="mso-tab-count: 1"> </span>= 'MOROCCO' </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>THEN<span style="mso-tab-count: 1"> </span>VOLUME </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>ELSE<span style="mso-tab-count: 1"> </span>0 </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>END) / SUM(VOLUME)<span style="mso-tab-count: 1"> </span>AS MKT_SHARE</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">FROM<span style="mso-tab-count: 1"> </span>(<span style="mso-tab-count: 1"> </span>SELECT<span style="mso-tab-count: 1"> </span>datepart(yy,O_ORDERDATE)<span style="mso-tab-count: 2"> </span>AS O_YEAR,</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>L_EXTENDEDPRICE * (1-L_DISCOUNT)<span style="mso-tab-count: 1"> </span>AS VOLUME,</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>N2.N_NAME<span style="mso-tab-count: 4"> </span>AS NATION</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 2"> </span>FROM<span style="mso-tab-count: 1"> </span>PART, </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>SUPPLIER, </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>LINEITEM, </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>ORDERS, </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>CUSTOMER, </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>NATION N1, </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>NATION N2, </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>REGION</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 2"> </span>WHERE<span style="mso-tab-count: 1"> </span>P_PARTKEY<span style="mso-tab-count: 1"> </span>= L_PARTKEY AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>S_SUPPKEY<span style="mso-tab-count: 1"> </span>= L_SUPPKEY AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>L_ORDERKEY<span style="mso-tab-count: 1"> </span>= O_ORDERKEY AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>O_CUSTKEY<span style="mso-tab-count: 1"> </span>= C_CUSTKEY AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>C_NATIONKEY<span style="mso-tab-count: 1"> </span>= N1.N_NATIONKEY AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>N1.N_REGIONKEY<span style="mso-tab-count: 1"> </span>= R_REGIONKEY AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>R_NAME<span style="mso-tab-count: 2"> </span>= 'AFRICA' AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>S_NATIONKEY<span style="mso-tab-count: 1"> </span>= N2.N_NATIONKEY AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>O_ORDERDATE<span style="mso-tab-count: 1"> </span>BETWEEN '1995-01-01' AND '1996-12-31' AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>P_TYPE<span style="mso-tab-count: 2"> </span>= 'PROMO BURNISHED NICKEL' AND</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 3"> </span>L_SHIPDATE<span style="mso-tab-count: 1"> </span>&gt;= CONVERT(datetime,(1156)*(30),121) AND<span style="mso-tab-count: 4"> </span>L_SHIPDATE<span style="mso-tab-count: 1"> </span>&lt;<span style="mso-spacerun: yes"> </span>CONVERT(datetime,((1185)+(1))*(30),121)<span style="mso-tab-count: 1"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><span style="mso-tab-count: 1"> </span>)<span style="mso-tab-count: 1"> </span>AS<span style="mso-tab-count: 1"> </span>ALL_NATIONS</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">GROUP<span style="mso-tab-count: 1"> </span>BY<span style="mso-tab-count: 1"> </span>O_YEAR</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">ORDER<span style="mso-tab-count: 1"> </span>BY<span style="mso-tab-count: 1"> </span>O_YEAR</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">As the queries get more complex, it gets harder to print them in a standard page of text.<span style="mso-spacerun: yes"> </span>So, I’ve truncated the plan somewhat in this example.<span style="mso-spacerun: yes"> </span>Notice that the same tree format still exists, and the main operators in this query are Scans, Seeks, Hash Joins, Stream Aggregates, a Sort, and a Loop Join.<span style="mso-spacerun: yes"> </span>I’ve also included the actual number of rows and actual number of executions columns as well.</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<span style="mso-spacerun: yes"> </span>Executes<span style="mso-spacerun: yes"> </span>Plan</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">0<span style="mso-spacerun: yes"> </span>0 Compute Scalar(DEFINE:([Expr1028]=[Expr1026]/[Expr1027]))<span style="mso-spacerun: yes"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">2<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|--Stream Aggregate(GROUP BY:([Expr1024]) DEFINE:([Expr1026]=SUM([par</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">2<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|--Nested Loops(Inner Join, OUTER REFERENCES:([N1].[N_REGIONKEY]</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">10<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|--Stream Aggregate(GROUP BY:([Expr1024], [N1].[N_REGIONKEY</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Sort(ORDER BY:([Expr1024] ASC, [N1].[N_REGIONKEY] A</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join, HASH:([N2].[N_NATIONKEY</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">25<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Clustered Index Scan(OBJECT:([tpch100M].[</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join, HASH:([N1].[N_NATI</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">25<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Index Scan(OBJECT:([tpch100M].[dbo].</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join, HASH:([tpch10</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Index Scan(OBJECT:([tpch100M].[</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join, HASH:([t</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1160<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join, HAS</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1432<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Hash Match(Inner Join</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">126<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Clustered Index </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">0<span style="mso-spacerun: yes"> </span>0<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Compute Scalar(D</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">224618 1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Clustered I</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">0<span style="mso-spacerun: yes"> </span>0<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Compute Scalar(DEFINE</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">45624<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Clustered Index </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">15000<span style="mso-spacerun: yes"> </span>1<span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Index Scan(OBJECT:([tpch10</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">2<span style="mso-spacerun: yes"> </span>10<span style="mso-spacerun: yes"> </span>|--Clustered Index Seek(OBJECT:([tpch100M].[dbo].[REGION].[</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify; mso-layout-grid-align: none"><span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">I’ll point out a few details about the statistics profile output.<span style="mso-spacerun: yes"> </span>Notice that the Compute Scalars (also called Projects) return zero for both columns.<span style="mso-spacerun: yes"> </span>Since Compute Scalar always returns exactly as many rows as it is given from its child, there isn’t any logic to count rows again in this operator simply for performance reasons.<span style="mso-spacerun: yes"> </span>The zeros can be safely ignored, and the values for its child can be used instead.<span style="mso-spacerun: yes"> </span>Another interesting detail can be seen in the last operator in this printout (the Seek into the Region table).<span style="mso-spacerun: yes"> </span>In this operator, there are 10 executions but only 2 rows returned.<span style="mso-spacerun: yes"> </span>This means that even though there were 10 attempts to find rows in this index, only two rows were ever found.<span style="mso-spacerun: yes"> </span>The parent operator (the Nested Loops near the top) has 10 rows coming from its first (left) child and only 2 rows output by the operator, which matches what you see in the seek.<span style="mso-spacerun: yes"> </span>Another interesting tidbit can be found if you look at the estimates for the Seek operator:</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Est.# rows<span style="mso-spacerun: yes"> </span>Est. #executes </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt 87pt; TEXT-INDENT: -87pt; TEXT-ALIGN: justify; mso-list: l0 level1 lfo1; tab-stops: list 87.0pt"><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes; mso-fareast-font-family: 'Courier New'"><span style="mso-list: Ignore">1.0<span style="FONT: 7pt 'Times New Roman'"> </span></span></span></strong><span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes">20.106487 </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">The SQL Server Query Optimizer will estimate a minimum of one row coming out of a seek operator.<span style="mso-spacerun: yes"> </span>This is done to avoid the case when a very expensive subtree is picked due to an cardinality underestimation.<span style="mso-spacerun: yes"> </span>If the subtree is estimated to return zero rows, many plans cost about the same and there can be errors in plan selection as a result.<span style="mso-spacerun: yes"> </span>So, you’ll notice that the estimation is “high” for this case, and some errors could result.<span style="mso-spacerun: yes"> </span>You also might notice that we estimate 20 executions of this branch instead of the actual 10.<span style="mso-spacerun: yes"> </span>However, given the number of joins that have been evaluated before this operator, being off by a factor of 2 (10 rows) isn’t considered to be too bad.<span style="mso-spacerun: yes"> </span>(Errors can increase exponentially with the number of joins).</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes"> </span></p>
<span style="mso-no-proof: yes"> </span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">SQL Server supports executing query plans in parallel.<span style="mso-spacerun: yes"> </span>Parallelism can add complexity to the statistics profile output as there are different kinds of parallelism that have different impacts on the counters for each operator.<span style="mso-spacerun: yes"> </span>Parallel Scans exist at the leaves of the tree, and these will count all rows from the table into each thread even through each thread only returns a fraction of the rows.<span style="mso-spacerun: yes"> </span>The number of executions (the second column in the output) will also have 1 execution for each thread.<span style="mso-spacerun: yes"> </span>So, it is typical to just divide the number of threads into the total number of rows to see how many rows were actually returned by the table.<span style="mso-spacerun: yes"> </span>Parallel zones higher in the tree usually work the same way.<span style="mso-spacerun: yes"> </span>These will have N (where N is the degree of parallelism) more executions than the equivalent non-parallel query.<span style="mso-spacerun: yes"> </span>There are a few cases where we will broadcast one row to multiple threads.<span style="mso-spacerun: yes"> </span>If you examine the type of the parallelism exchange operation, you can identify these cases and notice that one row becomes multiple rows through the counts in the statistics profile results.</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">The most common use of the statistics profile output is to identify areas where the Optimizer may be seeing and using incomplete or incorrect information.<span style="mso-spacerun: yes"> </span>This is often the root cause of many performance problems in queries.<span style="mso-spacerun: yes"> </span>If you can identify areas where the estimated and actual cardinality values are far apart, then you likely have found a reason why the Optimizer is not returning the “best” plan.<span style="mso-spacerun: yes"> </span>The reasons for the estimate being incorrect can vary, but it can include missing or out-of-date statistics, too low of a sample rate on those statistics, correlations between data columns, or use of operators outside of the optimizer’s statistical model, to name a few common cases.</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><span style="mso-no-proof: yes">Future posts will cover some of the details associated with tracking down each of these cases.</span></p>
Source : http://blogs.msdn.com/queryoptteam/default.aspx]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2006/08/how-to-read-statistics-profile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UPDATE STATISTICS undocumented options</title>
		<link>http://blogmyquery.com/index.php/2006/07/update-statistics-undocumented-options/</link>
		<comments>http://blogmyquery.com/index.php/2006/07/update-statistics-undocumented-options/#comments</comments>
		<pubDate>Fri, 21 Jul 2006 23:08:00 +0000</pubDate>
		<dc:creator>QueryOptTeam</dc:creator>
				<category><![CDATA[Sqlserver]]></category>

		<guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:674350</guid>
		<description><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt">If you read the Books Online page describing the UPDATE STATISTICS command, you will see that there are some undocumented options.<SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">UPDATE STATISTICS table &#124; view <BR>&#160;&#160;&#160; [ <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { index &#124; statistics_name }<BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#124; ( { index &#124;statistics_name } [ ,...n ] ) <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<BR>&#160;&#160;&#160; ] <BR>&#160;&#160;&#160; [&#160;&#160;&#160; WITH <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ FULLSCAN ] <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#124; SAMPLE number { PERCENT &#124; ROWS } ] <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#124; RESAMPLE <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#124; &#60;update_stats_stream_option&#62; [ ,...n ]<BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ] <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ [ , ] [ ALL &#124; COLUMNS &#124; INDEX ] <BR>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ [ , ] NORECOMPUTE ] <BR>&#160;&#160;&#160; ] ;<BR><BR></SPAN><STRONG><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'">&#60;update_stats_stream_option&#62; ::=</SPAN></STRONG><B><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'"><BR><STRONG><SPAN style="FONT-FAMILY: 'Courier New'">&#160;&#160;&#160; [ STATS_STREAM = stats_stream ]</SPAN></STRONG><BR><STRONG><SPAN style="FONT-FAMILY: 'Courier New'">&#160;&#160;&#160; [ ROWCOUNT = numeric_constant ]</SPAN></STRONG><BR><STRONG><SPAN style="FONT-FAMILY: 'Courier New'">&#160;&#160;&#160; [ PAGECOUNT = numeric contant ]</SPAN></STRONG></SPAN></B><o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">&#160;<o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><STRONG><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'">&#60;update_stats_stream_option&#62; </SPAN></STRONG><o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><STRONG><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'">&#160;&#160;&#160; This syntax is for internal use only and is not supported. Microsoft reserves the right to change this syntax at any time.</SPAN></STRONG><o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">There is a very good reason why these options are undocumented. They are meant for testing and debugging purposes, and should never ever be used on production systems.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">However, these options can also be extremely helpful to create examples and sample scripts to demonstrate various features and behaviors of the Query Optimizer and the related query plan shapes. We decided to explain what ROWCOUNT and PAGECOUNT do, so that to be able to use these commands in examples we’ll be posting in some future. Feel free to use these options on development systems for experimental and educational purposes, but please do not play with fire and do not use them in production!</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">As the name of these options suggest, ROWCOUNT and PAGECOUNT alter the internal metadata of the specified table or index by overriding the counters containing the row and page counts of the object. These counters are in turn read by the Query Optimizer when processing queries that access the table and/or index in question. These commands can basically cheat the Optimizer into thinking that a table or index is extremely large.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">SQL Server’s Query Optimization process is structured in multiple stages. Further optimization stages consider a progressively larger and more sophisticated set of possible tree transformations and query optimizations. Later stages of optimization are only entered when the estimated cost of the query is sufficiently high, in order to avoid wasting precious CPU cycles against simple queries that do not need that level of sophistication anyway. The multiple optimization stages are a mean to produce efficient query plans without consuming excessive amounts of CPU. Typically, in order to make the Optimizer “think” a lot and enter these later stages it is necessary to have big tables with a large number of rows and pages, which in turn take time and space to populate. Using ROWCOUNT and PAGECOUNT allows us to exercise these code paths with relatively simple scripts that do not require an extremely complex setup phase.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Here is an example. When running this simple script on your SQL 2005 instance you will likely see a different query plan for the two selects before and after updating the statistics. The recompile option is used to ensure that the query plans are regenerated. From the statistics profile, you’ll also see very different estimated row counts and consequently costs.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">use tempdb<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table t1(i int, j int)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table t2(h int, k int)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">set statistics profile on<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select distinct(i) from t1<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select * from t1, t2 where i = k order by j + k<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">update statistics t1 with rowcount = 10000, pagecount = 10000<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">update statistics t2 with rowcount = 100000, pagecount = 100000<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select distinct(i) from t1 option (recompile)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select * from t1, t2 where i = k order by j + k option (recompile)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Enjoy!</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Stefano</P><img src="http://blogs.msdn.com/aggbug.aspx?PostID=674350" width="1" height="1">]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">If you read the Books Online page describing the UPDATE STATISTICS command, you will see that there are some undocumented options.<span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"> </span></p>


<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">UPDATE STATISTICS table | view
[
{
{ index | statistics_name }
| ( { index |statistics_name } [ ,...n ] )
}
]
[    WITH
[
[ FULLSCAN ]
| SAMPLE number { PERCENT | ROWS } ]
| RESAMPLE
| &lt;update_stats_stream_option&gt; [ ,...n ]
]
[ [ , ] [ ALL | COLUMNS | INDEX ]
[ [ , ] NORECOMPUTE ]
] ;

</span><strong><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'">&lt;update_stats_stream_option&gt; ::=</span></strong><strong><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'">
<strong><span style="FONT-FAMILY: 'Courier New'"> [ STATS_STREAM = stats_stream ]</span></strong>
<strong><span style="FONT-FAMILY: 'Courier New'"> [ ROWCOUNT = numeric_constant ]</span></strong>
<strong><span style="FONT-FAMILY: 'Courier New'"> [ PAGECOUNT = numeric contant ]</span></strong></span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><strong><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'">&lt;update_stats_stream_option&gt; </span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><strong><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'"> This syntax is for internal use only and is not supported. Microsoft reserves the right to change this syntax at any time.</span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">There is a very good reason why these options are undocumented. They are meant for testing and debugging purposes, and should never ever be used on production systems.</p>
<span id="more-201"></span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">However, these options can also be extremely helpful to create examples and sample scripts to demonstrate various features and behaviors of the Query Optimizer and the related query plan shapes. We decided to explain what ROWCOUNT and PAGECOUNT do, so that to be able to use these commands in examples we’ll be posting in some future. Feel free to use these options on development systems for experimental and educational purposes, but please do not play with fire and do not use them in production!</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">As the name of these options suggest, ROWCOUNT and PAGECOUNT alter the internal metadata of the specified table or index by overriding the counters containing the row and page counts of the object. These counters are in turn read by the Query Optimizer when processing queries that access the table and/or index in question. These commands can basically cheat the Optimizer into thinking that a table or index is extremely large.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">SQL Server’s Query Optimization process is structured in multiple stages. Further optimization stages consider a progressively larger and more sophisticated set of possible tree transformations and query optimizations. Later stages of optimization are only entered when the estimated cost of the query is sufficiently high, in order to avoid wasting precious CPU cycles against simple queries that do not need that level of sophistication anyway. The multiple optimization stages are a mean to produce efficient query plans without consuming excessive amounts of CPU. Typically, in order to make the Optimizer “think” a lot and enter these later stages it is necessary to have big tables with a large number of rows and pages, which in turn take time and space to populate. Using ROWCOUNT and PAGECOUNT allows us to exercise these code paths with relatively simple scripts that do not require an extremely complex setup phase.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Here is an example. When running this simple script on your SQL 2005 instance you will likely see a different query plan for the two selects before and after updating the statistics. The recompile option is used to ensure that the query plans are regenerated. From the statistics profile, you’ll also see very different estimated row counts and consequently costs.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">use tempdb</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table t1(i int, j int)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table t2(h int, k int)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">set statistics profile on</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select distinct(i) from t1</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select * from t1, t2 where i = k order by j + k</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">update statistics t1 with rowcount = 10000, pagecount = 10000</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">update statistics t2 with rowcount = 100000, pagecount = 100000</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select distinct(i) from t1 option (recompile)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">select * from t1, t2 where i = k order by j + k option (recompile)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Enjoy!</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Stefano</p>
<p>
Source : http://blogs.msdn.com/queryoptteam/default.aspx
</p>]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2006/07/update-statistics-undocumented-options/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hints for DML queries</title>
		<link>http://blogmyquery.com/index.php/2006/07/hints-for-dml-queries/</link>
		<comments>http://blogmyquery.com/index.php/2006/07/hints-for-dml-queries/#comments</comments>
		<pubDate>Fri, 14 Jul 2006 23:30:00 +0000</pubDate>
		<dc:creator>QueryOptTeam</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Hints for DML queries]]></category>

		<guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:666196</guid>
		<description><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Not everyone knows that&#160;query level hints (like loop join) will impact the entirety of a DML query plan. This includes foreign key validation and indexed view maintenance.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Let us look at an example with two tables involved in a foreign key constraint.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">use tempdb<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table department(deptid int primary key clustered, deptname varchar(10))<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table employee(empid int primary key clustered, empname varchar(10), deptid int references department(deptid))<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">insert department values(1, 'Optimizer')<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">At first glance, it might seem useless to provide a join hint for a scalar insert - like when inserting a row to the employee table – because the query contains no joins. However, this can make sense in presence of foreign key validations, because the Optimizer will automatically augment the query plan with a join for the purpose of validating the constraint. For example, this insert statement here</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">insert employee select 1 empid, 'Stefano' empname, 1 deptid </SPAN><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">option (merge join)</SPAN></B><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">will produce a plan with a merge join between the employee and department tables. The join will enforce that the value of the deptid column actually exists in the primary table, department. The “Assert” operator will raise an error if a matching row is not found.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160; </SPAN>&#124;--Assert<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</SPAN><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">Merge Join</SPAN></B><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Insert(employee)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Scan(department)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Unfortunately, this technique is restricted to only query (vs. table) level hints, so&#160;it's not possible for example&#160;to force the indexes being used when accessing the other table involved in the constraint (department in the example). Also, TSQL syntax does not allow specifying query level hints for scalar inserts, like "insert table&#160;values...", but the easy workaround is to rewrite the statement&#160;as "insert select" like in the example. Update and delete statements do regularly accept query level hints.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">update employee set empname = 'Conor', deptid = 2 where empid = 1 </SPAN><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">option (loop join)</SPAN></B><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160; </SPAN>&#124;--Assert<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</SPAN><B><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">Nested Loops</SPAN></B><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Update(employee)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Seek(department)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break"></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Let us now look at slightly more complex example with an indexed view.<o:p></o:p></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><BR><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">use tempdb<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">go<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">SET ANSI_NULLS ON<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">SET ANSI_PADDING ON<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">SET ANSI_WARNINGS ON<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">SET CONCAT_NULL_YIELDS_NULL ON<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">SET NUMERIC_ROUNDABORT OFF<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">SET QUOTED_IDENTIFIER ON<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">go<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;</SPAN><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">create table t1(i int, j int)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">go<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">create table t2(h int, k int)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">go<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">create view v with schemabinding as<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">select i, h, count_big(*) c from dbo.t1, dbo.t2<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">where j = k<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">group by i, h<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">go<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">create unique clustered index v_ih on v(i, h)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New">go<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">The changes to either table t1 or t2 need to be propagated to the indexed view v1, in order to keep it consistent at all times. Since the indexed view contains a join and an aggregation in its definition, the Optimizer will automatically augment DML query plans against t1 or t2 with joins and aggregations. Query level hints can be used to influence the join and/or grouping strategy employed by the Optimizer in the query plan.<SPAN style="mso-bidi-font-size: 10.0pt"><o:p></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000>insert into t1 select 1, 2 </FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">option (hash join, hash group)</SPAN></B><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160; </SPAN>&#124;--Sequence<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Table Spool<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Table Insert(t1)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Update(v)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Collapse<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Sort<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Compute Scalar<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Hash Match(Right Outer Join)</SPAN></B><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN>&#124;--Clustered Index Scan(v)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Hash Match(Aggregate)</SPAN></B><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Hash Match(Inner Join)</SPAN></B><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Table Spool<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</SPAN>&#124;--Table Scan(t2)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000>insert into t1 select 1, 2 </FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">option (loop join, order group)</SPAN></B><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p><FONT face="Courier New" color=#800000 size=2>&#160;</FONT></o:p></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160; </SPAN>&#124;--Sequence<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Table Spool<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Table Insert(t1)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Update(v)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Collapse<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Sort<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Compute Scalar<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Nested Loops(Left Outer Join)</SPAN></B><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Stream Aggregate</SPAN></B><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160; </SPAN>&#124;--Sort<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><FONT size=2><FONT face="Courier New"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT color=#800000><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--</FONT></SPAN><B><SPAN style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Nested Loops(Inner Join)<o:p></o:p></SPAN></B></FONT></FONT></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Table Spool<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;</SPAN>&#124;<SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Table Scan(t2)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=NormalCourierNew style="MARGIN: 0in 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><FONT size=2><FONT color=#800000><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </SPAN>&#124;--Clustered Index Seek(v)<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face="Courier New" color=#800000 size=2></FONT>&#160;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Needless to say, query hints are fully documented in Books Online – look for the “Query Hint (Transact-SQL)” topic.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&#160;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Ciao,<o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Stefano</P><img src="http://blogs.msdn.com/aggbug.aspx?PostID=666196" width="1" height="1">]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Not everyone knows that query level hints (like loop join) will impact the entirety of a DML query plan. This includes foreign key validation and indexed view maintenance.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Let us look at an example with two tables involved in a foreign key constraint.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">use tempdb</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table department(deptid int primary key clustered, deptname varchar(10))</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">create table employee(empid int primary key clustered, empname varchar(10), deptid int references department(deptid))</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">insert department values(1, 'Optimizer')</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span id="more-202"></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">At first glance, it might seem useless to provide a join hint for a scalar insert - like when inserting a row to the employee table – because the query contains no joins. However, this can make sense in presence of foreign key validations, because the Optimizer will automatically augment the query plan with a join for the purpose of validating the constraint. For example, this insert statement here</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">insert employee select 1 empid, 'Stefano' empname, 1 deptid </span><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">option (merge join)</span></strong><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">will produce a plan with a merge join between the employee and department tables. The join will enforce that the value of the deptid column actually exists in the primary table, department. The “Assert” operator will raise an error if a matching row is not found.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--Assert</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--</span><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">Merge Join</span></strong><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--Clustered Index Insert(employee)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--Clustered Index Scan(department)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Unfortunately, this technique is restricted to only query (vs. table) level hints, so it's not possible for example to force the indexes being used when accessing the other table involved in the constraint (department in the example). Also, TSQL syntax does not allow specifying query level hints for scalar inserts, like "insert table values...", but the easy workaround is to rewrite the statement as "insert select" like in the example. Update and delete statements do regularly accept query level hints.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">update employee set empname = 'Conor', deptid = 2 where empid = 1 </span><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">option (loop join)</span></strong><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--Assert</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--</span><strong><span style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">Nested Loops</span></strong><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--Clustered Index Update(employee)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"><span style="mso-spacerun: yes"> </span>|--Clustered Index Seek(department)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><br style="mso-special-character: line-break" /><br style="mso-special-character: line-break" /></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Let us now look at slightly more complex example with an indexed view.</p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">use tempdb</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">go</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">SET ANSI_NULLS ON</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">SET ANSI_PADDING ON</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">SET ANSI_WARNINGS ON</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">SET CONCAT_NULL_YIELDS_NULL ON</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">SET NUMERIC_ROUNDABORT OFF</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">SET QUOTED_IDENTIFIER ON</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">go</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span></span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">create table t1(i int, j int)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">go</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">create table t2(h int, k int)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">go</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">create view v with schemabinding as</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">select i, h, count_big(*) c from dbo.t1, dbo.t2</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">where j = k</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">group by i, h</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">go</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">create unique clustered index v_ih on v(i, h)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;">go</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">The changes to either table t1 or t2 need to be propagated to the indexed view v1, in order to keep it consistent at all times. Since the indexed view contains a join and an aggregation in its definition, the Optimizer will automatically augment DML query plans against t1 or t2 with joins and aggregations. Query level hints can be used to influence the join and/or grouping strategy employed by the Optimizer in the query plan.<span style="mso-bidi-font-size: 10.0pt"> </span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;">insert into t1 select 1, 2 </span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">option (hash join, hash group)</span></strong><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"> </span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Sequence</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Table Spool</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Table Insert(t1)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Clustered Index Update(v)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Collapse</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Sort</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Compute Scalar</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;"><span style="mso-spacerun: yes"> </span>|--</span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Hash Match(Right Outer Join)</span></strong><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"> </span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>|--Clustered Index Scan(v)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;"><span style="mso-spacerun: yes"> </span>|--</span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Hash Match(Aggregate)</span></strong><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"> </span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;"><span style="mso-spacerun: yes"> </span>|--</span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Hash Match(Inner Join)</span></strong><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"> </span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Table Spool</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>|--Table Scan(t2)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;">insert into t1 select 1, 2 </span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">option (loop join, order group)</span></strong><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"> </span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Sequence</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Table Spool</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Table Insert(t1)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Clustered Index Update(v)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Collapse</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Sort</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Compute Scalar</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;"><span style="mso-spacerun: yes"> </span>|--</span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Nested Loops(Left Outer Join)</span></strong><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"> </span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;"><span style="mso-spacerun: yes"> </span>|--</span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Stream Aggregate</span></strong><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"> </span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Sort</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="font-size: x-small;"><span style="font-family: Courier New;"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="color: #800000;"><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--</span></span><strong><span style="COLOR: red; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'">Nested Loops(Inner Join)</span></strong></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Table Spool</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>|<span style="mso-spacerun: yes"> </span>|--Table Scan(t2)</span></span></span></span></p>
<p class="NormalCourierNew" style="MARGIN: 0in 0in 0pt"><span style="mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Courier New'"><span style="font-size: x-small;"><span style="color: #800000;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes"> </span>|--Clustered Index Seek(v)</span></span></span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Courier New; color: #800000; font-size: x-small;"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Needless to say, query hints are fully documented in Books Online – look for the “Query Hint (Transact-SQL)” topic.</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Ciao,</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">Stefano</p>
Source : http://blogs.msdn.com/queryoptteam/default.aspx]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2006/07/hints-for-dml-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Non updating updates</title>
		<link>http://blogmyquery.com/index.php/2006/07/non-updating-updates/</link>
		<comments>http://blogmyquery.com/index.php/2006/07/non-updating-updates/#comments</comments>
		<pubDate>Fri, 07 Jul 2006 21:45:00 +0000</pubDate>
		<dc:creator>QueryOptTeam</dc:creator>
				<category><![CDATA[Sqlserver]]></category>

		<guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:659453</guid>
		<description><![CDATA[<FONT size=2></FONT>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Tahoma size=2>A question we are frequently asked is what happens when an update statement assigns a column to its same current value. For example,</FONT> <FONT face=Tahoma><FONT size=2></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><FONT size=3></FONT></o:p>&#160;</P></FONT></FONT>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">use tempdb<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">create table t(i int, cc as i + 1)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">create index t_cc on t(cc)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">insert into t values(1)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">update t set i = 1<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"><o:p>&#160;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">update t set i = i<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</SPAN><SPAN style="COLOR: maroon"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT size=3>&#160;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>Columns do get updated even if the value does not change. However, to be honest, i wouldn't worry too much about it.<BR>The runtime cost for updating a row is roughly equal to</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>a) locating the row in the heap or B-Tree</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>b) locking the row</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>c) changing the updated column values</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>d) logging the change.<BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break"></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Tahoma size=2>For updates to columns that fit in the 8Kb page, i don't think that avoiding part of the copy would make too much of a difference. Realistically, performing a comparison to tell what changed with memcmp would roughly cost as much as changing the value with memcpy. And this cost would surely be overshadowed by a) + b) + d). Updates to columns that don't fit in the 8Kb page are inherently slower, and i think it is up to the application to avoid making unnecessary changes to these.<BR><BR>Now a), b), c) and d) need to be performed for every index carrying one or more columns being modified. So a major factor for the runtime cost of an update query is the number of nonclustered indexes that need to be maintained. In SQL 2005 we introduced an optimization to skip nonclustered index maintenance if none of the updated columns stored in the index actually changed. The reasoning is that running the comparison for only the modified columns stored in one or more nonclustered indexes introduces a runtime overhead that is very small, and surely significantly lower than a), b) and d). Given that columns stored in an index are typically pretty small in size, it is very easy to break even if the optimization actually saves a small minority of nonclustered index row updates. The optimization does not apply to the clustered index, as we want to ensure to always exclusively lock the affected rows even if no columns are really changing.<BR><BR>You can see the new optimization in action by comparing the statistics profile output for one of the update statements in the previous example in SQL 2000 and 2005 (see attached image).</FONT><BR><BR></FONT><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">set statistics profile on<BR>go<BR><BR></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">update t set i = i<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</SPAN><BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break"></P>
<DIV><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><FONT face=Tahoma size=2>In SQL 2000, the lack of the optimization leads to updating the nonclustered index even if the value is not changing.</FONT></SPAN></DIV>
<DIV><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><FONT face=Tahoma size=2>&#160;</FONT>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>In the SQL 2005 plan, it is possible to appreciate</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>- a “Compute Scalar” operator that compares the current value and new value of the column being modified</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>- a new filter operator that on a row by row basis will determine whether the nonclustered index is being affected or not</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2>- the fact that nonclustered index maintenance is now bypassed</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Tahoma size=2></FONT>&#160;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><FONT face=Tahoma size=2>This new SQL 2005 optimization allows to better address the problem of maintaining a diverse set of columns across multiple recurring update statements against a certain table. In SQL 2000 there was a tradeoff between building dynamic SQL statements and possibly incur in frequent compilations, to only maintain the modified columns vs. using a parameterized and standard statement that updated all the columns, but would also maintain all the nonclustered indexes all the time. Like i said before, special precautions should still be taken for columns that don't fit the 8Kb page limit.</FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><FONT face=Tahoma size=2></FONT></SPAN>&#160;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><FONT face=Tahoma size=2>Ciao,</FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><FONT face=Tahoma size=2>Stefano</FONT></SPAN></P></SPAN></SPAN></DIV><img src="http://blogs.msdn.com/aggbug.aspx?PostID=659453" width="1" height="1">]]></description>
			<content:encoded><![CDATA[<span style="font-size: x-small;"> </span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-size: small;"><span style="font-family: Tahoma; font-size: x-small;">A question we are frequently asked is what happens when an update statement assigns a column to its same current value. For example,</span> <span style="font-family: Tahoma;"><span style="font-size: x-small;"> </span></span></span></p>
<span style="font-size: small;"><span style="font-family: Tahoma;">
</span></span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-size: small;"><span style="font-family: Tahoma;"><span style="font-size: small;"> </span></span></span><span style="font-size: small;"><span style="font-family: Tahoma;"> </span></span></p>
<span style="font-size: small;"><span style="font-family: Tahoma;"> </span></span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">use tempdb</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">create table t(i int, cc as i + 1)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">create index t_cc on t(cc)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">insert into t values(1)</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">update t set i = 1</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">update t set i = i</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</span><span style="COLOR: maroon"></span></p>
<span id="more-203"></span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-size: small;"> </span></p>


<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">Columns do get updated even if the value does not change. However, to be honest, i wouldn't worry too much about it.
The runtime cost for updating a row is roughly equal to</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">a) locating the row in the heap or B-Tree</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">b) locking the row</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">c) changing the updated column values</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">d) logging the change.<br style="mso-special-character: line-break" /><br style="mso-special-character: line-break" /></span></p>


<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-size: small;"><span style="font-family: Tahoma; font-size: x-small;">For updates to columns that fit in the 8Kb page, i don't think that avoiding part of the copy would make too much of a difference. Realistically, performing a comparison to tell what changed with memcmp would roughly cost as much as changing the value with memcpy. And this cost would surely be overshadowed by a) + b) + d). Updates to columns that don't fit in the 8Kb page are inherently slower, and i think it is up to the application to avoid making unnecessary changes to these.

Now a), b), c) and d) need to be performed for every index carrying one or more columns being modified. So a major factor for the runtime cost of an update query is the number of nonclustered indexes that need to be maintained. In SQL 2005 we introduced an optimization to skip nonclustered index maintenance if none of the updated columns stored in the index actually changed. The reasoning is that running the comparison for only the modified columns stored in one or more nonclustered indexes introduces a runtime overhead that is very small, and surely significantly lower than a), b) and d). Given that columns stored in an index are typically pretty small in size, it is very easy to break even if the optimization actually saves a small minority of nonclustered index row updates. The optimization does not apply to the clustered index, as we want to ensure to always exclusively lock the affected rows even if no columns are really changing.

You can see the new optimization in action by comparing the statistics profile output for one of the update statements in the previous example in SQL 2000 and 2005 (see attached image).</span>

</span><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'">set statistics profile on
go

</span><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">update t set i = i</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt; COLOR: maroon; FONT-FAMILY: 'Courier New'">go</span><br style="mso-special-character: line-break" /><br style="mso-special-character: line-break" /></p>

<div><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span style="font-family: Tahoma; font-size: x-small;">In SQL 2000, the lack of the optimization leads to updating the nonclustered index even if the value is not changing.</span></span></div>
<div><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span style="font-family: Tahoma; font-size: x-small;"> </span>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">In the SQL 2005 plan, it is possible to appreciate</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">- a “Compute Scalar” operator that compares the current value and new value of the column being modified</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">- a new filter operator that on a row by row basis will determine whether the nonclustered index is being affected or not</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;">- the fact that nonclustered index maintenance is now bypassed</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="font-family: Tahoma; font-size: x-small;"> </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span style="font-family: Tahoma; font-size: x-small;">This new SQL 2005 optimization allows to better address the problem of maintaining a diverse set of columns across multiple recurring update statements against a certain table. In SQL 2000 there was a tradeoff between building dynamic SQL statements and possibly incur in frequent compilations, to only maintain the modified columns vs. using a parameterized and standard statement that updated all the columns, but would also maintain all the nonclustered indexes all the time. Like i said before, special precautions should still be taken for columns that don't fit the 8Kb page limit.</span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span style="font-family: Tahoma; font-size: x-small;"> </span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span style="font-family: Tahoma; font-size: x-small;">Ciao,</span></span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span style="font-family: Tahoma; font-size: x-small;">Stefano</span></span></p>

</span></span></div>
Source : http://blogs.msdn.com/queryoptteam/default.aspx]]></content:encoded>
			<wfw:commentRss>http://blogmyquery.com/index.php/2006/07/non-updating-updates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
