<?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; How to Read Statistics Profile</title>
	<atom:link href="http://blogmyquery.com/index.php/tag/how-to-read-statistics-profile/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogmyquery.com</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 14:15:11 +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>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>
	</channel>
</rss>

