<?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>John Sansom - SQL Server DBA in the UK &#187; Query Optimisation</title>
	<atom:link href="http://www.johnsansom.com/index.php/category/performance-tuning/query-optimisation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnsansom.com</link>
	<description>SQL Server DBA Blog, with straightforward advice, quality resources and musings about SQL Server</description>
	<lastBuildDate>Fri, 10 Sep 2010 12:45:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Performing fast SQL Server delete operations</title>
		<link>http://www.johnsansom.com/index.php/2009/05/fast-sql-server-delete/</link>
		<comments>http://www.johnsansom.com/index.php/2009/05/fast-sql-server-delete/#comments</comments>
		<pubDate>Wed, 27 May 2009 13:37:46 +0000</pubDate>
		<dc:creator>John Sansom</dc:creator>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[Query Optimisation]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[fast delete]]></category>
		<category><![CDATA[query tuning]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.johnsansom.com/?p=523</guid>
		<description><![CDATA[There have been quite a few queries on the forums this past week with regard to performing fast delete operations on large tables and I wanted to share some of the possible solutions here with you. Fast ordered delete using a view To perform a fast ordered delete, take a look at the article  Fast [...]]]></description>
			<content:encoded><![CDATA[<p>There have been quite a few queries on the forums this past week with regard to performing fast delete operations on large tables and I wanted to share some of the possible solutions here with you.</p>
<h2><strong>Fast ordered delete using a view</strong></h2>
<p style="text-align: justify;">To perform a fast ordered delete, take a look at the article  <a title="Fast Ordered Delete" href="http://blogs.msdn.com/sqlcat/archive/2009/05/21/fast-ordered-delete.aspx">Fast Ordered Delete</a> provided by Kevin Stephenson of MySpace and Lubor Kollar, a member of the SQL Server Customer Advisory Team. What makes the article of particular interest is that the client is MySpace. It stands to reason then that the database in question here will be particularly large and provides a good test case for this proof of concept. The crux of the solution presented focuses on replacing a subquery within the where clause, with a view definition that identifies the records to be deleted .</p>
<p style="padding-left: 30px;"><span style="text-decoration: underline;"><em>Before:</em></span></p>
<p style="padding-left: 60px;"><em><span style="color: #993300;">delete from t1 where a in (select top (10000) a from t1 order by a);</span></em></p>
<p style="padding-left: 30px;"><span style="text-decoration: underline;"><em>After:</em></span></p>
<p style="padding-left: 60px;"><em><span style="color: #993300;">create view v1 as (select top (10000) * from t1 order by a)</span></em></p>
<p style="padding-left: 60px;"><em><span style="color: #993300;">delete from v1</span> </em></p>
<p>You can see the difference and resulting simplification in the exeuction plans produced for these queries in the article <a title="Fast Ordered Delete" href="http://blogs.msdn.com/sqlcat/archive/2009/05/21/fast-ordered-delete.aspx">Fast Ordered Delete</a>.</p>
<p>This implementation lends itself well to batching delete operations, a technique that is used to limit the transaction log space used by a delete operation and to facilitate more frequent truncation of the transaction log.</p>
<h2><strong>Fast ordered delete using a derived table</strong></h2>
<p>The previous solution  focused on utilising a view in order to simplify the execution plan produced for the query. This was achieved by referencing the given table once, rather than twice which in turn reduces the amount of I/O required. Interestingly, the same goal can be achieved by using a derived table as explained by <a title="Simon Sabin" href="http://sqlblogcasts.com/blogs/simons/about.aspx">Simon Sabin</a> in the article <a title="Delete Top X Rows Avoiding a Table Scan" href="http://sqlblogcasts.com/blogs/simons/archive/2009/05/22/DELETE-TOP-x-rows-avoiding-a-table-scan.aspx">Delete Top X rows avoiding a table scan</a></p>
<h2><strong>Working with large database tables</strong></h2>
<p style="text-align: justify;">Perhaps one further consideration that is worthy of being raised here is database table design. For example, if during the design process for a database system it becomes apparent that a significant number of delete operations will be required for a given table, it is then likely that the table in question would be a good candidate for SQL Server Partitioning and in particular <em>Sliding Window Partitioning</em>. Such an implementation would allow for the Switching in and out of partitions without adverse performance implications.</p>
<p style="text-align: justify;">For further reading, consult the Microsoft Whitepaper: <a title="How to implement an Automatic Sliding Window Partitioned Table on SQL Server 2005" href="http://technet.microsoft.com/en-us/library/aa964122(SQL.90).aspx">How to Implement an Automatic Sliding Window in a Partitioned Table on SQL Server 2005</a></p>
<p><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!--[if gte mso 10]> <mce:style><!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0cm 5.4pt 0cm 5.4pt; 	mso-para-margin:0cm; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;} --></p>
<p><!--[endif]--></p>
<h2>Can&#8217;t Find the Answer Your Looking for?</h2>
<p>Got a SQL Server question that you can&#8217;t find the answer to? Sounds like a great problem to have , let me know about it!</p>
<h2>You may also be interested in&#8230;&#8230;&#8230;&#8230;..</h2>
<ul>
<li><a title="How much memory is each SQL Server database using?" href="How much memory is each SQL Server database using?">How much memory is each SQL Server database using?</a></li>
<li><a title="How much memory is each SQL Server database using?" href="How much memory is each SQL Server database using?"></a><a title="How to identify the most costly SQL Server queries using DMV’s  Read more: http://www.johnsansom.com/index.php/2009/05/how-to-identify-the-most-costly-sql-server-queries-using-dmvs/#ixzz0utFe0oqT  Under Creative Commons License: Attribution Non-Commercial No Derivatives" href="http://www.johnsansom.com/index.php/2009/05/how-to-identify-the-most-costly-sql-server-queries-using-dmvs/">How to identify the most costly SQL Server queries using DMV’s</a></li>
<li><a title="The SQL Server Buffer Pool and Buffer Cache Hit Ratio  Read more: http://www.johnsansom.com/index.php/2009/03/the-sql-server-buffer-pool-and-buffer-cache-hit-ratio/#ixzz0utH5hkxq  Under Creative Commons License: Attribution Non-Commercial No Derivatives" href="http://www.johnsansom.com/index.php/2009/03/the-sql-server-buffer-pool-and-buffer-cache-hit-ratio/">The SQL Server Buffer Pool and Buffer Cache Hit Ratio</a></li>
<li><a title="How to Become a SQL Server DBA" href="http://www.johnsansom.com/index.php/how-to-become-a-sql-server-dba/">How to become a SQL Server DBA</a></li>
</ul>
<p><strong> </strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnsansom.com/index.php/2009/05/fast-sql-server-delete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is SQL Server Parameter Sniffing</title>
		<link>http://www.johnsansom.com/index.php/2009/05/sql-server-parameter-sniffing/</link>
		<comments>http://www.johnsansom.com/index.php/2009/05/sql-server-parameter-sniffing/#comments</comments>
		<pubDate>Wed, 06 May 2009 13:59:25 +0000</pubDate>
		<dc:creator>John Sansom</dc:creator>
				<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[Query Optimisation]]></category>
		<category><![CDATA[SQL Server Tips]]></category>
		<category><![CDATA[parameter sniffing]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.johnsansom.com/?p=480</guid>
		<description><![CDATA[Have you ever come across a stored procedure that utilises parameters and performs like a dream the majority of the time but for some reason crawls along on some other occasions? Well the chances are that this is down to SQL Server Parameter Sniffing. I was re-reading through the white paper Batch Compilation, Recompilation, and [...]]]></description>
			<content:encoded><![CDATA[<p><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!--  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal 	{mso-style-parent:""; 	margin:0cm; 	margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:12.0pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:"Times New Roman"; 	mso-ansi-language:EN-GB;} a:link, span.MsoHyperlink 	{color:blue; 	text-decoration:underline; 	text-underline:single;} a:visited, span.MsoHyperlinkFollowed 	{color:purple; 	text-decoration:underline; 	text-underline:single;} @page Section1 	{size:612.0pt 792.0pt; 	margin:72.0pt 90.0pt 72.0pt 90.0pt; 	mso-header-margin:36.0pt; 	mso-footer-margin:36.0pt; 	mso-paper-source:0;} div.Section1 	{page:Section1;} --><!--[if gte mso 10]> <mce:style><!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0cm 5.4pt 0cm 5.4pt; 	mso-para-margin:0cm; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;} --> <!--[endif]--></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB">Have you ever come across a stored procedure that utilises parameters and performs like a dream the majority of the time but for some reason crawls along on some other occasions? Well the chances are that this is down to SQL Server Parameter Sniffing.</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB">I was re-reading through the white paper <a title="Batch Sompilation,Recompilation and Plan Caching Issues in SQL Server 2005" href="http://technet.microsoft.com/en-gb/library/cc966425.aspx">Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005</a> again this week. It reminded me of the fact that Parameter Sniffing catches a lot of people out and so I wanted to share with you the details of how this issue can arise so that you may avoid it in your environments.</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></p>
<p><em></em></p>
<p class="MsoNormal"><em><span style="font-size: 10pt; font-family: Arial; font-style: normal;" lang="EN-GB"><strong>What is Parameter Sniffing?</strong></span></em></p>
<p class="MsoNormal"><em><span style="font-size: 10pt; font-family: Arial; font-style: normal;" lang="EN-GB"> </span></em></p>
<p class="MsoNormal"><em><span style="font-size: 10pt; font-family: Arial; font-style: normal;" lang="EN-GB">&#8220;Parameter sniffing&#8221; refers to a process whereby SQL Server&#8217;s execution environment &#8220;sniffs&#8221; the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word &#8220;current&#8221; refers to the parameter values present in the statement call that caused a compilation or a recompilation.</span></em><em></em></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"><strong>How does Parameter Sniffing work?</strong></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB">You can find an excellent article <a title="Parameter Sniffing Walkthrough" href="http://omnibuzz-sql.blogspot.com/2006/11/parameter-sniffing-stored-procedures.html">here </a>by <a title="Omibuzz's Blog" href="http://omnibuzz-sql.blogspot.com/">Omnibuzz </a>providing an example walk-through of the issues of Parameter Sniffing.</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"><strong>Where can I find out more?</strong></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB">For an excellent discussion on Parameter Sniffing and other similar query tuning issues take a look at the Microsoft reference <a href="http://technet.microsoft.com/en-gb/library/cc966425.aspx">Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005</a>.</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;" lang="EN-GB">I hope you find this information useful in both troubleshooting and more importantly, avoiding issues with SQL Server Parameter Sniffing. </span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnsansom.com/index.php/2009/05/sql-server-parameter-sniffing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hidden RBAR: Triangular Joins</title>
		<link>http://www.johnsansom.com/index.php/2009/01/hidden-rbar-triangular-joins/</link>
		<comments>http://www.johnsansom.com/index.php/2009/01/hidden-rbar-triangular-joins/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 09:20:21 +0000</pubDate>
		<dc:creator>John Sansom</dc:creator>
				<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[Query Optimisation]]></category>
		<category><![CDATA[SQL Server Tips]]></category>

		<guid isPermaLink="false">http://johnsansom.com/?p=82</guid>
		<description><![CDATA[No matter how good a T-SQL programmer you think you are, it is always good to revisit the basics every once in a while and to ensure that you keep your high quality coding standards in check. One of my favourite series of articles regarding good T-SQL coding practices is by Jeff Moden over at [...]]]></description>
			<content:encoded><![CDATA[<p>No matter how good a T-SQL programmer you think you are, it is always good to revisit the basics every once in a while and to ensure that you keep your high quality coding standards in check.</p>
<p>One of my favourite series of articles regarding good T-SQL coding practices is by Jeff Moden over at SQLServerCentral.com. Jeff discusses how to avoid RBAR (pronounced &#8220;ree-bar&#8221; and is a &#8220;Modenism&#8221; for &#8220;Row-By-Agonizing-Row&#8221;) using his humorous narrative tones.</p>
<p>I highly recommend you take a look at this enjoyable read. <a title="Hidden RBAR: Triangular Joins" href="http://www.sqlservercentral.com/articles/T-SQL/61539/" target="_self">Hidden RBAR: Triangular Joins</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnsansom.com/index.php/2009/01/hidden-rbar-triangular-joins/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
