<?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>SQLWorkshops.com Blog &#187; Performance</title>
	<atom:link href="http://blog.sqlworkshops.com/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlworkshops.com</link>
	<description>Microsoft SQL Server Performance Monitoring &#38; Tuning</description>
	<lastBuildDate>Mon, 16 May 2016 15:05:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Plan Caching and Query Memory Part 2: When not to use stored procedure or other plan caching mechanisms like sp_executesql or prepared statement</title>
		<link>http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-2-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement/</link>
		<comments>http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-2-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 15:45:18 +0000</pubDate>
		<dc:creator>R Meyyappan</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://blog.sqlworkshops.com/?p=94</guid>
		<description><![CDATA[SQL Server estimates Memory requirement at compile time, when stored procedure or other plan caching mechanisms like sp_executesql or prepared statement are used, the memory requirement is estimated based on first set of execution parameters. This is a common reason &#8230; <a href="http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-2-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><span class="style18">SQL Server estimates Memory requirement at compile time, when stored procedure or other plan caching mechanisms like sp_executesql or prepared statement are used, the memory requirement is estimated based on first set of execution parameters. This is a common reason for spill over tempdb and hence poor performance. Common memory allocating queries are that perform Sort and do Hash Match operations like Hash Join or Hash Aggregation or Hash Union. This article covers Hash Match operations with examples. It is recommended to read <strong><a href="http://www.sqlworkshops.com/plancachingandquerymemory.htm">Plan Caching and Query Memory Part I</a></strong> before this article which covers an introduction and Query memory for Sort. In most cases it is cheaper to pay for the compilation cost of dynamic queries than huge cost for spill over tempdb, unless memory requirement for a query does not change significantly based on predicates.</span></p>
<p><span class="style18">This article covers underestimation / overestimation of memory for Hash Match operation. <strong><a href="http://www.sqlworkshops.com/plancachingandquerymemory.htm">Plan Caching and Query Memory Part I</a></strong> covers underestimation / overestimation for Sort. It is important to note that underestimation of memory for Sort and Hash Match operations lead to spill over tempdb and hence negatively impact performance. Overestimation of memory affects the memory needs of other concurrently executing queries. <strong>In addition, it is important to note, with Hash Match operations, overestimation of memory can actually lead to poor performance.</strong></span></p>
<p><span class="style18">To read additional articles I wrote click <strong><strong><a href="http://www.sqlworkshops.com/articles.htm">here</a></strong></strong>. To watch the webcasts click <strong><strong><a href="http://www.sqlworkshops.com/webcasts">here</a></strong></strong>.</span></p>
<p><span class="style18"><strong>The best way to learn is to practice. To create the below tables and reproduce the behavior, join the mailing list by using this link: <span style="color: #03664b;"><span style="text-decoration: underline;"><span style="text-decoration: underline;"><span class="style18"><a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a></span></span></span></span> and I will send you the table creation script. Most of these concepts are also covered in our webcasts: <span style="text-decoration: underline;"><a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a></span></strong></span></p>
<p>Let’s create a Customer’s State table that has 99% of customers in NY and the rest 1% in WA.Customers table used in Part I of this article is also used here.To observe Hash Warning, enable &#8216;Hash Warning&#8217; in SQL Profiler under Events &#8216;Errors and Warnings&#8217;.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">drop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">table</span> CustomersState</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">create</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">table</span> CustomersState<span style="color: gray;">(</span>CustomerID <span style="color: blue;">int</span> <span style="color: blue;">primary</span> <span style="color: blue;">key</span><span style="color: gray;">,</span> <span style="color: blue;">Address</span> <span style="color: blue;">char</span><span style="color: gray;">(</span>200<span style="color: gray;">),</span> <span style="color: blue;">State</span> <span style="color: blue;">char</span><span style="color: gray;">(</span>2<span style="color: gray;">))</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">insert</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> CustomersState<span style="color: gray;">(</span>CustomerID<span style="color: gray;">,</span> <span style="color: blue;">Address</span><span style="color: gray;">)</span> <span style="color: blue;">select</span> CustomerID<span style="color: gray;">,</span> <span style="color: red;">&#8216;Address&#8217;</span> <span style="color: blue;">from</span> Customers</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">update</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersState <span style="color: blue;">set</span> <span style="color: blue;">State</span> <span style="color: gray;">=</span> <span style="color: red;">&#8216;NY&#8217;</span> <span style="color: blue;">where</span> CustomerID <span style="color: gray;">%</span> 100 <span style="color: gray;">!=</span> 1</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">update</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersState <span style="color: blue;">set</span> <span style="color: blue;">State</span> <span style="color: gray;">=</span> <span style="color: red;">&#8216;WA&#8217;</span> <span style="color: blue;">where</span> CustomerID <span style="color: gray;">%</span> 100 <span style="color: gray;">=</span> 1</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">update</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> CustomersState <span style="color: blue;">with</span> <span style="color: blue;">fullscan</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>Let’s create a stored procedure that joins customers with CustomersState table with a predicate on State.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">create</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByState @State <span style="color: blue;">char</span><span style="color: gray;">(</span>2<span style="color: gray;">)</span> <span style="color: blue;">as</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">begin</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @CustomerID <span style="color: blue;">int</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @CustomerID <span style="color: gray;">=</span> e<span style="color: gray;">.</span>CustomerID <span style="color: blue;">from</span> Customers e</span></p>
<p class="style18"><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">inner</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: gray;">join</span> CustomersState es <span style="color: blue;">on </span><span style="color: gray;">(</span>e<span style="color: gray;">.</span>CustomerID <span style="color: gray;">=</span> es<span style="color: gray;">.</span>CustomerID<span style="color: gray;">)</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> es<span style="color: gray;">.</span><span style="color: blue;">State</span> <span style="color: gray;">=</span> @State</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> 1<span style="color: gray;">)</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">end</span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>Let’s execute the stored procedure first with parameter value ‘WA’ – which will select 1% of data.</p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;WA&#8217;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>The stored procedure took 294 ms to complete.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h1.jpg" alt="" width="740" height="500" /></p>
<p>The stored procedure was granted 6704 KB based on 8000 rows being estimated.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h2.jpg" alt="" width="740" height="500" /></p>
<p>The estimated number of rows, 8000 is similar to actual number of rows 8000 and hence the memory estimation should be ok.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h3.jpg" alt="" width="740" height="500" /></p>
<p>There was no Hash Warning in SQL Profiler.To observe Hash Warning, enable &#8216;Hash Warning&#8217; in SQL Profiler under Events &#8216;Errors and Warnings&#8217;.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/hp1.jpg" alt="" width="740" height="500" /></p>
<p>Now let’s execute the stored procedure with parameter value ‘NY’ – which will select 99% of data.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">-Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;NY&#8217;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>The stored procedure took 2922 ms to complete.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h4.jpg" alt="" width="740" height="500" /></p>
<p>The stored procedure was granted 6704 KB based on 8000 rows being estimated.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h5.jpg" alt="" width="740" height="500" /></p>
<p>The estimated number of rows, 8000 is way different from the actual number of rows 792000 because the estimation is based on the first set of parameter value supplied to the stored procedure which is ‘WA’ in our case. This underestimation will lead to spill over tempdb, resulting in poor performance.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h6.jpg" alt="" width="740" height="500" /></p>
<p>There was Hash Warning (Recursion) in SQL Profiler.To observe Hash Warning, enable &#8216;Hash Warning&#8217; in SQL Profiler under Events &#8216;Errors and Warnings&#8217;.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/hp2.jpg" alt="" width="740" height="500" /></p>
<p>Let’s recompile the stored procedure and then let’s first execute the stored procedure with parameter value ‘NY’.<br />
In a production instance it is not advisable to use sp_recompile instead one should use DBCC FREEPROCCACHE (plan_handle). This is due to locking issues involved with sp_recompile, refer to our webcasts, <span class="style18"><span style="text-decoration: underline;"><strong><a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a></strong></span></span> for further details.</p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: maroon;">sp_recompile</span>CustomersByState</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;NY&#8217;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>Now the stored procedure took only 1046 ms instead of 2922 ms.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h7.jpg" alt="" width="740" height="500" /></p>
<p>The stored procedure was granted 146752 KB of memory.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h8.jpg" alt="" width="740" height="500" /></p>
<p>The estimated number of rows, 792000 is similar to actual number of rows of 792000. Better performance of this stored procedure execution is due to better estimation of memory and avoiding spill over tempdb.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h9.jpg" alt="" width="740" height="500" /></p>
<p>There was no Hash Warning in SQL Profiler.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/hp1.jpg" alt="" width="740" height="500" /></p>
<p>Now let’s execute the stored procedure with parameter value ‘WA’.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;WA&#8217;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>The stored procedure took 351 ms to complete, higher than the previous execution time of 294 ms.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h10.jpg" alt="" width="740" height="500" /></p>
<p>This stored procedure was granted more memory (146752 KB) than necessary (6704 KB) based on parameter value ‘NY’ for estimation (792000 rows) instead of parameter value ‘WA’ for estimation (8000 rows). This is because the estimation is based on the first set of parameter value supplied to the stored procedure which is ‘NY’ in this case. This overestimation leads to poor performance of this Hash Match operation, it might also affect the performance of other concurrently executing queries requiring memory and hence overestimation is not recommended.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h11.jpg" alt="" width="740" height="500" /></p>
<p>The estimated number of rows, 792000 is much more than the actual number of rows of 8000.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h12.jpg" alt="" width="740" height="500" /></p>
<p><strong>Intermediate Summary:</strong> This issue can be avoided by not caching the plan for memory allocating queries. Other possibility is to use recompile hint or optimize for hint to allocate memory for predefined data range.Let’s recreate the stored procedure with recompile hint.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">drop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByState</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">create</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByState @State <span style="color: blue;">char</span><span style="color: gray;">(</span>2<span style="color: gray;">)</span> <span style="color: blue;">as</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">begin</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @CustomerID <span style="color: blue;">int</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @CustomerID <span style="color: gray;">=</span> e<span style="color: gray;">.</span>CustomerID <span style="color: blue;">from</span> Customers e</span></p>
<p class="style18"><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">inner</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: gray;">join</span> CustomersState es <span style="color: blue;">on </span><span style="color: gray;">(</span>e<span style="color: gray;">.</span>CustomerID <span style="color: gray;">=</span> es<span style="color: gray;">.</span>CustomerID<span style="color: gray;">)</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> es<span style="color: gray;">.</span><span style="color: blue;">State</span> <span style="color: gray;">=</span> @State</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> 1<span style="color: gray;">,</span> <span style="color: blue;">recompile</span><span style="color: gray;">)</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">end</span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>Let’s execute the stored procedure initially with parameter value ‘WA’ and then with parameter value ‘NY’.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;WA&#8217;</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;NY&#8217;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>The stored procedure took 297 ms and 1102 ms in line with previous optimal execution times.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h13.jpg" alt="" width="740" height="500" /></p>
<p>The stored procedure with parameter value ‘WA’ has good estimation like before.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h14.jpg" alt="" width="740" height="500" /></p>
<p>Estimated number of rows of 8000 is similar to actual number of rows of 8000.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h15.jpg" alt="" width="740" height="500" /></p>
<p>The stored procedure with parameter value ‘NY’ also has good estimation and memory grant like before because the stored procedure was recompiled with current set of parameter values.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h16.jpg" alt="" width="740" height="500" /></p>
<p>Estimated number of rows of 792000 is similar to actual number of rows of 792000.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h17.jpg" alt="" width="740" height="500" /></p>
<p>The compilation time and compilation CPU of 1 ms is not expensive in this case compared to the performance benefit.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h18.jpg" alt="" width="740" height="500" /></p>
<p>There was no Hash Warning in SQL Profiler.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/hp1.jpg" alt="" width="740" height="500" /></p>
<p>Let’s recreate the stored procedure with optimize for hint of ‘NY’.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">drop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByState</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">create</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByState @State <span style="color: blue;">char</span><span style="color: gray;">(</span>2<span style="color: gray;">)</span> <span style="color: blue;">as</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">begin</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @CustomerID <span style="color: blue;">int</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @CustomerID <span style="color: gray;">=</span> e<span style="color: gray;">.</span>CustomerID <span style="color: blue;">from</span> Customers e</span></p>
<p class="style18"><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">inner</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: gray;">join</span> CustomersState es <span style="color: blue;">on </span><span style="color: gray;">(</span>e<span style="color: gray;">.</span>CustomerID <span style="color: gray;">=</span> es<span style="color: gray;">.</span>CustomerID<span style="color: gray;">)</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> es<span style="color: gray;">.</span><span style="color: blue;">State</span> <span style="color: gray;">=</span> @State</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> 1<span style="color: gray;">,</span> optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@State <span style="color: gray;">=</span> <span style="color: red;">&#8216;NY&#8217;</span><span style="color: gray;">))</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">end</span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>Let’s execute the stored procedure initially with parameter value ‘WA’ and then with parameter value ‘NY’.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;WA&#8217;</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByState<span style="color: red;">&#8216;NY&#8217;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p>The stored procedure took 353 ms with parameter value ‘WA’, this is much slower than the optimal execution time of 294 ms we observed previously. This is because of overestimation of memory. The stored procedure with parameter value ‘NY’ has optimal execution time like before.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h19.jpg" alt="" width="740" height="500" /></p>
<p>The stored procedure with parameter value ‘WA’ has overestimation of rows because of optimize for hint value of ‘NY’.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h21.jpg" alt="" width="740" height="500" /></p>
<p>Unlike before, more memory was estimated to this stored procedure based on optimize for hint value ‘NY’.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h20.jpg" alt="" width="740" height="500" /></p>
<p>The stored procedure with parameter value ‘NY’ has good estimation because of optimize for hint value of ‘NY’. Estimated number of rows of 792000 is similar to actual number of rows of 792000.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h23.jpg" alt="" width="740" height="500" /></p>
<p>Optimal amount of memory was estimated to this stored procedure based on optimize for hint value ‘NY’.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/h22.jpg" alt="" width="740" height="500" /></p>
<p>There was no Hash Warning in SQL Profiler.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/hp1.jpg" alt="" width="740" height="500" /></p>
<p><span class="style18">This article covers underestimation / overestimation of memory for Hash Match operation. <strong><a href="http://www.sqlworkshops.com/plancachingandquerymemory.htm">Plan Caching and Query Memory Part I</a></strong> covers underestimation / overestimation for Sort. It is important to note that underestimation of memory for Sort and Hash Match operations lead to spill over tempdb and hence negatively impact performance. Overestimation of memory affects the memory needs of other concurrently executing queries. <strong>In addition, it is important to note, with Hash Match operations, overestimation of memory can actually lead to poor performance.</strong></span></p>
<p><strong>Summary:</strong> Cached plan might lead to underestimation or overestimation of memory because the memory is estimated based on first set of execution parameters. It is recommended not to cache the plan if the amount of memory required to execute the stored procedure has a wide range of possibilities. <strong style="mso-bidi-font-weight: normal;">One can mitigate this by using recompile hint, but that will lead to compilation overhead. However, in most cases it might be ok to pay for compilation rather than spilling sort over tempdb which could be very expensive compared to compilation cost.</strong> The other possibility is to use optimize for hint, but in case one sorts more data than hinted by optimize for hint, this will still lead to spill. On the other side there is also the possibility of overestimation leading to unnecessary memory issues for other concurrently executing queries. In case of Hash Match operations, this overestimation of memory might lead to poor performance. When the values used in optimize for hint are archived from the database, the estimation will be wrong leading to worst performance, so one has to exercise caution before using optimize for hint, recompile hint is better in this case.</p>
<p><strong>I explain these concepts with detailed examples in my webcasts (<a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a>), I recommend you to watch them. The best way to learn is to practice. To create the above tables and reproduce the behavior, join the mailing list at <a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a> and I will send you the relevant SQL Scripts.</strong></p>
<p>Disclaimer and copyright information: This article refers to organizations and products that may be the trademarks or registered trademarks of their various owners. Copyright of this article belongs to Ramesh Meyyappan / <a href="http://www.sqlworkshops.com">www.sqlworkshops.com</a>. You may freely use the ideas and concepts discussed in this article with acknowledgement (<a href="http://www.sqlworkshops.com">www.sqlworkshops.com</a>), but you may not claim any of it as your own work. This article is for informational purposes only; you use any of the suggestions given here entirely at your own risk. Previously published under / <a href="http://sqlblogcasts.com/blogs/sqlworkshops/archive/2011/02/16/plan-caching-and-query-memory-part-ii-hash-match-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp-executesql-or-prepared-statement.aspx">http://sqlblogcasts.com/blogs/sqlworkshops/archive/2011/02/16/plan-caching-and-query-memory-part-ii-hash-match-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp-executesql-or-prepared-statement.aspx</a> / <a href="http://www.sqlworkshops.com/plancachingandquerymemory2.htm">http://www.sqlworkshops.com/plancachingandquerymemory2.htm</a> and in Level 400 Webcasts <a href="http://www.sqlworkshops.com/webcasts">http://www.sqlworkshops.com/webcasts</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-2-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Workspace Memory / Query Memory Tuning – RESOURCE_SEMAPHORE / IO_COMPLETION / SLEEP_TASK Waits</title>
		<link>http://blog.sqlworkshops.com/workspace-memory-query-memory-tuning-resource_semaphore-io_completion-sleep_task-waits/</link>
		<comments>http://blog.sqlworkshops.com/workspace-memory-query-memory-tuning-resource_semaphore-io_completion-sleep_task-waits/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 15:44:42 +0000</pubDate>
		<dc:creator>R Meyyappan</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://blog.sqlworkshops.com/?p=102</guid>
		<description><![CDATA[SQL Server is configured to use a lot of memory, but my query is slow and not using all the memory available and it is spilling the sort or the hash match operation to tempdb, how can you tune the &#8230; <a href="http://blog.sqlworkshops.com/workspace-memory-query-memory-tuning-resource_semaphore-io_completion-sleep_task-waits/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p class="style18"><strong>SQL Server is configured to use a lot of memory, but my query is slow and not using all the memory available and it is spilling the sort or the hash match operation to tempdb, how can you tune the configuration and the query?</strong></p>
<p class="style18"><span class="style18">Memory allocating queries request memory based on estimation (ideal memory) and query memory (workspace memory) availability, when they don’t get the right amount of memory they spill to tempdb and lead to performance issues. Previous <strong><a href="http://www.sqlworkshops.com/articles.htm">articles</a></strong> discussed ways to make the estimation better; this article discusses ways to address query memory availability.</span></p>
<p><span class="style18">You can find the ideal amount of memory a query needs (when the query is executing) using sys.dm_exec_query_memory_grants. Common memory allocating queries are that perform Sort and do Hash Match operations like Hash Join or Hash Aggregation or Hash Union.</span></p>
<p><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: gray;">*</span> <span style="color: blue;">from</span> <span style="color: green;">sys</span><span style="color: gray;">.</span><span style="color: green;">dm_exec_query_memory_grants</span></span><span class="style18" style="line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="MsoNormal"><span class="style18">Column ideal_memory_kb indicates the ideal amount of memory the query needs. This is based on estimation, this might be incorrect for various reasons including out of date statistics, in some cases (more common that you might think) under estimation of memory by the optimizer even when the statistics are up to date (refer to article <span style="color: #03664b;"><span style="text-decoration: underline;"><span style="text-decoration: underline;"><span class="style18"><a href="http://jahaines.blogspot.com/2010/03/performance-tuning-101-what-you-will.html">http://jahaines.blogspot.com/2010/03/performance-tuning-101-what-you-will.html</a></span></span></span>) and due to plan caching (refer to article <span style="text-decoration: underline;"><span style="text-decoration: underline;"><a href="http://www.sqlworkshops.com/plancachingandquerymemory.htm">Plan Caching and Query Memory</a></span></span>). Some might recommend tweaking the index/column statistics in an undocumented way to inflate the estimation for additional memory, I suggest following the MSSQL Tip <span style="color: #03664b;"><span style="text-decoration: underline;"><a title="http://www.mssqltips.com/sqlservertip/1955" href="http://www.mssqltips.com/sqlservertip/1955" target="_blank"><span style="text-decoration: underline;"><span class="style18">http://www.mssqltips.com/sqlservertip/1955</span></span></a></span> and webcast 1 and 2 at <a href="http://www.sqlworkshops.com/agenda.asp"><span style="text-decoration: underline;">www.sqlworkshops.com/webcasts</span></a> to understand the issue and the recommendations.</span></span></span></p>
<p><span class="style18">To read additional articles I wrote click <span class="style18"><strong><span style="text-decoration: underline;"><strong><span style="text-decoration: underline;"><a href="http://www.sqlworkshops.com/articles.htm">here</a></span></strong></span></strong></span>. To watch the webcasts click <strong><strong><a href="http://www.sqlworkshops.com/webcasts">here</a></strong></strong>.</span></p>
<p><span class="style18">The ideal amount of memory a query needs is based on estimat</span><span class="style18">ion. Requested memory is based on ideal memory and maximum available workspace memory. There is a possibility the estimation is correct (let’s say you have up to date statistics and/or fixed the under estimation issues by following the above articles) but the available workspace memory is not enough and hence ideal amount is low. The next question is: what is available workspace memory.</span></p>
<p><span class="style18">Available workspace memory, also known as query memory, is the amount of memory available for common memory allocating queries that perform Sort and do Hash Match operations. This is automatically calculated based on your system configuration. You can monitor the currently available workspace memory and maximum workspace memory by executing the command dbcc memorystatus and looking for ‘Available’ and ‘Current Max’ under ‘Query Memory Objects (default)’ and ‘Small Query Memory Objects (default)’. Or using Performance Monitor counters ‘Granted Workspace Memory (KB)’ and ‘Maximum Workspace Memory (KB)’ under object Memory Manager. ‘Maximum Workspace Memory (KB)’ is the sum of ‘Current Max’ of both ‘Query Memory Objects (default)’ and ‘Small Query Memory Objects (default)’, note ‘Current Max’ is in pages (* 8 = KB).</span></p>
<p><span class="style18">Here is a query to find the ‘Maximum Workspace Memory (KB)’ using sys.dm_os_performance_counters, ‘Maximum Workspace Memory (KB)’ can be up to 75% of ‘Target Server Memory (KB)’.</span></p>
<p><span class="style18"><strong>The best way to learn is to practice. To create the below tables and reproduce the behavior, join the mailing list by using this link: <a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a> and I will send you the table creation script.</strong></span></p>
<p><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> cntr_value<br />
</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    from</span> <span style="color: green;">sys</span><span style="color: gray;">.</span><span style="color: green;">dm_os_performance_counters<br />
</span><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    where</span> <span style="color: fuchsia;">object_name</span> <span style="color: gray;">like</span> <span style="color: red;">&#8216;%Memory Manager%&#8217;</span> <span style="color: gray;">and</span> counter_name <span style="color: gray;">like</span> <span style="color: red;">&#8216;Maximum Workspace Memory (KB)%&#8217;</span><span style="color: gray;">)</span> <span style="color: blue;">as</span> <span style="color: red;">&#8216;Maximum Workspace Memory (KB)&#8217;</span><span style="color: gray;">,<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 2;">    </span></span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> cntr_value<br />
</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    from</span> <span style="color: green;">sys</span><span style="color: gray;">.</span><span style="color: green;">dm_os_performance_counters<br />
</span><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    where</span> <span style="color: fuchsia;">object_name</span> <span style="color: gray;">like</span> <span style="color: red;">&#8216;%Memory Manager%&#8217;</span> <span style="color: gray;">and</span> counter_name <span style="color: gray;">like</span> <span style="color: red;">&#8216;Target Server Memory (KB)%&#8217;</span><span style="color: gray;">)</span> <span style="color: blue;">as</span> <span style="color: red;">&#8216;Target Server Memory (KB)&#8217;</span><span style="color: gray;">,<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 2;">   </span></span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> cntr_value<br />
</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    from</span> <span style="color: green;">sys</span><span style="color: gray;">.</span><span style="color: green;">dm_os_performance_counters<br />
</span><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    where</span> <span style="color: fuchsia;">object_name</span> <span style="color: gray;">like</span> <span style="color: red;">&#8216;%Memory Manager%&#8217;</span> <span style="color: gray;">and</span> counter_name <span style="color: gray;">like</span> <span style="color: red;">&#8216;Maximum Workspace Memory (KB)%&#8217;</span><span style="color: gray;">)</span> <span style="color: gray;">*</span> 100.0 <span style="color: gray;">/<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 2;">    </span></span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> cntr_value<br />
</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    from</span> <span style="color: green;">sys</span><span style="color: gray;">.</span><span style="color: green;">dm_os_performance_counters<br />
</span><span style="mso-tab-count: 3;">    </span><span style="color: blue;">    where</span> <span style="color: fuchsia;">object_name</span> <span style="color: gray;">like</span> <span style="color: red;">&#8216;%Memory Manager%&#8217;</span> <span style="color: gray;">and</span> counter_name <span style="color: gray;">like</span> <span style="color: red;">&#8216;Target Server Memory (KB)%&#8217;</span><span style="color: gray;">)</span><span style="mso-spacerun: yes;">  </span><span style="color: blue;">as</span> Ratio<br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">In my server, when Target Server Memory is 4096MB, Maximum Workspace Memory is 3077MB, which is about 75% of Target Server Memory.</p>
<p><span class="style18">By default a query will not request more than 25% of this Maximum Workspace Memory in SQL Server 2008 and above and this Memory Grant 25% can be changed using Resource Governor Workload Group settings. With SQL Server 2005 and below, this is 20% and cannot be changed without the support of Microsoft (with a combination of undocumented trace flag and changes to the configuration parameters).</span></p>
<p><span class="style18">Why should you care about 25%? You might have a customer executing a heavy reporting query in the night and they want to use more than 25% of Maximum Workspace Memory for that single query. Let’s assume the customer configured 4096MB for their SQL Server instance (the Target Server Memory might be less than the configured memory if there is memory pressure on the server), let&#8217;s say their workspace memory is 3077MB (‘Query Memory Objects (default)’-&gt;‘Current Max’ = 381175 pages = 3049400KB + ‘Small Query Memory Objects (default)’ -&gt;‘Current Max’ = 12800 pages = 102400KB; 3049400KB + 102400KB = 3077MB. And this customer’s query executing the report is limited to a maximum of 3049400KB / 4 = 744MB.</span></p>
<p><span class="style18">Let’s say the customer’s query executing the report needs 1,470MB of query memory (ideal memory) in order not to spill the sort to tempdb, but the query will request only 744MB. In case the customer can set the ‘request_max_memory_grant_percent’ (Memory Grant %) of the Workload Group setting to 50%, then the query can request up to 1,488MB and in this case the query will request the ideal memory it needs which is 1,470MB.</span></p>
<p><span class="style18">SQL Server has memory grant queues based on cost, if queries need more memory and there is not enough memory available in the queue, then the query will wait, you can get additional details using the command dbcc memorystatus. If a query requests huge amount of memory, but doesn’t utilize it (due to over estimation), this memory will be reserved and cannot be used by other queries, in some cases this will lead to unnecessary memory grant waits. So one has to be very careful not to overestimate (also described in the webcast <a href="http://www.sqlworkshops.com/agenda.asp">www.sqlworkshops.com/webcasts</a>) too much memory as it will affect concurrency. When the query waits for memory, the wait type will be ‘RESOURCE_SEMAPHORE’.</span></p>
<p>Let&#8217;s set &#8216;max server memory (MB)&#8217; to 4GB.</p>
<p><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: maroon;">sp_configure</span><span style="color: red;">&#8216;max server memory (MB)&#8217;</span><span style="color: gray;">,</span> 4096<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
reconfigure<br />
go</span></p>
<p><span class="style18"><strong>The best way to learn is to practice. To create the below tables and reproduce the behavior, join the mailing list by using this link: <span style="color: #03664b;"><span style="text-decoration: underline;"><span style="text-decoration: underline;"><span class="style18"><a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a></span></span></span> and I will send you the table creation script.</span></strong></span></p>
<p><strong><span class="style18">Scenario 1:<br />
</span></strong><span class="style18">This query will underestimate memory due to optimizer issues and will also request less memory (744MB) due to the 25% Resource Governor Workload Group Memory Grant setting. <span class="style18"><span style="color: #ff0000;">The query will be slow spilling the sort to tempdb.</span></span></span></p>
<p><span class="style18">When the sort spills to tempdb the wait type will be ‘<strong>IO_COMPLETION</strong>’ but when the Hash Match operation spills to tempdb the wait type will be ‘<strong>SLEEP_TASK</strong>’.</span></p>
<p class="style18"><span class="style18">We are using option (maxdop 1) to disable parallelism, to learn more about monitoring and tuning parallel query execution, refer to the webcast <a href="http://www.sqlworkshops.com/agenda.asp"><span style="text-decoration: underline;">www.sqlworkshops.com/webcasts</span></a> and article </span><span class="style18"><span style="text-decoration: underline;"><a href="http://www.sqlworkshops.com/parallelmerge.htm"><span style="text-decoration: underline;">Parallel Sort and Merge Join – Watch out for unpredictability in performance</span></a></span></span>.</p>
<p><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
</span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 500000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="color: gray;"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: gray;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span></span></span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">1</span><span style="color: gray;">)</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p><strong><span class="style18">Scenario 2:<br />
</span></strong><span class="style18">This query will have better memory estimation due to the option clause with optimize for hint, but will still request less memory (744MB) due to the 25% Resource Governor Workload Group Memory Grant setting. <span class="style18"><span style="color: #ff0000;">The query will also be slow spilling the sort to tempdb.</span></span></span></p>
<p><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 500000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 600000<span style="color: gray;">)<span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: gray;">, <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span></span></span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">1</span><span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
&#8211; to query optimizer under estimation of memory.<br />
</span><span class="style18"><span style="color: #008000; font-family: Courier New; font-size: x-small;">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span class="style18"><strong>Scenario 3:<br />
</strong>This query will have better memory estimation due to the option clause with optimize for hint and will also request enough memory (1,470MB) due to the 50% Resource Governor Workload Group Memory Grant setting.<span class="style18"><strong><span style="color: #0000ff;">The query will be fast with no spilling of sort to tempdb.</span></strong></span></span></p>
<p class="style18">Please read the entire article and answer the challenge posted at the end of this article before changing the Resource Governor Workload Group Memory Grant setting in your production server as there are major disadvantages of changing the Resource Governor Workload Group Memory Grant setting, don&#8217;t do it without understanding the full picture.</p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">alter</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">workload</span> <span style="color: blue;">group</span> [default] <span style="color: blue;">with</span><span style="color: gray;">(</span>request_max_memory_grant_percent<span style="color: gray;">=</span>50<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
alter</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">resource</span> <span style="color: blue;">governor</span> <span style="color: blue;">reconfigure</span><span class="style18"><br />
</span></span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 500000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 600000<span style="color: gray;">)<span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">, <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span></span>1<span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
<span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; to query optimizer under estimation of memory.<br />
</span><span class="style18">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p><strong><span class="style18">Scenario 4:<br />
</span></strong>L<span class="style18">et&#8217;s execute 2 of this query concurrently in 2 sessions, with 25% Resource Governor Workload Group Memory Grant setting.</span></p>
<p><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">alter</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">workload</span> <span style="color: blue;">group</span> [default] <span style="color: blue;">with</span><span style="color: gray;">(</span>request_max_memory_grant_percent<span style="color: gray;">=</span>25<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
alter</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">resource</span> <span style="color: blue;">governor</span> <span style="color: blue;">reconfigure</span><span class="style18"><br />
</span></span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span class="style18"><span style="text-decoration: underline;">Session 1:</span></span><strong><br />
</strong>Let&#8217;s execute this query in a loop and measure the performance of the query executed in session 2. This query will have better memory estimation due to the option clause with optimize for hint, but will still request less memory (744MB) due to the 25% <span class="style18">Resource Governor Workload Group Memory Grant setting</span>. <span class="style18"><span style="color: #ff0000;">The query will be spilling the sort to tempdb.</span></span></p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">while 1=1<br />
begin<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 500000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 600000<span style="color: gray;">), <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span>1<span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
<span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; to query optimizer under estimation of memory.<br />
<span class="style18">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span>&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">end<br />
go</span></span></p>
<p class="style18"><span class="style18"><span style="text-decoration: underline;">Session 2:</span></span><strong><br />
</strong>Let&#8217;s execute this query few times. This query will have better memory estimation due to the option clause with optimize for hint, but will still request less memory (744MB) due to the 25% <span class="style18">Resource Governor Workload Group Memory Grant setting</span>. <span class="style18"><span style="color: #ff0000;">The query will be slow spilling the sort to tempdb. </span></span>This query might take up to twice the amount of time to complete compared to Scenario 2, because 2 queries from 2 sessions are concurrently spilling to tempdb. There is no wait for memory grants (no RESOURCE_SEMAPHORE waits).</p>
<p><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 500000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 600000<span style="color: gray;">), <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span>1<span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
<span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; to query optimizer under estimation of memory.<br />
<span class="style18">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span>&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></span></p>
<p><strong><span class="style18">Scenario 5:<br />
</span></strong>L<span class="style18">et&#8217;s execute 2 of this query concurrently in 2 sessions, with 50% Resource Governor Workload Group Memory Grant setting.</span></p>
<p><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">alter</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">workload</span> <span style="color: blue;">group</span> [default] <span style="color: blue;">with</span><span style="color: gray;">(</span>request_max_memory_grant_percent<span style="color: gray;">=50)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
alter</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">resource</span> <span style="color: blue;">governor</span> <span style="color: blue;">reconfigure</span><span class="style18"><br />
</span></span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span class="style18"><span style="text-decoration: underline;">Session 1:</span></span><strong><br />
</strong>Let&#8217;s execute this query in a loop and measure the performance of the query executed in session 2. This query will have better memory estimation due to the option clause with optimize for hint and will also request enough memory (1,470MB) due to the 50% Resource Governor Workload Group Memory Grant setting.<span class="style18"><strong><span style="color: #0000ff;">The query will have no spilling of sort to tempdb.</span></strong></span></p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">while 1=1<br />
begin<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 500000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 600000<span style="color: gray;">), <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span>1<span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
<span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; to query optimizer under estimation of memory.<br />
<span class="style18">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span>&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">end<br />
go</span></span></p>
<p class="style18"><span class="style18"><span style="text-decoration: underline;">Session 2:</span></span><strong><br />
</strong>Let&#8217;s execute this query few times. This query will have better memory estimation due to the option clause with optimize for hint and will also request enough memory (1,470MB) due to the 50% Resource Governor Workload Group Memory Grant setting.<span class="style18"><strong><span style="color: #0000ff;">The query will be fast with no spilling of sort to tempdb.</span></strong></span> But this query might take up to twice the amount of time to complete compared to Scenario 3, because 2 queries from 2 sessions are concurrently requesting nearly 50% of workspace memory. <span class="style18"><span style="color: #ff0000;">There is wait for memory grants (RESOURCE_SEMAPHORE waits)</span></span>, SQL Server grants memory to one query at a time as the memory in the grant queue is not enough to grant simultaneously the requested memory to both queries.</p>
<p><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 500000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 600000<span style="color: gray;">), <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span>1<span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
<span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; to query optimizer under estimation of memory.<br />
<span class="style18">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span>&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></span></p>
<p class="style18"><span class="style18"><span style="text-decoration: underline;">Overall performance of scenario 5 is better than scenario 4 even though there was memory grant waits. </span></span>In this case waiting for memory is far worse than spilling the sort to tempdb.</p>
<p><strong><span class="style18">Scenario 6:<br />
</span></strong>L<span class="style18">et&#8217;s execute two queries with less memory requirement concurrently in two sessions (so memory can be granted for both queries simultaneously from the same memory grant queue), with 50% Resource Governor Workload Group Memory Grant setting.</span></p>
<p><span class="style18"><span class="style18"><span style="text-decoration: underline;">Session 1:</span></span><strong><br />
</strong>Let&#8217;s execute this query in a loop and measure the performance of the query executed in session 2. This query will have better memory estimation due to the option clause with optimize for hint and will also request enough memory (1,102MB) due to the 50% Resource Governor Workload Group Memory Grant setting.<span class="style18"><strong><span style="color: #0000ff;">The query will have no spilling of sort to tempdb.</span></strong></span></span></p>
<p><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">while</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> 1<span style="color: gray;">=</span>1</span></span></p>
<p><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">begin<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 400000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 450000<span style="color: gray;">), <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span>1<span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
<span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; to query optimizer under estimation of memory.<br />
<span class="style18">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span>&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">end<br />
go</span></span></p>
<p><span class="style18"><span class="style18"><span style="text-decoration: underline;">Session 2:</span></span><strong><br />
</strong>Let&#8217;s execute this query few times. This query will have better memory estimation due to the option clause with optimize for hint and will also request enough memory (1,102MB) due to the 50% Resource Governor Workload Group Memory Grant setting.<span class="style18"><strong><span style="color: #0000ff;">The query will be fast with no spilling of sort to tempdb.</span></strong></span> <span class="style18"><strong><span style="color: #0000ff;">There will be no wait for memory grants (RESOURCE_SEMAPHORE waits)</span></strong></span>, SQL Server grants memory to both queries simultaneously as the memory in the grant queue is enough to grant the requested memory to both queries.</span></p>
<p><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go<br />
</span>&#8211;Example provided by www.sqlworkshops.com<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c2 <span style="color: blue;">int</span><span style="color: gray;">,</span> @c3 <span style="color: blue;">char</span><span style="color: gray;">(</span>2000<span style="color: gray;">)<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: blue;">int<br />
</span></span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @i <span style="color: gray;">=</span> 400000<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @c1 <span style="color: gray;">=</span> c1<span style="color: gray;">,</span> @c2 <span style="color: gray;">=</span> c2<span style="color: gray;">,</span> @c3 <span style="color: gray;">=</span> c3<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    from</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> tab7<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    where</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> c1 <span style="color: gray;">&lt;</span> @i<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    order</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> c2<br />
</span><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">    option </span><span style="color: gray; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">(</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@i <span style="color: gray;">=</span> 450000<span style="color: gray;">), <span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">maxdop </span></span>1<span style="color: gray;">)<br />
</span></span><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; Option optimize for is used to inflate memory request<br />
&#8211; without this the query will spill the sort to tempdb due<br />
</span><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211; to query optimizer under estimation of memory.<br />
<span class="style18">&#8211; One possibility is to inflate the number of rows,<br />
&#8211; the other possibility is to inflate the row size<br />
</span>&#8211; For more information refer to webcasts 1 &amp; 2<br />
&#8211; at www.sqlworkshops.com/webcasts.</span><br />
go</span></span></p>
<p class="style18"> <span class="style18"><strong>Challenge:<br />
</strong><span class="style18"><span style="color: #ff0000;">There are major disadvantages of changing the Resource Governor Workload Group Memory Grant setting, so don&#8217;t make any changes without understanding the full picture. I will cover this is the next article, meanwhile if you can guess some reasons, write to me: <strong><a href="http://www.sqlworkshops.com/contacts.asp">contacts</a></strong></span></span><span class="style6"><span class="style18" style="font-size: x-small;"><span class="style18"><span style="color: #ff0000;">.</span></span></span></span></span></p>
<p><strong>I explain these concepts with detailed examples in my webcasts (<a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a>), I recommend you to watch them. The best way to learn is to practice. To create the above tables and reproduce the behavior, join the mailing list at <a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a> and I will send you the relevant SQL Scripts.</strong></p>
<p>Disclaimer and copyright information: This article refers to organizations and products that may be the trademarks or registered trademarks of their various owners. Copyright of this article belongs to Ramesh Meyyappan / <a href="http://www.sqlworkshops.com">www.sqlworkshops.com</a>. You may freely use the ideas and concepts discussed in this article with acknowledgement (<a href="http://www.sqlworkshops.com">www.sqlworkshops.com</a>), but you may not claim any of it as your own work. This article is for informational purposes only; you use any of the suggestions given here entirely at your own risk. Previously published under / <a href="http://sqlblogcasts.com/blogs/sqlworkshops/archive/2011/10/14/workspace-memory-query-memory-tuning-resource-semaphore-io-completion-sleep-task-waits.aspx">http://sqlblogcasts.com/blogs/sqlworkshops/archive/2011/10/14/workspace-memory-query-memory-tuning-resource-semaphore-io-completion-sleep-task-waits.aspx</a> / <a href="http://www.sqlworkshops.com/workspacememory.htm">http://www.sqlworkshops.com/workspacememory.htm</a> and in Level 400 Webcasts <a href="http://www.sqlworkshops.com/webcasts">http://www.sqlworkshops.com/webcasts</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqlworkshops.com/workspace-memory-query-memory-tuning-resource_semaphore-io_completion-sleep_task-waits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plan Caching and Query Memory Part 1: When not to use stored procedure or other plan caching mechanisms like sp_executesql or prepared statement</title>
		<link>http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-1-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement-2/</link>
		<comments>http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-1-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement-2/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 15:43:49 +0000</pubDate>
		<dc:creator>R Meyyappan</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://blog.sqlworkshops.com/?p=100</guid>
		<description><![CDATA[The most common performance mistake SQL Server developers make: SQL Server estimates memory requirement for queries at compilation time. This mechanism is fine for dynamic queries that need memory, but not for queries that cache the plan. With dynamic queries &#8230; <a href="http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-1-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><span class="style18">The most common performance mistake SQL Server developers make:</span></p>
<p><span class="style18">SQL Server estimates memory requirement for queries at compilation time. This mechanism is fine for dynamic queries that need memory, but not for queries that cache the plan. With dynamic queries the plan is not reused for different set of parameters values / predicates and hence different amount of memory can be estimated based on different set of parameter values / predicates. Common memory allocating queries are that perform Sort and do Hash Match operations like Hash Join or Hash Aggregation or Hash Union. This article covers Sort with examples. It is recommended to read <strong><a href="http://www.sqlworkshops.com/plancachingandquerymemory2.htm">Plan Caching and Query Memory Part II</a></strong> after this article which covers Hash Match operations.</span></p>
<p><span class="style18">When the plan is cached by using stored procedure or other plan caching mechanisms like sp_executesql or prepared statement, SQL Server estimates memory requirement based on first set of execution parameters. Later when the same stored procedure is called with different set of parameter values, the same amount of memory is used to execute the stored procedure. This might lead to underestimation / overestimation of memory on plan reuse, overestimation of memory might not be a noticeable issue for Sort operations, but underestimation of memory will lead to spill over tempdb resulting in poor performance.</span></p>
<p>This article covers underestimation / overestimation of memory for Sort. <strong><a href="http://www.sqlworkshops.com/plancachingandquerymemory2.htm">Plan Caching and Query Memory Part II</a></strong> covers underestimation / overestimation for Hash Match operation. It is important to note that underestimation of memory for Sort and Hash Match operations lead to spill over tempdb and hence negatively impact performance. Overestimation of memory affects the memory needs of other concurrently executing queries. <strong>In addition, it is important to note, with Hash Match operations, overestimation of memory can actually lead to poor performance.</strong></p>
<p><span class="style18">To read additional articles I wrote click <strong><strong><a href="http://www.sqlworkshops.com/articles.htm">here</a></strong></strong>. To watch the webcasts click <strong><strong><a href="http://www.sqlworkshops.com/webcasts">here</a></strong></strong>.</span></p>
<p><span class="style18">In most cases it is cheaper to pay for the compilation cost of dynamic queries than huge cost for spill over tempdb, unless memory requirement for a stored procedure does not change significantly based on predicates.</span></p>
<p><span class="style18"><strong>The best way to learn is to practice. To create the below tables and reproduce the behavior, join the mailing list by using this link: <span style="color: #03664b;"><span style="text-decoration: underline;"><span style="text-decoration: underline;"><span class="style23"><a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a></span></span></span></span> and I will send you the table creation script. Most of these concepts are also covered in our webcasts: <span style="text-decoration: underline;"><a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a></span></strong></span></p>
<p>Enough theory, let’s see an example where we sort initially 1 month of data and then use the stored procedure to sort 6 months of data.</p>
<p class="style18">Let’s create a stored procedure that sorts customers by name within certain date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">create</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByCreationDate @CreationDateFrom <span style="color: blue;">datetime</span><span style="color: gray;">,</span> @CreationDateTo <span style="color: blue;">datetime</span> <span style="color: blue;">as</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">begin</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">declare</span> @CustomerID <span style="color: blue;">int</span><span style="color: gray;">,</span> @CustomerName <span style="color: blue;">varchar</span><span style="color: gray;">(</span>48<span style="color: gray;">),</span> @CreationDate <span style="color: blue;">datetime</span></span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">select</span> @CustomerName <span style="color: gray;">=</span> c<span style="color: gray;">.</span>CustomerName<span style="color: gray;">,</span> @CreationDate <span style="color: gray;">=</span> c<span style="color: gray;">.</span>CreationDate <span style="color: blue;">from</span> Customers c</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">where</span> c<span style="color: gray;">.</span>CreationDate <span style="color: gray;">between</span> @CreationDateFrom <span style="color: gray;">and</span> @CreationDateTo</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">order</span> <span style="color: blue;">by</span> c<span style="color: gray;">.</span>CustomerName</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">option </span><span style="color: gray;">(</span><span style="color: blue;">maxdop</span> 1<span style="color: gray;">)</span></span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">end</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">Let’s execute the stored procedure initially with 1 month date range.</p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; mso-no-proof: yes;">set</span><span style="font-family: 'Courier New'; mso-no-proof: yes;"><span style="color: blue;">statistics</span> <span style="color: blue;">time</span> <span style="color: blue;">on</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-01-31&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 48 ms to complete.</p>
<p><img src="http://images.sqlworkshops.com/m1.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure was granted 6656 KB based on 43199.9 rows being estimated.</p>
<p><img src="http://images.sqlworkshops.com/m2.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The estimated number of rows, 43199.9 is similar to actual number of rows 43200 and hence the memory estimation should be ok.</p>
<p><img src="http://images.sqlworkshops.com/m19.jpg" alt="" width="740" height="500" /></p>
<p class="style18">There was no Sort Warnings in SQL Profiler.</p>
<p><img src="http://images.sqlworkshops.com/mp1.jpg" alt="" width="739" height="499" /></p>
<p class="style18">Now let’s execute the stored procedure with 6 month date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-06-30&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 679 ms to complete.</p>
<p><img src="http://images.sqlworkshops.com/m3.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure was granted 6656 KB based on 43199.9 rows being estimated.</p>
<p><img src="http://images.sqlworkshops.com/m4.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The estimated number of rows, 43199.9 is way different from the actual number of rows 259200 because the estimation is based on the first set of parameter value supplied to the stored procedure which is 1 month in our case. This underestimation will lead to sort spill over tempdb, resulting in poor performance.</p>
<p><img src="http://images.sqlworkshops.com/m20.jpg" alt="" width="740" height="500" /></p>
<p class="style18">There was Sort Warnings in SQL Profiler.</p>
<p><img src="http://images.sqlworkshops.com/mp2.jpg" alt="" width="739" height="499" /></p>
<p class="style18">To monitor the amount of data written and read from tempdb, one can execute <span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">select</span><span style="line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> num_of_bytes_written<span style="color: gray;">,</span> num_of_bytes_read <span style="color: blue;">from</span> <span style="color: green;">sys</span><span style="color: gray;">.</span><span style="color: green;">dm_io_virtual_file_stats</span><span style="color: gray;">(</span>2<span style="color: gray;">,</span> <span style="color: gray;">NULL)</span></span> before and after the stored procedure execution, for additional information refer to the webcast: <span class="style18"><span style="text-decoration: underline;"><strong><a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a></strong></span>.</span></p>
<p class="style18">Let’s recompile the stored procedure and then let’s first execute the stored procedure with 6 month date range.</p>
<p><span style="line-height: 115%; font-family: 'Calibri','sans-serif'; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">In a production instance it is not advisable to use sp_recompile instead one should use DBCC FREEPROCCACHE (plan_handle). This is due to locking issues involved with sp_recompile, refer to our webcasts for further details.</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: maroon;">sp_recompile</span>CustomersByCreationDate</span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: green; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-06-30&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">Now the stored procedure took only 294 ms instead of 679 ms.</p>
<p><img src="http://images.sqlworkshops.com/m5.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure was granted 26832 KB of memory.</p>
<p><img src="http://images.sqlworkshops.com/m6.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The estimated number of rows, 259200 is similar to actual number of rows of 259200. Better performance of this stored procedure is due to better estimation of memory and avoiding sort spill over tempdb.</p>
<p><img src="http://images.sqlworkshops.com/m21.jpg" alt="" width="740" height="500" /></p>
<p class="style18">There was no Sort Warnings in SQL Profiler.</p>
<p><img src="http://images.sqlworkshops.com/mp1.jpg" alt="" width="739" height="499" /></p>
<p class="style18">Now let’s execute the stored procedure with 1 month date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-01-31&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 49 ms to complete, similar to our very first stored procedure execution.</p>
<p><img src="http://images.sqlworkshops.com/m7.jpg" alt="" width="740" height="500" /><br />
This stored procedure was granted more memory (26832 KB) than necessary memory (6656 KB) based on 6 months of data estimation (259200 rows) instead of 1 month of data estimation (43199.9 rows). This is because the estimation is based on the first set of parameter value supplied to the stored procedure which is 6 months in this case. This overestimation did not affect performance, but it might affect performance of other concurrent queries requiring memory and hence overestimation is not recommended. This overestimation might affect performance Hash Match operations, refer to article <strong><a href="http://www.sqlworkshops.com/plancachingandquerymemory2.htm">Plan Caching and Query Memory Part II</a></strong> for further details.</p>
<p><img src="http://images.sqlworkshops.com/m8.jpg" alt="" width="740" height="500" /></p>
<p class="style18">Let’s recompile the stored procedure and then let’s first execute the stored procedure with 2 day date range.</p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: maroon;">sp_recompile</span>CustomersByCreationDate</span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: green; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-01-02&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 1 ms.</p>
<p><img src="http://images.sqlworkshops.com/m9.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure was granted 1024 KB based on 1440 rows being estimated.</p>
<p><img src="http://images.sqlworkshops.com/m10.jpg" alt="" width="740" height="500" /></p>
<p class="style18">There was no Sort Warnings in SQL Profiler.</p>
<p><img src="http://images.sqlworkshops.com/mp1.jpg" alt="" width="739" height="499" /></p>
<p class="style18">Now let’s execute the stored procedure with 6 month date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-06-30&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 955 ms to complete, way higher than 679 ms or 294ms we noticed before.</p>
<p><img src="http://images.sqlworkshops.com/m11.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure was granted 1024 KB based on 1440 rows being estimated. But we noticed in the past this stored procedure with 6 month date range needed 26832 KB of memory to execute optimally without spill over tempdb. This is clear underestimation of memory and the reason for the very poor performance.</p>
<p><img src="http://images.sqlworkshops.com/m12.jpg" alt="" width="740" height="500" /></p>
<p class="style18">There was Sort Warnings in SQL Profiler. Unlike before this was a Multiple pass sort instead of Single pass sort. This occurs when granted memory is too low.</p>
<p><img src="http://images.sqlworkshops.com/mp3.jpg" alt="" width="739" height="499" /></p>
<p class="style18"><strong>Intermediate Summary:</strong> This issue can be avoided by not caching the plan for memory allocating queries. Other possibility is to use recompile hint or optimize for hint to allocate memory for predefined date range.</p>
<p class="style18">Let’s recreate the stored procedure with recompile hint.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">drop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByCreationDate</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">create</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByCreationDate @CreationDateFrom <span style="color: blue;">datetime</span><span style="color: gray;">,</span> @CreationDateTo <span style="color: blue;">datetime</span> <span style="color: blue;">as</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">begin</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">declare</span> @CustomerID <span style="color: blue;">int</span><span style="color: gray;">,</span> @CustomerName <span style="color: blue;">varchar</span><span style="color: gray;">(</span>48<span style="color: gray;">),</span> @CreationDate <span style="color: blue;">datetime</span></span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">select</span> @CustomerName <span style="color: gray;">=</span> c<span style="color: gray;">.</span>CustomerName<span style="color: gray;">,</span> @CreationDate <span style="color: gray;">=</span> c<span style="color: gray;">.</span>CreationDate <span style="color: blue;">from</span> Customers c</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">where</span> c<span style="color: gray;">.</span>CreationDate <span style="color: gray;">between</span> @CreationDateFrom <span style="color: gray;">and</span> @CreationDateTo</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">order</span> <span style="color: blue;">by</span> c<span style="color: gray;">.</span>CustomerName</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">option </span><span style="color: gray;">(</span><span style="color: blue;">maxdop</span> 1<span style="color: gray;">,</span> <span style="color: blue;">recompile</span><span style="color: gray;">)</span></span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">end</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">Let’s execute the stored procedure initially with 1 month date range and then with 6 month date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-01-30&#8242;</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-06-30&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 48ms and 291 ms in line with previous optimal execution times.</p>
<p><img src="http://images.sqlworkshops.com/m13.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure with 1 month date range has good estimation like before.</p>
<p><img src="http://images.sqlworkshops.com/m14.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure with 6 month date range also has good estimation and memory grant like before because the query was recompiled with current set of parameter values.</p>
<p><img src="http://images.sqlworkshops.com/m15.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The compilation time and compilation CPU of 1 ms is not expensive in this case compared to the performance benefit.</p>
<p class="style18"><img src="http://images.sqlworkshops.com/m22.jpg" alt="" width="740" height="500" /></p>
<p>&nbsp;</p>
<p class="style18">Let’s recreate the stored procedure with optimize for hint of 6 month date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">drop</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByCreationDate</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">create</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> <span style="color: blue;">proc</span> CustomersByCreationDate @CreationDateFrom <span style="color: blue;">datetime</span><span style="color: gray;">,</span> @CreationDateTo <span style="color: blue;">datetime</span> <span style="color: blue;">as</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">begin</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">declare</span> @CustomerID <span style="color: blue;">int</span><span style="color: gray;">,</span> @CustomerName <span style="color: blue;">varchar</span><span style="color: gray;">(</span>48<span style="color: gray;">),</span> @CreationDate <span style="color: blue;">datetime</span></span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">select</span> @CustomerName <span style="color: gray;">=</span> c<span style="color: gray;">.</span>CustomerName<span style="color: gray;">,</span> @CreationDate <span style="color: gray;">=</span> c<span style="color: gray;">.</span>CreationDate <span style="color: blue;">from</span> Customers c</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">where</span> c<span style="color: gray;">.</span>CreationDate <span style="color: gray;">between</span> @CreationDateFrom <span style="color: gray;">and</span> @CreationDateTo</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">order</span> <span style="color: blue;">by</span> c<span style="color: gray;">.</span>CustomerName</span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">option </span><span style="color: gray;">(</span><span style="color: blue;">maxdop</span> 1<span style="color: gray;">,</span> optimize <span style="color: blue;">for </span><span style="color: gray;">(</span>@CreationDateFrom <span style="color: gray;">=</span> <span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> @CreationDateTo <span style="color: gray;">=</span><span style="color: red;">&#8217;2001-06-30&#8242;</span><span style="color: gray;">))</span></span></p>
<p class="style18"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="color: blue;">end</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">Let’s execute the stored procedure initially with 1 month date range and then with 6 month date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-01-30&#8242;</span></span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-06-30&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 48ms and 291 ms in line with previous optimal execution times.</p>
<p><img src="http://images.sqlworkshops.com/m16.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure with 1 month date range has overestimation of rows and memory. This is because we provided hint to optimize for 6 months of data.</p>
<p><img src="http://images.sqlworkshops.com/m17.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure with 6 month date range has good estimation and memory grant because we provided hint to optimize for 6 months of data.</p>
<p><img src="http://images.sqlworkshops.com/m18.jpg" alt="" width="740" height="500" /></p>
<p class="style18">Let’s execute the stored procedure with 12 month date range using the currently cashed plan for 6 month date range.</p>
<p class="style18"><span style="color: green; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">&#8211;Example provided by www.sqlworkshops.com</span></p>
<p class="style18"><span style="color: blue; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">exec</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> CustomersByCreationDate<span style="color: red;">&#8217;2001-01-01&#8242;</span><span style="color: gray;">,</span> <span style="color: red;">&#8217;2001-12-31&#8242;</span></span></p>
<p class="style18"><span style="color: blue; line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">go</span></p>
<p class="style18">The stored procedure took 1138 ms to complete.</p>
<p><img src="http://images.sqlworkshops.com/m23.jpg" alt="" width="740" height="500" /></p>
<p class="style18">2592000 rows were estimated based on optimize for hint value for 6 month date range. Actual number of rows is 524160 due to 12 month date range.</p>
<p><img src="http://images.sqlworkshops.com/m24.jpg" alt="" width="740" height="500" /></p>
<p class="style18">The stored procedure was granted enough memory to sort 6 month date range and not 12 month date range, so there will be spill over tempdb.</p>
<p><img src="http://images.sqlworkshops.com/m25.jpg" alt="" width="740" height="500" /><br />
There was Sort Warnings in SQL Profiler.<br />
<img src="http://images.sqlworkshops.com/mp2.jpg" alt="" width="739" height="499" /></p>
<p class="style18">As we see above, optimize for hint cannot guarantee enough memory and optimal performance compared to recompile hint.</p>
<p><span class="style18">This article covers underestimation / overestimation of memory for Sort. <strong><a href="http://www.sqlworkshops.com/plancachingandquerymemory2.htm">Plan Caching and Query Memory Part II</a></strong> covers underestimation / overestimation for Hash Match operation. It is important to note that underestimation of memory for Sort and Hash Match operations lead to spill over tempdb and hence negatively impact performance. Overestimation of memory affects the memory needs of other concurrently executing queries. <strong>In addition, it is important to note, with Hash Match operations, overestimation of memory can actually lead to poor performance.</strong></span></p>
<p><strong>Summary:</strong> Cached plan might lead to underestimation or overestimation of memory because the memory is estimated based on first set of execution parameters. It is recommended not to cache the plan if the amount of memory required to execute the stored procedure has a wide range of possibilities. <strong style="mso-bidi-font-weight: normal;">One can mitigate this by using recompile hint, but that will lead to compilation overhead. However, in most cases it might be ok to pay for compilation rather than spilling sort over tempdb which could be very expensive compared to compilation cost.</strong> The other possibility is to use optimize for hint, but in case one sorts more data than hinted by optimize for hint, this will still lead to spill. On the other side there is also the possibility of overestimation leading to unnecessary memory issues for other concurrently executing queries. In case of Hash Match operations, this overestimation of memory might lead to poor performance. When the values used in optimize for hint are archived from the database, the estimation will be wrong leading to worst performance, so one has to exercise caution before using optimize for hint, recompile hint is better in this case.</p>
<p><strong>I explain these concepts with detailed examples in my webcasts (<a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a>), I recommend you to watch them. The best way to learn is to practice. To create the above tables and reproduce the behavior, join the mailing list at <a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a> and I will send you the relevant SQL Scripts.</strong></p>
<p>Disclaimer and copyright information: This article refers to organizations and products that may be the trademarks or registered trademarks of their various owners. Copyright of this article belongs to Ramesh Meyyappan / <a href="http://www.sqlworkshops.com">www.sqlworkshops.com</a>. You may freely use the ideas and concepts discussed in this article with acknowledgement (<a href="http://www.sqlworkshops.com">www.sqlworkshops.com</a>), but you may not claim any of it as your own work. This article is for informational purposes only; you use any of the suggestions given here entirely at your own risk. Previously published under / <a href="http://sqlblogcasts.com/blogs/sqlworkshops/archive/2011/02/08/plan-caching-and-query-memory-part-i-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp-executesql-or-prepared-statement-he-most-common-performance-mistake-sql-server-developers-make.aspx">http://sqlblogcasts.com/blogs/sqlworkshops/archive/2011/02/08/plan-caching-and-query-memory-part-i-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp-executesql-or-prepared-statement-he-most-common-performance-mistake-sql-server-developers-make.aspx</a> / <a href="http://www.sqlworkshops.com/plancachingandquerymemory.htm">http://www.sqlworkshops.com/plancachingandquerymemory.htm</a> and in Level 400 Webcasts <a href="http://www.sqlworkshops.com/webcasts">http://www.sqlworkshops.com/webcasts</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqlworkshops.com/plan-caching-and-query-memory-part-1-when-not-to-use-stored-procedure-or-other-plan-caching-mechanisms-like-sp_executesql-or-prepared-statement-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server IO handling mechanism can be severely affected by high CPU usage</title>
		<link>http://blog.sqlworkshops.com/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage/</link>
		<comments>http://blog.sqlworkshops.com/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage/#comments</comments>
		<pubDate>Tue, 29 May 2012 18:43:19 +0000</pubDate>
		<dc:creator>R Meyyappan</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://blog.sqlworkshops.com/?p=73</guid>
		<description><![CDATA[Are you using SSD or SAN / NAS based storage solution and sporadically observe SQL Server experiencing high IO wait times or from time to time your DAS / HDD becomes very slow according to SQL Server statistics? Read on… &#8230; <a href="http://blog.sqlworkshops.com/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Are you using SSD or SAN / NAS based storage solution and sporadically observe SQL Server experiencing high IO wait times or from time to time your DAS / HDD becomes very slow according to SQL Server statistics? Read on… I need your help to up vote my connect item &#8211; <a href="https://connect.microsoft.com/SQLServer/feedback/details/744650/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage">https://connect.microsoft.com/SQLServer/feedback/details/744650/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage</a>. Instead of taking few seconds, queries could take minutes/hours to complete when CPU is busy.</p>
<p>To read additional articles I wrote click <strong><strong><a href="http://www.sqlworkshops.com/articles.htm">here</a></strong></strong>. To watch the webcasts click <strong><strong><a href="http://www.sqlworkshops.com/webcasts">here</a></strong></strong>.</p>
<p>In SQL Server when a query / request needs to read data that is not in data cache or when the request has to write to disk, like transaction log records, the request / task will queue up the IO operation and wait for it to complete (task in suspended state, this wait time is the resource wait time). When the IO operation is complete, the task will be queued to run on the CPU. If the CPU is busy executing other tasks, this task will wait (task in runnable state) until other tasks in the queue either complete or get suspended due to waits or exhaust their quantum of 4ms (this is the signal wait time, which along with resource wait time will increase the overall wait time). When the CPU becomes free, the task will finally be run on the CPU (task in running state).</p>
<p>The signal wait time can be up to 4ms per runnable task, this is by design. So if a CPU has 5 runnable tasks in the queue, then this query after the resource becomes available might wait up to a maximum of 5 X 4ms = 20ms in the runnable state (normally less as other tasks might not use the full quantum).</p>
<p>In case the CPU usage is high, let’s say many CPU intensive queries are running on the instance, there is a possibility that the IO operations that are completed at the Hardware and Operating System level are not yet processed by SQL Server, keeping the task in the resource wait state for longer than necessary. In case of an SSD, the IO operation might even complete in less than a millisecond, but it might take SQL Server 100s of milliseconds, for instance, to process the completed IO operation. For example, let’s say you have a user inserting 500 rows in individual transactions. When the transaction log is on an SSD or battery backed up controller that has write cache enabled, all of these inserts will complete in 100 to 200ms. With a CPU intensive parallel query executing across all CPU cores, the same inserts might take minutes to complete. WRITELOG wait time will be very high in this case (both under sys.dm_io_virtual_file_stats and sys.dm_os_wait_stats). In addition you will notice a large number of WAITELOG waits since log records are written by LOG WRITER and hence very high signal_wait_time_ms leading to more query delays. However, Performance Monitor Counter, PhysicalDisk, Avg. Disk sec/Write will report very low latency times.</p>
<p>Such delayed IO handling also occurs to read operations with artificially very high PAGEIOLATCH_SH wait time (with number of PAGEIOLATCH_SH waits remaining the same). This problem will manifest more and more as customers start using SSD based storage for SQL Server, since they drive the CPU usage to the limits with faster IOs. We have a few workarounds for specific scenarios, but we think Microsoft should resolve this issue at the product level. We have a connect item open &#8211; <a href="https://connect.microsoft.com/SQLServer/feedback/details/744650/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage">https://connect.microsoft.com/SQLServer/feedback/details/744650/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage</a> - (with example scripts) to reproduce this behavior, please up vote the item so the issue will be addressed by the SQL Server product team soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqlworkshops.com/sql-server-io-handling-mechanism-can-be-severely-affected-by-high-cpu-usage/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Prefetch – Querying at the speed of SAN</title>
		<link>http://blog.sqlworkshops.com/prefetch/</link>
		<comments>http://blog.sqlworkshops.com/prefetch/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 14:26:58 +0000</pubDate>
		<dc:creator>R Meyyappan</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://blog.sqlworkshops.com/?p=20</guid>
		<description><![CDATA[Prefetch is a mechanism with which SQL Server can fire up many I/O requests in parallel for Nested Loop join. The SAN administrator says your data volume has a throughput capacity of 400MB/sec. But your long running query is waiting &#8230; <a href="http://blog.sqlworkshops.com/prefetch/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Prefetch is a mechanism with which SQL Server can fire up many I/O requests in parallel for Nested Loop join.</p>
<p>The SAN administrator says your data volume has a throughput capacity of 400MB/sec. But your long running query is waiting for I/Os (PAGEIOLATCH_SH) and Windows Performance Monitor shows your data volume is doing 4MB/sec. Where is the problem?</p>
<p>When SQL Server does a Nested Loop join, it might enable Prefetch. Prefetch is a functionality where SQL Server fires up many I/O requests in parallel (many outstanding I/Os). Prefetch will lead to better performance for Nested Loop join queries when there are lots of rows in the outer input table. 25 is the magic number. When SQL Server estimates less than 25 rows for the outer input table Prefetch will be disabled. And when more than 25 rows are estimated Prefetch will be enabled.</p>
<p>To read additional articles I wrote click <strong><strong><a href="http://www.sqlworkshops.com/articles.htm">here</a></strong></strong>. To watch the webcasts click <strong><strong><a href="http://www.sqlworkshops.com/webcasts">here</a></strong></strong>.</p>
<p>This is a perfect design (yes, I am an ex-Microsoft employee!); SQL Server wants to avoid Prefetch for light weight queries, especially in an OLTP environment as too many queries doing Prefetch will hurt performance. I remember from my days at SQL Server Development team, OLTP benchmarks including latest TPC-E disables Prefetch with trace flag 8744. Write me an email and I will send you the link.</p>
<p>Why this is a problem: I observe this at many customers; plan is cached with Prefetch disabled because first execution resulted in less than 25 rows estimation for the outer input table. Then the plan is used for parameters that are resulting in more than 25 rows, actually 1000s of rows for the outer input table. In this case since SQL Server cached the plan, Prefetch is disabled and SQL Server is firing up 1 I/O at a time. This leads to poor utilization of SAN. Symptoms: Query is waiting for I/Os (PAGEIOLATCH_SH) for a long time (reading page after page), but disk queue length is never more than 1.</p>
<p><strong>The best way to learn is to practice. To create the below tables and reproduce the behavior, join the mailing list by using this link: <a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a> and I will send you the table creation script.</strong></p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;Create procedure that pulls orders based on City<br />
drop proc RegionalOrdersProc<br />
go<br />
create proc RegionalOrdersProc @City char(20)<br />
as<br />
begin<br />
declare @OrderID int, @OrderDetails char(200)<br />
select @OrderID = o.OrderID, @OrderDetails = o.OrderDetails<br />
from RegionalOrders ao inner join Orders o on (o.OrderID = ao.OrderID)<br />
where City = @City<br />
end<br />
go</p>
<p>set statistics time on<br />
go</p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;Execute the procedure with parameter SmallCity1<br />
exec RegionalOrdersProc &#8216;SmallCity1&#8242;<br />
go</p>
<p>&#8211;Right click on Clustered Index Scan<br />
&#8211;to look at the properties<br />
&#8211;Estimated number of rows in the<br />
&#8211;outer input table is less than 25<br />
<img src="http://images.sqlworkshops.com/Prefetch5.jpg" alt="" width="740" height="500" /></p>
<p>&#8211;Right click on Nested Loops<br />
&#8211;to look at the properties<br />
&#8211;Notice Prefetch is disabled (missing)<br />
<img src="http://images.sqlworkshops.com/Prefetch6.jpg" alt="" width="740" height="500" /></p>
<p>&#8211;Clear data cache<br />
checkpoint<br />
dbcc dropcleanbuffers<br />
go</p>
<p>Think about a business running a billing job looping over thousands of their customers using stored procedure. If the first customer has placed less than 25 orders for the billing period then Prefetch will be disabled and every customer is limited to using 1 spindle at a time.</p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;Execute the procedure with parameter BigCity<br />
&#8211;We are using cached plan<br />
exec RegionalOrdersProc &#8216;BigCity&#8217;<br />
go</p>
<p>&#8211;Estimated number of rows in the<br />
&#8211;outer input table is less than 25<br />
<img src="http://images.sqlworkshops.com/Prefetch2.jpg" alt="" width="740" height="500" /></p>
<p>&#8211;Notice Prefetch is disabled (missing)<br />
<img src="http://images.sqlworkshops.com/Prefetch1.jpg" alt="" width="740" height="500" /></p>
<p>&#8211;Query execution time 7 seconds<br />
&#8211;What was the execution time?</p>
<p>&#8211;Clear procedure cache<br />
&#8211;to trigger a new optimization<br />
dbcc freeproccache<br />
go</p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;Execute the procedure with parameter SmallCity2<br />
exec RegionalOrdersProc &#8216;SmallCity2&#8242;<br />
go</p>
<p>&#8211;Clear data cache<br />
checkpoint<br />
dbcc dropcleanbuffers<br />
go</p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;Execute the procedure with parameter SmallCity<br />
&#8211;We are using cached plan<br />
exec RegionalOrdersProc &#8216;BigCity&#8217;<br />
go</p>
<p>&#8211;Estimated number of rows in the<br />
&#8211;outer input table is greater than 25<br />
<img src="http://images.sqlworkshops.com/Prefetch4.jpg" alt="" width="740" height="500" /></p>
<p>&#8211;Notice Prefetch is enabled<br />
<img src="http://images.sqlworkshops.com/Prefetch3.jpg" alt="" width="740" height="500" /></p>
<p>&#8211;Query execution time 3 seconds<br />
&#8211;On a SAN it was less than a second<br />
&#8211;What was the execution time?</p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;You can fix the issue by using any of the following<br />
&#8211;hints<br />
&#8211;Create procedure that pulls orders based on City</p>
<p>drop proc RegionalOrdersProc<br />
go<br />
create proc RegionalOrdersProc @City char(20)<br />
as<br />
begin<br />
declare @OrderID int, @OrderDetails char(200)<br />
select @OrderID = o.OrderID, @OrderDetails = o.OrderDetails<br />
from RegionalOrders ao inner join Orders o on (o.OrderID = ao.OrderID)<br />
where City = @City<br />
&#8211;Hinting optimizer to use SmallCity2 for estimation<br />
option (optimize for (@City = &#8216;SmallCity2&#8242;))<br />
&#8211;Hinting optimizer to estimate for the currnet parameters<br />
&#8211;option (recompile)<br />
&#8211;Hinting optimize not to use histogram rather<br />
&#8211;density for estimation (average of all 3 cities)<br />
&#8211;option (optimize for (@City UNKNOWN))<br />
&#8211;option (optimize for UNKNOWN)<br />
end<br />
go</p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;Execute the procedure with parameter SmallCity1<br />
exec RegionalOrdersProc &#8216;SmallCity1&#8242;<br />
go</p>
<p>&#8211;Clear data cache<br />
checkpoint<br />
dbcc dropcleanbuffers<br />
go</p>
<p>&#8211;Example provided by <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a><br />
&#8211;Execute the procedure with parameter BigCity<br />
&#8211;We are using cached plan<br />
exec RegionalOrdersProc &#8216;BigCity&#8217;<br />
go</p>
<p>&#8211;Notice Prefetch is enabled<br />
&#8211;Estimated number of rows in the<br />
&#8211;outer input table is greater than 25<br />
&#8211;Query execution time 3 seconds<br />
&#8211;On a SAN it was less than a second<br />
&#8211;What was your execution time?</p>
<p><strong>Some of you might think plan guides or plan freezing might solve this issue, well it won’t help here.</strong> The estimation from your plan guide plan will be ignored by the optimizer. Optimizer makes new estimation based on current set of parameter values (compile time parameter values).</p>
<p><strong>I explain these concepts with detailed examples in my webcasts (<a href="http://www.sqlworkshops.com/webcasts">www.sqlworkshops.com/webcasts</a>), I recommend you to watch them. The best way to learn is to practice. To create the above tables and reproduce the behavior, join the mailing list at <a href="http://www.sqlworkshops.com/ml">www.sqlworkshops.com/ml</a> and I will send you the relevant SQL Scripts.</strong></p>
<p>If you search for trace flag 8744 you will end up with a KB Article 920093 titled: Tuning options for SQL Server 2005 and SQL Server 2008 when running in high performance workloads. There it is explained: Trace flag 8744 disables pre-fetching for the Nested Loops operator (with caution). Even though the topic looks interesting ‘Tuning options for high performance workloads’, don’t try these in production. From my point of view this article is about documenting undocumented trace flags used in benchmarks, let’s leave it at that!</p>
<p>Disclaimer and copyright information:<br />
This article refers to organizations and products that may be the trademarks or registered trademarks of their various owners.<br />
Copyright of this article belongs to Ramesh Meyyappan / <a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a>. You may freely use the ideas and concepts discussed in this article with acknowledgement (<a href="http://www.sqlworkshops.com/">www.sqlworkshops.com</a>), but you may not claim any of it as your own work.<br />
This article is for informational purposes only; you use any of the suggestions given here entirely at your own risk.</p>
<p>Previously published under <a href="http://sqlblogcasts.com/blogs/sqlworkshops/archive/2010/10/19/prefetch-querying-at-the-speed-of-san.aspx">http://sqlblogcasts.com/blogs/sqlworkshops/archive/2010/10/19/prefetch-querying-at-the-speed-of-san.aspx</a> / <a href="http://www.mssqltips.com/sqlservertip/2173/speed-up-sql-server-queries-with-prefetch">http://www.mssqltips.com/sqlservertip/2173/speed-up-sql-server-queries-with-prefetch</a> / <a href="http://www.sqlworkshops.com/prefetch.htm">http://www.sqlworkshops.com/prefetch.htm</a> and in Level 400 Webcasts <strong><a href="http://www.sqlworkshops.com/webcasts">http://www.sqlworkshops.com/webcasts</a></strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sqlworkshops.com/prefetch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
