<?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>tim laqua dot com &#187; merge join</title>
	<atom:link href="http://timlaqua.com/tag/merge-join/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Sun, 09 May 2010 15:25:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Quick Analysis of Cached Query Plans in SQL Server 2005</title>
		<link>http://timlaqua.com/2009/05/quick-analysis-of-cached-query-plans-in-sql-server-2005/</link>
		<comments>http://timlaqua.com/2009/05/quick-analysis-of-cached-query-plans-in-sql-server-2005/#comments</comments>
		<pubDate>Fri, 15 May 2009 21:55:03 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[hash match]]></category>
		<category><![CDATA[merge join]]></category>
		<category><![CDATA[nested loop]]></category>
		<category><![CDATA[query plan]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=207</guid>
		<description><![CDATA[I run this one pretty frequently when we need to figure out what procs are killing a complex ETL process and what exactly about them is making the server cry. So basically, if it's on the development server, I'll do a DBCC FREEPROCCACHE and a DBCC DROPCLEANBUFFERS, run the entire set of ETLs, then run [...]]]></description>
			<content:encoded><![CDATA[<p>I run this one pretty frequently when we need to figure out what procs are killing a complex ETL process and what exactly about them is making the server cry.  So basically, if it's on the development server, I'll do a <code>DBCC FREEPROCCACHE</code> and a <code>DBCC DROPCLEANBUFFERS</code>, run the entire set of ETLs, then run this query and then dig deeper in to the query plans that look suspect (high *Scan counts usually, sometimes lots of Hash Matches or Merge Joins).  On a production server, the clearing of the proc cache and dropcleanbuffers can be problematic so I'll often just run the query after a scheduled ETL run.  If you want to see the query plans mapped out visually, click on the query_plan value and SSMS will open up the XML.  Then save that XML file as a .sqlplan file.  Once you have that, close the XML and then open the .sqlplan file.<span id="more-207"></span></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
	 DatabaseName
	,ObjectName
	,<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		sum((/QP:ShowPlanXML/QP:BatchSequence/QP:Batch/QP:Statements/QP:StmtSimple/QP:QueryPlan/@CompileTime))'</span>,<span style="color: #FF0000;">'NVARCHAR(MAX)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>CompileTime<span style="color: #808080;">&#93;</span>
	,query_plan
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:ColumnsWithNoStatistics))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>ColumnsWithNoStatistics<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:TableScan))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>TableScan<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:IndexScan))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>IndexScan<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:RelOp[@PhysicalOp=&quot;Nested Loops&quot;]))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>NestedLoop<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:RelOp[@PhysicalOp=&quot;Hash Match&quot;]))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>HashMatch<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:RelOp[@PhysicalOp=&quot;Merge Join&quot;]))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>MergeJoin<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> 
	<span style="color: #808080;">&#40;</span>	
		<span style="color: #0000FF;">SELECT</span>
			<span style="color: #FF00FF;">OBJECT_NAME</span><span style="color: #808080;">&#40;</span>st.<span style="color: #202020;">objectid</span>,st.<span style="color: #202020;">dbid</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> ObjectName,
			<span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>st.<span style="color: #202020;">dbid</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> DatabaseName,
			st.<span style="color: #202020;">dbid</span>,
			st.<span style="color: #202020;">objectid</span>,
			qs.<span style="color: #202020;">plan_handle</span>
		<span style="color: #0000FF;">FROM</span> 
			sys.<span style="color: #202020;">dm_exec_query_stats</span> <span style="color: #0000FF;">AS</span> qs
			<span style="color: #808080;">CROSS</span> APPLY sys.<span style="color: #202020;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>qs.<span style="color: #202020;">sql_handle</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> st
		<span style="color: #0000FF;">WHERE</span>
			ObjectId <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
		<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span>
			st.<span style="color: #202020;">dbid</span>,
			st.<span style="color: #202020;">objectid</span>,
			qs.<span style="color: #202020;">plan_handle</span>
	<span style="color: #808080;">&#41;</span> a
	<span style="color: #808080;">CROSS</span> APPLY sys.<span style="color: #202020;">dm_exec_query_plan</span><span style="color: #808080;">&#40;</span>plan_handle<span style="color: #808080;">&#41;</span> qp
<span style="color: #0000FF;">WHERE</span> DatabaseName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'NameOfDatabase'</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> DatabaseName, ObjectName</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/05/quick-analysis-of-cached-query-plans-in-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
