<?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>OPTION FOR Hint &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/option-for-hint/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 20 Nov 2017 05:59:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>SQL Server Parameter Sniffing</title>
		<link>https://www.dbrnd.com/2015/05/sql-server-parameter-sniffing/</link>
					<comments>https://www.dbrnd.com/2015/05/sql-server-parameter-sniffing/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 31 May 2015 16:49:49 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[OPTION FOR Hint]]></category>
		<category><![CDATA[OPTION Recompile]]></category>
		<category><![CDATA[Parameter Sniffing]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=215</guid>

					<description><![CDATA[In this post, I am sharing about the Parameter Sniffing of SQL Server. I will discuss, why the query optimizer sometimes fails to choose a proper query plan. One day I experienced that one of my production stored procedure was not returning expected performance, and before two days, same stored procedure was running fine. At that time, I didn&#8217;t know [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fsql-server-parameter-sniffing%2F&amp;linkname=SQL%20Server%20Parameter%20Sniffing" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fsql-server-parameter-sniffing%2F&amp;linkname=SQL%20Server%20Parameter%20Sniffing" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fsql-server-parameter-sniffing%2F&amp;linkname=SQL%20Server%20Parameter%20Sniffing" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fsql-server-parameter-sniffing%2F&amp;linkname=SQL%20Server%20Parameter%20Sniffing" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fsql-server-parameter-sniffing%2F&amp;linkname=SQL%20Server%20Parameter%20Sniffing" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fsql-server-parameter-sniffing%2F&amp;linkname=SQL%20Server%20Parameter%20Sniffing" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fsql-server-parameter-sniffing%2F&#038;title=SQL%20Server%20Parameter%20Sniffing" data-a2a-url="https://www.dbrnd.com/2015/05/sql-server-parameter-sniffing/" data-a2a-title="SQL Server Parameter Sniffing"></a></p><div>
<div>
<p>In this post, I am sharing about the Parameter Sniffing of SQL Server. I will discuss, why the query optimizer sometimes fails to choose a proper query plan.</p>
</div>
<div>
<p>One day I experienced that one of my production stored procedure was not returning expected performance, and before two days, same stored procedure was running fine.</p>
</div>
<div>
<p>At that time, I didn&#8217;t know about the Parameter Sniffing and started to look into this problem.</p>
</div>
<div>
<p>I found that before two days there were total 25000 records and later there were total 10,00,00 records.</p>
</div>
<div>
<p>I concluded that records increased quickly, but not increased ten times, and it increased just three times, and even I had already tested this stored procedure for 10,00,00,00 records.</p>
</div>
</div>
<div></div>
<div>
<p><b>Now time to know about the Parameter Sniffing:</b></p>
</div>
<div></div>
<div>
<blockquote><p>Many times query optimizer chooses old query plan for execution because it stores this plan into the cache for frequently running queries.</p></blockquote>
</div>
<blockquote>
<div>
<p>Now, what happened when old query generated table scan for the first set of records and later the same parameter is required to change for index scanning because records increased.</p>
</div>
<div>
<p>I check the query execution plan and found that in my situation query optimizer use old query plan instead of to create a new query plan.</p>
</div>
</blockquote>
<div>
<blockquote><p>The query optimizer was using an old query plan from the query cache.</p></blockquote>
</div>
<div></div>
<div>
<div>
<p><b>Now, this is known as a Parameter Sniffing issue. </b></p>
</div>
<div>
<p>For you guys, this is very important to know why this is happening.</p>
</div>
<div>
<p>The Query optimizer does not always choose the old query plan, but many times due to traffic or load, query optimizer chooses a plan from the query cache.</p>
</div>
<div>
<p>As a DBA, you should monitor all indexes and its usages to identify this kind of problem.</p>
</div>
<p><b>SQL Server Solution for Parameter Sniffing :</b></p>
</div>
<div></div>
<div>
<p><strong>Three options for this solution:</strong></p>
</div>
<ul>
<li>OPTION 1: RECOMPILING</li>
<li>OPTION 2: OPTIMIZE FOR VALUE</li>
<li>OPTION 3: OPTIMIZE FOR UNKNOWN</li>
<li></li>
</ul>
<div>
<p><b>OPTION 1 : RECOMPILING</b></p>
<p>RECOMPILING,  means forcing SQL Server to recompile stored procedure or statement at every execution. You can set recompile hint on stored procedure or statement.</p>
<p>Sample code for Stored procedure and statements.</p>
</div>
<pre class="crayon-plain-tag">/*Stored Procedure with Recomplie option*/
CREATE PROCEDURE dbo.usp_gettestdata @testid INT
WITH RECOMPILE
AS

/*Select statement with Recomplie option*/
SELECT * FROM dbo.tbl_testdata
OPTION (RECOMPILE);</pre>
<div>
<p><b>OPTION 2 : OPTIMIZE FOR VALUE</b></p>
</div>
<div>
<p>As a DBA, you can find a particular set of values which require executing under the best execution plan so you can set OPTIMIZE for a hint for this value. But values and data rapidly change in the database system so this only for those values or data which are not changing frequently.</p>
<p>Sample statement with OPTIMIZE for Hint:</p><pre class="crayon-plain-tag">CREATE PROCEDURE dbo.usp_EmployeeData
 @EmpID INT
AS
SELECT EmployeName, EmployeeDOB, EmployeeClass
FROM dbo.tbl_EmployeeDetails
WHERE EmpID= @EmpID
OPTION (OPTIMIZE FOR (@EmpID=26));</pre><p>
</p></div>
<p><b>OPTION 3 : OPTIMIZE FOR VALUE</b></p>
<p>Basically, you can set UNKNOWN hint for the query optimizer to create a generic execution plan base on statistical data. This may be again generated parameter sniffing issue, but still, you can use this for infrequently running queries.<br />
<pre class="crayon-plain-tag">CREATE PROCEDURE dbo.usp_EmployeeData
 @EmpID INT
AS
SELECT EmployeName, EmployeeDOB, EmployeeClass
FROM dbo.tbl_EmployeeDetails
WHERE EmpID= @EmpID
OPTION (OPTIMIZE FOR UNKNOWN);</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/05/sql-server-parameter-sniffing/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
