<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Journey to SQL Authority with Pinal Dave &#187; SQL Optimization</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 01:30:19 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Journey to SQL Authority with Pinal Dave &#187; SQL Optimization</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
			<item>
		<title>SQL SERVER &#8211; 2005 2008 &#8211; Backup, Integrity Check and Index Optimization By Ola Hallengren</title>
		<link>http://blog.sqlauthority.com/2009/11/17/sql-server-2005-2008-backup-integrity-check-and-index-optimization-by-ola-hallengren/</link>
		<comments>http://blog.sqlauthority.com/2009/11/17/sql-server-2005-2008-backup-integrity-check-and-index-optimization-by-ola-hallengren/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[SQLAuthority Website Review]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7410</guid>
		<description><![CDATA[Script of Backup, Integrity Check and Index Optimization are the most important scripts for any developer. SQL Expert and true SQL enthusiast Ola Hallengren is known for his excellent scripts.
Please try it out and let me  know what you think. The documentation is available on http://ola.hallengren.com/Documentation.html and the script can be downloaded from http://ola.hallengren.com.
Here [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7410&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Script of Backup, Integrity Check and Index Optimization are the most important scripts for any developer. SQL Expert and true SQL enthusiast Ola Hallengren is known for his excellent scripts.</p>
<p style="text-align:justify;">Please try it out and let me  know what you think. The documentation is available on <a href="http://ola.hallengren.com/Documentation.html" target="_blank">http://ola.hallengren.com/Documentation.html</a> and the script can be downloaded from <a href="http://ola.hallengren.com/" target="_blank">http://ola.hallengren.com</a>.</p>
<p style="text-align:justify;">Here is brief documentation sent by Ola himself for his script in his own words.</p>
<h2 style="text-align:justify;">Backup Maintenance</h2>
<p style="text-align:justify;">I  think that most of you have experienced the error messages &#8220;BACKUP  LOG cannot be performed because there is no current database backup.&#8221;  and &#8220;Cannot perform a differential backup for database &#8220;&#8221;,  because a current database backup does not exist.&#8221;.</p>
<p style="text-align:justify;">This usually happens when you  have created a new database or when you have changed recovery model  of a database from Simple to Full.</p>
<p style="text-align:justify;">The consequence is that the  database is not getting backed up, until a full backup (or a differential  backup for the BACKUP LOG error message if that can be done) has been  performed. It will also create some noise in your monitoring system.</p>
<p style="text-align:justify;">The solution is to check if  a differential or transaction log backup can be performed before doing  the backup. This can be done by checking sys.master_files.differential_base_lsn  and sys.database_recovery_status.last_log_backup_lsn.</p>
<p style="text-align:justify;">The backup solution that I  have developed has a parameter called @ChangeBackupType.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">EXECUTE </span><span style="color:black;">dbo.DatabaseBackup </span><span style="color:#434343;">@Databases </span><span style="color:blue;">= </span><span style="color:red;">'USER_DATABASES'</span><span style="color:gray;">, </span><span style="color:#434343;">@Directory </span><span style="color:blue;">= </span><span style="color:red;">'C:\Backup'</span><span style="color:gray;">, </span><span style="color:#434343;">@BackupType </span><span style="color:blue;">= </span><span style="color:red;">'LOG'</span><span style="color:gray;">, </span><span style="color:#434343;">@ChangeBackupType </span><span style="color:blue;">= </span><span style="color:red;">'Y'</span></code></p>
<p style="text-align:justify;">If it&#8217;s a new database in Full  recovery model, the backup type for that database and job run will be  changed to full. If it&#8217;s a database that was newly changed to Full recovery  model, then the backup type for that database and job run will be changed  to differential. The next time the job runs a transaction log backup  will be performed for that database.</p>
<p style="text-align:justify;">New databases start getting  backed up quickly and no more &#8220;BACKUP LOG cannot be performed because  there is no current database backup.&#8221; and &#8220;Cannot perform  a differential backup for database &#8220;&#8221;, because a current database  backup does not exist.&#8221;.</p>
<h2 style="text-align:justify;">Index Optimization</h2>
<p style="text-align:justify;">The design idea is categorize all indexes based on their fragmentation level (High, Medium or Low) and whether there are columns with LOB (Large Object) data types. For each category you can define an action. The possible actions are to rebuild indexes online or offline, reorganize indexes, update statistics, reorganize indexes and update statistics or to do nothing.</p>
<p style="text-align:justify;">Here’s an example.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">EXECUTE </span><span style="color:black;">dbo.IndexOptimize<br />
</span><span style="color:#434343;">@Databases </span><span style="color:blue;">= </span><span style="color:red;">'USER_DATABASES'</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationHigh_LOB </span><span style="color:blue;">= </span><span style="color:red;">'INDEX_REBUILD_OFFLINE'</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationHigh_NonLOB </span><span style="color:blue;">= </span><span style="color:red;">'INDEX_REBUILD_ONLINE'</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationMedium_LOB </span><span style="color:blue;">= </span><span style="color:red;">'INDEX_REORGANIZE'</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationMedium_NonLOB </span><span style="color:blue;">= </span><span style="color:red;">'INDEX_REORGANIZE'</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationLow_LOB </span><span style="color:blue;">= </span><span style="color:red;">'NOTHING'</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationLow_NonLOB </span><span style="color:blue;">= </span><span style="color:red;">'NOTHING'</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationLevel1 </span><span style="color:blue;">= </span><span style="color:black;">5</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FragmentationLevel2 </span><span style="color:blue;">= </span><span style="color:black;">30</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@PageCountLevel </span><span style="color:blue;">= </span><span style="color:black;">1000<br />
</span></code></p>
<p style="text-align:justify;">Indexes with a fragmentation above 30% are to be rebuilt, online if possible (no LOB columns), otherwise offline (LOB columns). Indexes with a fragmentation between 5% and 30% are to be reorganized. Indexes with a fragmentation below 5% or a size below 1000 pages are not to be touched.</p>
<p style="text-align:justify;">If you are using partitioning IndexOptimize has a parameter, @PartitionLevel to do index rebuilds and reorganizations on the partition level. If you prefer to do sort operations in tempdb you can do that with the parameter @SortInTempdb and if you would like to set a fillfactor you can do that with the parameter @FillFactor.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<div id="_mcePaste" style="overflow:hidden;position:absolute;left:-10000px;top:0;width:1px;height:1px;text-align:justify;"><span style="font-family:Times New Roman;font-size:small;">Please try it out and let me  know what you think. The documentation is available on <a href="http://ola.hallengren.com/Documentation.html" target="_blank">http://ola.hallengren.com/Documentation.html</a> and the script can be downloaded from <a href="http://ola.hallengren.com/" target="_blank">http://ola.hallengren.com</a>.</span></div>
Posted in Pinal Dave, SQL, SQL Authority, SQL Documentation, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, SQLAuthority News, SQLAuthority Website Review, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7410/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7410&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/17/sql-server-2005-2008-backup-integrity-check-and-index-optimization-by-ola-hallengren/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Authority News &#8211; Training MS SQL Server 2005/2008 Query Optimization And Performance Tuning</title>
		<link>http://blog.sqlauthority.com/2009/11/10/sql-authority-news-training-ms-sql-server-20052008-query-optimization-and-performance-tuning/</link>
		<comments>http://blog.sqlauthority.com/2009/11/10/sql-authority-news-training-ms-sql-server-20052008-query-optimization-and-performance-tuning/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 01:30:30 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Training]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7357</guid>
		<description><![CDATA[This is very short note announcing details about my course details for &#8216;MS SQL Server 2005/2008 Query Optimization And Performance Tuning&#8216;.
The course is scheduled to be delivered from 19th Nov, 2009 and 21st Nov, 2009 in Pune, India.
This three day course is an intensive course designed to give attendees an in-depth look at the query [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7357&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">This is very short note announcing details about my course details for &#8216;<strong>MS SQL Server 2005/2008 Query Optimization And Performance Tuning</strong>&#8216;.</p>
<p style="text-align:justify;">The course is scheduled to be delivered from <strong><em>19th Nov, 2009 and 21st Nov, 2009</em></strong> in <em><strong>Pune, India</strong></em>.</p>
<p style="text-align:justify;">This three day course is an intensive course designed to give attendees an in-depth look at the query optimization and performance tuning concepts and methods found in SQL Server 2005/2008. This course is designed to prepare the SQL Server developers and administrators for a transition to SQL Server 2005/2008 while discussing best practices for a variety of topics.</p>
<p style="text-align:justify;"><strong>Day 1 : Strengthen the basics along with SQL Server 2005/2008 New Features</strong></p>
<p style="padding-left:30px;text-align:justify;">Module 01: Subqueries, Ranking Functions, Joins and Set Operations<br />
Module 02: Table Expressions<br />
Module 03: TOP and APPLY<br />
Module 04: SQL Server 2008 Enhancements</p>
<p style="text-align:justify;"><strong>Day 2: Query Optimization &amp; Performance Tuning 1</strong></p>
<p style="padding-left:30px;text-align:justify;">Module 05: Logical Query Processing<br />
Module 06: Query Tuning<br />
Module 07:  Introduction to the Query Processor<br />
Module 08:  Review of common query coding which causes poor performance</p>
<p style="text-align:justify;"><strong>Day 3: Query Optimization &amp; Performance Tuning 2</strong></p>
<p style="padding-left:30px;text-align:justify;">Module 09:  SQL Server Indexing and index maintenance<br />
Module 10:  Plan Guides, query hints, UDFs, and Computed Columns<br />
Module 11:  Understanding SQL Server Execution Plans<br />
Module 12: Real World Index and Optimization Tips</p>
<p style="text-align:justify;">The space are limited and I encourage you to register as soon as possible.</p>
<p style="text-align:justify;"><strong>Call to register</strong> &#8211; +91 98817 95739, +91 98508 43664, +91 99229 33319</p>
<p style="text-align:justify;"><strong>Email to register</strong> &#8211; workshop &#8220;at&#8221; seedinfotech.com</p>
<p style="text-align:justify;">If due to any reason, you can not attend this course, please call or email on details above for future schedule for the course and location.</p>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.pinaldave.com/bimg/MSQOPT.jpg"><img title="Click to Enlarge" src="http://www.pinaldave.com/bimg/MSQOPT.jpg" alt="" width="500" height="955" /></a><p class="wp-caption-text">Click to Enlarge</p></div>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, SQL Training, SQLAuthority News, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7357/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7357&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/10/sql-authority-news-training-ms-sql-server-20052008-query-optimization-and-performance-tuning/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/MSQOPT.jpg" medium="image">
			<media:title type="html">Click to Enlarge</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Removing Key Lookup &#8211; Seek Predicate &#8211; Predicate &#8211; An Interesting Observation Related to Datatypes</title>
		<link>http://blog.sqlauthority.com/2009/11/09/sql-server-removing-key-lookup-seek-predicate-predicate-an-interesting-observation-related-to-datatypes/</link>
		<comments>http://blog.sqlauthority.com/2009/11/09/sql-server-removing-key-lookup-seek-predicate-predicate-an-interesting-observation-related-to-datatypes/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 01:30:12 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7088</guid>
		<description><![CDATA[Recently, I have been working on Query Optimization project. While working on it, I found the following interesting observation. This entire concept may appear very simple, but if you are working in the area of query optimization and server tuning, you will find such useful hints.
Before we start, let us understand the difference between Seek [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7088&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Recently, I have been working on Query Optimization project. While working on it, I found the following interesting observation. This entire concept may appear very simple, but if you are working in the area of query optimization and server tuning, you will find such useful hints.</p>
<p style="text-align:justify;">Before we start, let us understand the difference between Seek Predicate and Predicate. Seek Predicate is the operation that describes the b-tree portion of the Seek. Predicate is the operation that describes the additional filter using non-key columns. Based on the description, it is very clear that Seek Predicate is better than Predicate as it searches indexes whereas in Predicate, the search is on non-key columns – which implies that the search is on the data in page files itself.</p>
<p style="text-align:justify;">Earlier, I had posted regarding how to remove key lookup and bookmark lookup in the following:</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup – Part 2</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/12/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-3" target="_blank">SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup – Remove Key Lookup &#8211; Part 3</a></p>
<p style="text-align:justify;">Let us see an example where we will remove the bookmark lookup first using the covering index. On removing the bookmark lookup, it resulted in Index Scan, which is not good for performance. When Index Scan is converted to Index Seek, it provides  a significant improvement in performance.</p>
<p style="text-align:justify;">Run following SELECT, which is based on the database AdventureWorks.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- CTRL + M Enforces Key Lookup<br />
-- Try 1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:black;">14417807<br />
GO</span></code></p>
<p style="text-align:justify;">Let us check the execution plan for the same. The execution plan consists of the key lookup because there are columns that we are trying to retrieve in SELECT as well in WHERE clause.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseek1.jpg" alt="" width="500" height="217" /></p>
<p style="text-align:justify;">Let us create a covering index on this table HumanResources.Employee.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Create Non clustered Index<br />
</span><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_HumanResources_Employee_Example] </span><span style="color:blue;">ON </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">ASC</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">After creating above index, let us run the same SELECT statement again.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- CTRL + M Removes Key Lookup, but it still enforces Index Scan<br />
-- Try 2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:black;">14417807<br />
GO</span></code></p>
<p style="text-align:justify;">Due to non-clustered index, Key Lookup is removed along with Nested Loops but <em>Index Scan</em> still remains, and this is not good for performance.<span style="color:black;"> </span></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseek2.jpg" alt="" width="500" height="131" /></p>
<p style="text-align:justify;">I tried to remove the scan, but my attempts were unsuccessful. I finally looked at the datatype of the NationalIDNumber. All this time, I was assuming that this datatype is INT, but on a careful check, I found that the datatype of NationalIDNumber is nvarchar(15).</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseekdt.jpg" alt="" width="327" height="175" /></p>
<p style="text-align:justify;">In the SELECT statement, we were comparing the datatype of NVARCHAR to INT, and this was forcing the <strong>predicate </strong>operation while executing the query.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseekpre.jpg" alt="" width="492" height="668" /></p>
<p style="text-align:justify;">As discussed earlier, due to predicate operation, there has to be explicit conversion on the side of NationalIDNumber, which forces the query optimizer to not use the index and instead it has to scan the complete data in table to get the necessary data. This is not the desired solution. Index Scan reduces performance. The reason for this conversion is because I am using INT value in WHERE clause instead of NVARCHAR.</p>
<p style="text-align:justify;">I changed my WHERE clause and passed STRING as the parameter instead of INT.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- CTRL + M Removes Key Lookup and it enforces Index Seek<br />
-- Try 3<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:red;">'14417807'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">After running the query with the changed WHERE clause, the Index Scan is now converted into Index Seek.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseek3.jpg" alt="" width="500" height="145" /></p>
<p style="text-align:justify;">Index Seek is definitely the most optimal solution in this particular scenario.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseekpre1.jpg" alt="" width="510" height="584" /></p>
<p style="text-align:justify;">When the detail execution plan was checked, I found the following two notable points. First, the predicate is converted to seek predicate, which is the reason for performance improvement, as described earlier. Instead of scanning data in the table, Index Seek is performed. Second, as the datatype of the NationalIDNumber is NVARCHAR and the parameters are passed as VARCHAR, the conversion happens on the parameters instead of NationalIDNumber column, and this forces Index Scan to Index Seek.</p>
<p style="text-align:justify;">If we pass the parameter as NVARCHAR instead of VARCHAR, the execution plan will remain the same, but CONVERT_IMPLICIT will not be required any more. Let us run the following query, which has NVARCHAR as the parameter.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- CTRL + M Removes Key Lookup and it enforces Index Seek and no CONVERT_IMPLICIT<br />
-- Try 4<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:red;">N'14417807'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">The execution plan of the above query is very similar to that in which we had passed the parameter as VARCHAR.</p>
<p style="text-align:justify;"><img src="http://www.pinaldave.com/bimg/klseek3.jpg" alt="" width="500" height="145" /></p>
<p style="text-align:justify;">Now let us check the execution plan for the same.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseekpre2.jpg" alt="" width="502" height="675" /></p>
<p style="text-align:justify;">In the WHERE condition, the operators we have on both the sides of &#8220;=&#8221; are of NVARCHAR. NationalIDNumber and parameter passed – both are NVARCHAR, which has removed CONVERT_IMPLICIT. However, there are no changes in the execution plan.</p>
<p style="text-align:justify;"><strong>In summary, when Key Lookup is removed and index seek replaces index scan, the performance is tuned up. </strong>Let us quickly compare the execution plan of the above four options. I have included the complete code here for easy reference.</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- CTRL + M Enforces Key Lookup<br />
-- Try 1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:black;">14417807<br />
GO<br />
</span><span style="color:green;">-- Create Non clustered Index<br />
</span><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_HumanResources_Employee_Example] </span><span style="color:blue;">ON </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">ASC</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:green;">--WAITFOR DELAY '00:00:30'<br />
-- CTRL + M Removes Key Lookup, but it still enforces Index Scan<br />
-- Try 2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:black;">14417807<br />
GO<br />
</span><span style="color:green;">-- CTRL + M Removes Key Lookup and it enforces Index Seek<br />
-- Try 3<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:red;">'14417807'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- CTRL + M Removes Key Lookup and it enforces Index Seek and no CONVERT_IMPLICIT<br />
-- Try 4<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">NationalIDNumber</span><span style="color:gray;">, </span><span style="color:black;">HireDate</span><span style="color:gray;">, </span><span style="color:black;">MaritalStatus<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">NationalIDNumber </span><span style="color:blue;">= </span><span style="color:red;">N'14417807'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* What is the reason for difference between Try 2 and Try 3?<br />
Check the exeuction plan<br />
*/<br />
-- Drop Index<br />
</span><span style="color:blue;">DROP INDEX </span><span style="color:black;">[IX_HumanResources_Employee_Example] </span><span style="color:blue;">ON </span><span style="color:black;">HumanResources.Employee </span><span style="color:blue;">WITH </span><span style="color:gray;">( </span><span style="color:black;">ONLINE </span><span style="color:blue;">= OFF </span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Let us look at the execution plan.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/klseek.jpg" alt="" width="500" height="521" /></p>
<p style="text-align:justify;">Summary points:</p>
<ul style="text-align:justify;">
<li>In      general, Index Seek is better than Index Scan (I am not talking about it      depends conditions)</li>
<li>Understand      Predicates and Seek Predicates and see if you have only Seek Predicates.</li>
<li>In      case of key lookup or bookmark lookup, see if you can create a covering      index or included column index.</li>
<li>Use      the datatype wisely even though there is no change in the resultset.</li>
</ul>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7088/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7088/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7088/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7088/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7088/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7088/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7088/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7088/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7088/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7088/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7088&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/09/sql-server-removing-key-lookup-seek-predicate-predicate-an-interesting-observation-related-to-datatypes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/klseek1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseek2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseekdt.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseekpre.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseek3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseekpre1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseek3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseekpre2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/klseek.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Tuning the Performance of Change Data Capture in SQL Server 2008</title>
		<link>http://blog.sqlauthority.com/2009/10/28/sql-server-tuning-the-performance-of-change-data-capture-in-sql-server-2008/</link>
		<comments>http://blog.sqlauthority.com/2009/10/28/sql-server-tuning-the-performance-of-change-data-capture-in-sql-server-2008/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 01:30:59 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL White Papers]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7062</guid>
		<description><![CDATA[Change data capture (CDC) is a new feature in SQL Server 2008 designed to capture insert, update, merge, and delete activities applied to SQL Server tables and to avail those changes in an easy-to-understand format.
Conventionally, detecting changes in a source database to transfer these changes to a data warehouse required any of the following:

Special  [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7062&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><strong>Change data capture (CDC)</strong> is a new feature in SQL Server 2008 designed to capture insert, update, merge, and delete activities applied to SQL Server tables and to avail those changes in an easy-to-understand format.</p>
<p style="text-align:justify;">Conventionally, detecting changes in a source database to transfer these changes to a data warehouse required any of the following:</p>
<ul style="text-align:justify;">
<li>Special      columns in the source tables (time stamps, row versions).</li>
<li>Triggers      that capture changes.</li>
<li>Comparison      of the source and the destination systems.</li>
</ul>
<p style="text-align:justify;">The above methods can have significant disadvantages: special columns require a change in the source database schema, and in many cases, a change in the application logic. Triggers must be implemented manually, and this can lead to significant additional overhead on DML commands to the source system. Comparing the source and target databases can also place a heavy load on both systems.</p>
<p style="text-align:justify;">CDC offers a win-win alternative that allows the transfer of changes without forcing the DBA to either place a heavy load on the server or add the above change-detection steps. CDC enables the capture of changes by asynchronously reading the transaction log of the source database and then recording the changes in those logs in special change tables for uploading later into the target database. Therefore, no comparison between the source and target systems is needed for changes.</p>
<p style="text-align:justify;"><strong><a href="http://technet.microsoft.com/en-us/library/dd266396.aspx" target="_blank">Read white paper of Tuning the Performance of Change Data Capture in SQL Server 2008</a></strong></p>
<p style="text-align:justify;"><strong> </strong><strong><a href="http://www.pinaldave.com/best-sql-server-download.cfm?Download=cdc" target="_blank">Download Script of Change Data Capture (CDC)</a></strong></p>
<p style="text-align:justify;"><strong><a href="http://www.simple-talk.com/sql/learn-sql-server/introduction-to-change-data-capture-%28cdc%29-in-sql-server-2008/" target="_blank">Introduction to Change Data Capture (CDC)</a></strong></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in SQL, SQL Authority, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, SQL White Papers, SQLAuthority News, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7062/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7062&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/28/sql-server-tuning-the-performance-of-change-data-capture-in-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQLAuthority News &#8211; PASS 2009 Sessions on Query Optimization and Performance Tuning</title>
		<link>http://blog.sqlauthority.com/2009/10/20/sqlauthority-news-pass-2009-sessions-on-query-optimization-and-performance-tuning/</link>
		<comments>http://blog.sqlauthority.com/2009/10/20/sqlauthority-news-pass-2009-sessions-on-query-optimization-and-performance-tuning/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 01:30:56 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority Author Visit]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[SQL PASS]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7169</guid>
		<description><![CDATA[PASS Summit 2009 is now only 10 days away and I am very excited for the same. I can not wait to attend the summit as this is the most awaited conference of SQL Server in world. Everybody will be there and there will be something for everybody. My core expertise is in Query Optimization [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7169&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/" target="_blank">PASS Summit 2009</a> is now only 10 days away and I am very excited for the same. I can not wait to attend the summit as this is the most awaited conference of SQL Server in world. Everybody will be there and there will be something for everybody. My core expertise is in Query Optimization and Performance Tuning area, and when I see the list of PASS session on the subject, I am totally speechless. There are so many great speaker at PASS who are there to talk on the subject.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/2009PASS_Signature02.gif" alt="" width="400" height="140" /></p>
<p style="text-align:justify;">It is absolutely not possible to attend all of them many of them are running parallel to each other. In any case, I strongly suggest if you are the expert who try till last bit to optimize the query and performance, I expect you to attend many of the following session. There is always option to get recorded DVD for the sessions afterwords.</p>
<p style="text-align:justify;">You can read my article here regarding <strong><a href="http://blog.sqlauthority.com/2009/09/11/sqlauthority-news-why-i-am-going-to-attend-pass-summit-unite-2009-seattle/" target="_blank">Why I am Going to Attend PASS Summit Unite 2009- Seattle</a></strong>.</p>
<p style="text-align:justify;">Here is my list of the sessions related to performance tuning and query optimization.</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/SpotlightSessions/MakingEffectiveUseofthePlanCache.aspx" target="_blank">Making Effective Use of the Plan Cache</a><br />
Greg Low (Solid Q Australia)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/SpotlightSessions/CapturingandAnalyzingFileWaitStats.aspx" target="_blank">Capturing and Analyzing File &amp; Wait Stats</a><br />
Andrew Kelly (Solid Quality Mentors)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/SpotlightSessions/TSQLTipsTricks.aspx" target="_blank">T-SQL Tips &amp; Tricks</a><br />
Itzik Ben-Gan (Solid Quality Mentors)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/SpotlightSessions/BestPracticesforWorkingWithExecutionPlans.aspx" target="_blank">Best Practices for Working With Execution Plans</a><br />
Grant Fritchey (FM Global)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/DMVsasaShortcuttoProcedureTuning.aspx" target="_blank">DMV&#8217;s as a Shortcut to Procedure Tuning</a><br />
Grant Fritchey (FM Global)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/SpotlightSessions/Liesdamnedliesandstatistics.aspx" target="_blank">Lies, damned lies and statistics</a><br />
Gail Shaw (XpertEase)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/InsightintoIndexes.aspx" target="_blank">Insight into Indexes</a><br />
Gail Shaw (XpertEase)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/EnhanceyourTSQLtoperformbetter.aspx" target="_blank">Enhance your T-SQL to perform better</a><br />
Joe Webb (WebbTech Solutions, LLC)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/SpotlightSessions/InsidetheManagementDataWarehouse.aspx" target="_blank">Exploring Index Internals</a><br />
Kalen Delaney (SQLearning)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/SuperBowlSuperLoadALookatPerformance.aspx" target="_blank">Super Bowl, Super Load &#8211; A Look at Performance Tuning for VLDB&#8217;&#8217;s</a><br />
Michelle Ufford (GoDaddy)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/OptimizingSQLServer2008ApplicationsusingTabl.aspx" target="_blank">Optimizing SQL Server 2008 Applications using Table Valued Parameters, XML and MERGE</a><br />
Tobias Ternstrom (Microsoft Corp.)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/SQLServerExecutionPlansFromCompilationTo.aspx" target="_blank">SQL Server Execution Plans From Compilation To Caching To Reuse</a><br />
Maciej Pilecki (Project Botticelli Ltd.)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/UsingSQLServer2008forPerformanceTuning.aspx" target="_blank">Using SQL Server 2008 for Performance Tuning</a><br />
Buck Woody (Microsoft Corp.)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/SQLCATDesigningHighPerformanceIOforSQLSer.aspx" target="_blank">SQLCAT: Designing High Performance I/O for SQL Server (90 Mins)</a><br />
Thomas Kejser (Microsoft Corp.)<br />
Mike Ruthruff (Microsoft SQL CAT)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/SpotlightSessions/SQLServer20052008PerformanceTuningandOpti.aspx" target="_blank">SQL Server 2005 / 2008 Performance Tuning and Optimization Techniques (90 Mins)</a><br />
David Pless (Microsoft CSS)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/DrDMVHowtoUseDynamicManagementViews.aspx" target="_blank">Dr. DMV: How to Use Dynamic Management Views to Monitor and Diagnose Performance Issues With High Volume OLTP Workloads</a><br />
Glenn Berry (NewsGator Technologies)</p>
<p style="text-align:justify;"><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/HowtheQueryOptimizerWorks.aspx" target="_blank">How the Query Optimizer Works</a><br />
Ben Nevarez (AIG)</p>
<p style="text-align:justify;"><strong>I will personally try to attend many of the above listed sessions. Here are two sessions not from above list, which I will be sure attending. You are encouraged to meet me and attend this two rock star sessions.</strong></p>
<p style="text-align:justify;"><strong><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/OvercomingSSISDeploymentandConfigurationChall.aspx" target="_blank">Overcoming SSIS Deployment and Configuration Challenges</a></strong><br />
Rushabh Mehta (Solid Quality Learning)</p>
<p style="text-align:justify;"><strong><a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/SQLServer2008CreatepowerfulXMLSchema.aspx" target="_blank">SQL Server 2008 &#8211; Create powerful XML Schema collections to validate your XML documents</a></strong><br />
Jacob Sebastian (beyondrelational.com)</p>
<p style="text-align:justify;">Let me know what sessions you will be sure attending by leaving your comment here. It would be good idea to create the list of the session which one is confident about attending.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Best Practices, Database, MVP, SQL, SQL Authority, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, SQLAuthority Author Visit, SQLAuthority News, T SQL, Technology Tagged: PASS, SQL PASS <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7169/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7169&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/20/sqlauthority-news-pass-2009-sessions-on-query-optimization-and-performance-tuning/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/2009PASS_Signature02.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup &#8211; Remove Key Lookup &#8211; Part 3</title>
		<link>http://blog.sqlauthority.com/2009/10/12/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-3/</link>
		<comments>http://blog.sqlauthority.com/2009/10/12/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-3/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 01:30:16 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7057</guid>
		<description><![CDATA[Earlier I have written two different articles on the subject Remove Bookmark Lookup. This article is as part 3 of original article. Please read first two articles here before continuing reading this article.
SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup
SQL SERVER – Query Optimization – Remove [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7057&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Earlier I have written two different articles on the subject Remove Bookmark Lookup. This article is as part 3 of original article. Please read first two articles here before continuing reading this article.</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup</a></strong></p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup – Part 2</a></strong></p>
<p style="text-align:justify;">We read in above articles that we can remove bookmark lookup using covering index. Covering Index is the index, which contains all the columns used in SELECT as well in JOINs and WHERE conditions. In our example we have created clustered index first.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Create Clustered Index<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_ID] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">Based on clustered index we have created following non clustered index. Please note that we do not have to create both the index together. We can create either covering index or included column index along with it. Please note, I am suggesting to create either of it, not both of it.</p>
<p style="text-align:justify;">In earlier article I have suggested to include all columns but in reality in any non clustered index there is no need to include columns included in clustered index. All non clusteredindex automatically contains pointers to clustered index any way.</p>
<p style="text-align:justify;">We should create index described in earlier article as following.</p>
<p style="text-align:justify;"><strong>Method 1: Creating covering non-clustered index.</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_Cover] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">City</span><span style="color:gray;">, </span><span style="color:black;">FirstName</span><span style="color:black;"><br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;"><strong>Method 2: Creating included column non-lustered index.</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_Include] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">City<br />
</span><span style="color:gray;">) </span><span style="color:black;">INCLUDE </span><span style="color:gray;">(</span><span style="color:black;">FirstName</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">Let us examine the execution plan and compare the query costs and also verify that if both the index usages are forcing index seek instead of index scan.</p>
<p style="text-align:justify;"><span style="color:black;"><img src="http://www.pinaldave.com/bimg/keylook6.jpg" alt="" width="500" height="294" /></span></p>
<p style="text-align:justify;">As discussed in the example, any non clustered index does not need to include columns which are included in the clustered index.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;"><strong>Related Post:</strong></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup – Part 2</a></p>
<p><a href="http://blog.sqlauthority.com/2009/10/12/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-3" target="_blank">SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup – Remove Key Lookup &#8211; Part 3</a></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7057/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7057/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7057/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7057&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/12/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/keylook6.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Download Logical Query Processing Poster</title>
		<link>http://blog.sqlauthority.com/2009/10/10/sql-server-download-logical-query-processing-poster/</link>
		<comments>http://blog.sqlauthority.com/2009/10/10/sql-server-download-logical-query-processing-poster/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Logical Query Processing Phases]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7043</guid>
		<description><![CDATA[Download Logical Query Processing Poster
Earlier this year, I had written article on SQL SERVER – Logical Query Processing Phases – Order of Statement Execution and I had asked one question to readers. I got very good response for this question. Today, I am going to discuss about one of the errata I have made there. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7043&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><strong><a href="http://www.pinaldave.com/best-sql-server-download.cfm?Download=Logical-Query-Processing-Poster" target="_blank"><strong>Download Logical Query Processing Poster</strong></a></strong></p>
<p style="text-align:justify;">Earlier this year, I had written article on <a href="http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/" target="_blank"><strong>SQL SERVER – Logical Query Processing Phases – Order of Statement Execution</strong></a> and I had asked one question to readers. I got very good response for this question. Today, I am going to discuss about one of the errata I have made there. I had displayed the Logical Query Processing order, where I had incorrectly listed the last two operations. I have listed the operations as ORDER BY first and TOP afterwards. The fact is that TOP is always executed first and ORDER BY after that.</p>
<p style="text-align:justify;">The correct order of the Logical Query Processing Phases is as follows:</p>
<p style="text-align:justify;">1. FROM<br />
2. ON<br />
3.OUTER<br />
4.WHERE<br />
5.GROUP BY<br />
6.CUBE | ROLLUP<br />
7.HAVING<br />
8. SELECT<br />
9. DISTINCT<br />
10. TOP<br />
11. ORDER BY</p>
<p style="text-align:justify;">This logical order is properly explained by Solid Quality Mentor and SQL Expert <strong>Itzik Ben-Gan</strong> in <em>Logical Query Processing Phases</em> in his book <a href="http://www.amazon.com/dp/0735626030/" target="_blank">Inside Microsoft SQL Server 2008: T-SQL Querying</a>. This book contains the poster that explains the <em>Logical Query Processing Phases</em>.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?Download=Logical-Query-Processing-Poster" target="_blank"><strong>Download Logical Query Processing Poster</strong></a><strong> </strong></p>
<p style="text-align:justify;">I had mentioned in the original post that I will give the best explanation of the subject in my blog post with due credit. Following are the excellent comments and explanation received as comments on the blog. I am listing the selected portion from the original comments.</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-50559" target="_blank">Brian Tkatch</a></strong></p>
<p style="padding-left:30px;text-align:justify;">If the query includes an ORDER BY clause, the first expression rows, or expression percent of rows, ordered by the ORDER BY clause are returned. If the query has no ORDER BY clause, the order of the rows is arbitrary.</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-50592" target="_blank">Mike Walsh</a></strong></p>
<p style="padding-left:30px;text-align:justify;">The ORDER BY is ordering your results, it is forcing them to come back in a certain way (contrary to relational theory :) ). The TOP operator is telling SQL Server to restrict the rows returned to a certain number of rows (either explicitly or by percentage). If TOP happened first that would mean you would just be ordering by the number of rows specified in the top.</p>
<p style="padding-left:30px;text-align:justify;">It would appear as though to do the top it has to know the Order By clause and it needs to understand there is an Order By, and then filter only the top number/percent of rows according to that.</p>
<p style="padding-left:30px;text-align:justify;">The actual ordering for display or passing on to the next query happens after the TOP filter is applied (even though it looked at the order by).</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56101" target="_blank">Bob</a></strong></p>
<p style="padding-left:30px;text-align:justify;">In the case of ORDER BY and TOP, that in the absence of an ORDER BY clause, TOP will return arbitrary rows. Therefore, you can consider that ORDER BY precedes TOP when ORDER BY is specified, however, that may not be what actually happens procedurally, just what apparently happens when the results come back.</p>
<p style="text-align:justify;">I am sure that after <a href="http://www.pinaldave.com/best-sql-server-download.cfm?Download=Logical-Query-Processing-Poster" target="_blank">downloading poster</a> and reading the comments on the original article, it will clarify why TOP operators come earlier than ORDER BY in the query execution plan.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;">
Posted in Pinal Dave, SQL, SQL Authority, SQL Download, SQL Optimization, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Logical Query Processing Phases <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7043/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7043/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7043/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7043/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7043/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7043/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7043/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7043/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7043/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7043/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7043&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/10/sql-server-download-logical-query-processing-poster/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup &#8211; Remove Key Lookup &#8211; Part 2</title>
		<link>http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/</link>
		<comments>http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 01:30:53 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6967</guid>
		<description><![CDATA[This article is follow up of my previous article SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup &#8211; Remove Key Lookup. Please do read my previous article before continuing further.
I have described there two different methods to reduce query execution cost. Let us compare the performance of the SELECT statement [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6967&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">This article is follow up of my previous article <strong><a href="http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/" target="_blank">SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup &#8211; Remove Key Lookup</a></strong>. Please do read my previous article before continuing further.</p>
<p style="text-align:justify;">I have described there two different methods to reduce query execution cost. Let us compare the performance of the SELECT statement of the previous query.</p>
<p style="text-align:justify;">We have created two different indexes on the table.</p>
<p style="text-align:justify;"><strong>Method 1: Creating covering non-clustered index.</strong></p>
<p style="text-align:justify;">In this method, we will create a non-clustered index that contains the columns used in the SELECT statement along with the column used in the WHERE clause.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_Cover] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">City</span><span style="color:gray;">, </span><span style="color:black;">FirstName</span><span style="color:gray;">, </span><span style="color:black;">ID<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;"><strong>Method 2: Creating included column non-lustered index.</strong></p>
<p style="text-align:justify;">In this method, we will create a nonclustered index that includes the columns used in the SELECT statement along with the column used in the WHERE clause. Here, we will use new syntax introduced in SQL Server 2005. An index with included nonkey columns can significantly improve query performance when all the columns in the query are included in the index.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_Include] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">City<br />
</span><span style="color:gray;">) </span><span style="color:black;">INCLUDE </span><span style="color:gray;">(</span><span style="color:black;">FirstName</span><span style="color:gray;">,</span><span style="color:black;">ID</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">Let us compare the performance of our query by running the following query with two different indexes.</p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">FirstName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">OneIndex </span><span style="color:blue;">WITH </span><span style="color:gray;">(</span><span style="color:blue;">INDEX</span><span style="color:gray;">(</span><span style="color:black;">IX_OneIndex_Cover</span><span style="color:gray;">))<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">FirstName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">OneIndex </span><span style="color:blue;">WITH </span><span style="color:gray;">(</span><span style="color:blue;">INDEX</span><span style="color:gray;">(</span><span style="color:black;">IX_OneIndex_Include</span><span style="color:gray;">))<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Let us examine the execution plan and compare the query costs.</p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/keylook6.jpg" alt="" width="500" height="294" /><br />
</span></p>
<p style="text-align:justify;">In fact, in this example, the performance of both the queries is quite same. In this case, we can use either of the query and obtain the same performance.</p>
<p style="text-align:justify;">I have mentioned in my previous article that I prefer the Method 2. The reason is that method 2 has many advantages over method 1.</p>
<p style="text-align:justify;">1) Index can exceed the 900-byte limitation of the index key.</p>
<p style="text-align:justify;">2) Index can include datatypes that are not allowed as key columns – varchar(max), nvarchar(max) or XML.</p>
<p style="text-align:justify;">3) Size of the key index can be reduced, which improves the overall performance of the index operation.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;"><strong>Related Post:</strong></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup – Part 2</a></p>
<p><a href="http://blog.sqlauthority.com/2009/10/12/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-3" target="_blank">SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup – Remove Key Lookup &#8211; Part 3</a></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6967/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6967/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6967/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6967&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/keylook6.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup &#8211; Remove Key Lookup</title>
		<link>http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/</link>
		<comments>http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 01:30:49 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6960</guid>
		<description><![CDATA[Today, I would like to share one very quick tip about how to remove bookmark lookup or RID lookup. Let us first understand Bookmark lookup or RID lookup. Please note that from SQL Server 2005 SP1 onwards, Bookmark look up is known as Key look up.
When a small number of rows are requested by a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6960&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Today, I would like to share one very quick tip about how to remove bookmark lookup or RID lookup. Let us first understand Bookmark lookup or RID lookup. Please note that from SQL Server 2005 SP1 onwards, Bookmark look up is known as Key look up.</p>
<p style="text-align:justify;">When a small number of rows are requested by a query, the SQL Server optimizer will try to use a non-clustered index on the column or columns contained in the WHERE clause to retrieve the data requested by the query. If the query requests data from columns not present in the non-clustered index, SQL Server must go back to the data pages to get the data in those columns. Even if the table has a clustered index or not, the query will still have to return to the table or clustered index to retrieve the data.</p>
<p style="text-align:justify;">In the above scenario, if table has clustered index, it is called <strong>bookmark lookup</strong> (or key lookup); if the table does not have clustered index, but a non-clustered index, it is called <strong>RID lookup</strong>. This operation is very expensive. To optimize any query containing bookmark lookup or RID lookup, it should be removed from the execution plan to improve performance. There are two different ways to remove bookmark/RID lookup.</p>
<p style="text-align:justify;">Before we understand these two methods, we will create sample table without clustered index and simulate RID lookup. RID Lookup is a bookmark lookup on a heap that uses a supplied row identifier (RID).</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">tempdb<br />
GO<br />
</span><span style="color:green;">-- Create Table OneIndex with few columns<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">OneIndex </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">,<br />
</span><span style="color:black;">FirstName </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">),<br />
</span><span style="color:black;">LastName </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">),<br />
</span><span style="color:black;">City </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert One Hundred Thousand Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">OneIndex </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">,</span><span style="color:black;">FirstName</span><span style="color:gray;">,</span><span style="color:black;">LastName</span><span style="color:gray;">,</span><span style="color:black;">City</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">100000 ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">) </span><span style="color:black;">RowID</span><span style="color:gray;">,<br />
</span><span style="color:red;">'Bob'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">2 </span><span style="color:blue;">= </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'Smith'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'Brown' </span><span style="color:blue;">END</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">1000 </span><span style="color:blue;">= </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">10 </span><span style="color:blue;">= </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'New York'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">10 </span><span style="color:blue;">= </span><span style="color:black;">5 </span><span style="color:blue;">THEN </span><span style="color:red;">'San Marino'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">10 </span><span style="color:blue;">= </span><span style="color:black;">3 </span><span style="color:blue;">THEN </span><span style="color:red;">'Los Angeles'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'Houston' </span><span style="color:blue;">END<br />
FROM </span><span style="color:black;">sys.all_objects a<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">sys.all_objects b<br />
GO</span></code></p>
<p style="text-align:justify;">Now let us run following select statement and check the execution plan.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">FirstName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">OneIndex<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/keylook1.jpg" alt="" width="472" height="295" /></span></p>
<p style="text-align:justify;"><span style="color:black;">As there is no index on table, scan is performed over the table. We will create a clustered index on the table and check the execution plan once again. </span></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Create Clustered Index<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_ID] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">Now, run following select on the table once again.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">FirstName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">OneIndex<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/keylook2.jpg" alt="" width="473" height="274" /></p>
<p><!--[if gte mso 9]&gt;  800x600  &lt;![endif]--></p>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-US X-NONE X-NONE            MicrosoftInternetExplorer4              &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                             &lt;![endif]--></p>
<p style="text-align:justify;">It is clear from execution plan that as a clustered index is created on the table, table scan is now converted to clustered index scan. In either case, base table is completely scanned and there is no seek on the table.</p>
<p style="text-align:justify;">Now, let us see the WHERE clause of our table. From our basic observation, if we create an index on the column that contains the clause, a performance improvement may be obtained. Let us create non-clustered index on the table and then check the execution plan.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Create Index on Column City As that is used in where condition<br />
</span><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_City] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[City] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">After creating the non-clustered index, let us run our select statement again and check the execution plan.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">FirstName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">OneIndex<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/keylook3.jpg" alt="" width="486" height="351" /></p>
<p><!--[if gte mso 9]&gt;  800x600  &lt;![endif]--></p>
<p style="text-align:justify;">As we have an index on the WHERE clause, the SQL Server query execution engine uses the non-clustered index to retrieve data from the table. However, the columns used in the SELECT clause are still not part of the index, and to display those columns, the engine will have to go to the base table again and retrieve those columns. This particular behavior is known as bookmark lookup or key lookup.</p>
<p style="text-align:justify;">There are two different methods to resolve this issue. I have demonstrated both the methods together; however, it is recommended that you use any one of these methods for removing key lookup. I prefer Method 2.</p>
<p style="text-align:justify;"><strong>Method 1: Creating non-clustered cover index.</strong></p>
<p style="text-align:justify;">In this method, we will create non-clustered index containing the columns, which are used in the SELECT statement, along with the column which is used in the WHERE clause.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_Cover] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">City</span><span style="color:gray;">, </span><span style="color:black;">FirstName</span><span style="color:gray;">, </span><span style="color:black;">ID<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">Once the above non-clustered index, which covers all the columns in query, is created, let us run the following SELECT statement and check our execution plan.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">FirstName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">OneIndex<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">From the execution plan, we can confirm that key lookup is removed the only index seek is happening. As there is no key lookup, the SQL Server query engine does not have to go to retrieve the data from data pages and it obtains all the necessary data from index itself.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/keylook4.jpg" alt="" width="469" height="262" /></p>
<p style="text-align:justify;"><strong>Method 2: Creating an included column non-clustered index.</strong></p>
<p style="text-align:justify;">Here, we will create non-clustered index that also includes the columns, which are used in the SELECT statement, along with the column used in the WHERE clause. In this method, we will use new syntax introduced in SQL Server 2005. An index with included nonkey columns can significantly improve query performance when all columns in the query are included in the index.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_OneIndex_Include] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[OneIndex]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">City<br />
</span><span style="color:gray;">) </span><span style="color:black;">INCLUDE </span><span style="color:gray;">(</span><span style="color:black;">FirstName</span><span style="color:gray;">,</span><span style="color:black;">ID</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code></p>
<p style="text-align:justify;">From the execution plan, it is very clear that this method  also removes the key lookup as well.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/keylook5.jpg" alt="" width="467" height="263" /></p>
<p style="text-align:justify;">In summary, Key lookup, Bookmark lookup or RID lookup reduces the performance of query, and we can improve the performance of query by using included column index or cover index.</p>
<p style="text-align:justify;">I will cover few additional concepts related to the optimal method in another article.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;"><strong>Related Post:</strong></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/10/08/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-2/" target="_blank">SQL SERVER – Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup – Part 2</a></p>
<p><a href="http://blog.sqlauthority.com/2009/10/12/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup-part-3" target="_blank">SQL SERVER &#8211; Query Optimization &#8211; Remove Bookmark Lookup &#8211; Remove RID Lookup – Remove Key Lookup &#8211; Part 3</a></p>
<div id="_mcePaste" style="overflow:hidden;position:absolute;left:-10000px;top:2058px;width:1px;height:1px;text-align:justify;">
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">FirstName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">OneIndex<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Las Vegas'<br />
</span><span style="color:black;">GO</span></code></p>
</div>
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6960/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6960&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/07/sql-server-query-optimization-remove-bookmark-lookup-remove-rid-lookup-remove-key-lookup/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/keylook1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/keylook2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/keylook3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/keylook4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/keylook5.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Observation &#8211; Query Hint &#8211; FORCE ORDER</title>
		<link>http://blog.sqlauthority.com/2009/10/06/sql-server-interesting-observation-query-hint-force-order/</link>
		<comments>http://blog.sqlauthority.com/2009/10/06/sql-server-interesting-observation-query-hint-force-order/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 01:30:17 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6916</guid>
		<description><![CDATA[SQL Server never stops to amaze me. As regular readers of this blog already know that besides conducting corporate training, I work on large-scale projects on query optimizations and server tuning projects. In one of the recent projects, I have noticed that a Junior Database Developer used the query hint Force Order; when I asked [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6916&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">SQL Server never stops to amaze me. As regular readers of this blog already know that besides conducting corporate training, I work on large-scale projects on query optimizations and server tuning projects. In one of the recent projects, I have noticed that a Junior Database Developer used the query hint Force Order; when I asked for details, I found out that the basic concept was not properly understood by him.</p>
<p style="text-align:justify;">Today, let us try to understand its working and the effect of this hint. Further, we will see the extent of difference in performance created by this one query hint. I also have one interesting question for all of you as well; I will give the answer in one of my later posts.</p>
<p style="text-align:left;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:black;">CountEmployee </span><span style="color:green;">-- 290 Rows<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:black;">CountEmployeeAddress </span><span style="color:green;">-- 290 Rows<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:black;">CountEmployeeDepartmentHistory </span><span style="color:green;">-- 296 Rows<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee e<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeAddress ea </span><span style="color:blue;">ON </span><span style="color:black;">ea.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory edh </span><span style="color:blue;">ON </span><span style="color:black;">edh.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee e<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeAddress ea </span><span style="color:blue;">ON </span><span style="color:black;">ea.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory edh </span><span style="color:blue;">ON </span><span style="color:black;">edh.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
</span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:black;">FORCE </span><span style="color:blue;">ORDER</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">In above example, I have used three tables and their row count is listed as well. Employee and EmployeeAddress – both have same number rows, and EmployeeDepartmentHistory table has around 6 rows more than the other two tables. Now let us run the query without using OPTION (FORCE ORDER) and run it along with the query hint and check the execution plan. You will find a difference in the query cost.</p>
<p style="text-align:justify;">We all accept that the table with least number of rows should be listed as the base table, and the same is done here. We have two such tables with the least number of rows, which are listed as base tables. Now before we further explain this, let us see the execution plan for the same.</p>
<p style="text-align:justify;">
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/forceorder.jpg" alt="" width="500" height="404" /></p>
<p style="text-align:justify;">It is very clear from the above execution plan that when I order is forced the query cost goes high. This proves that the SQL Server has already made a good decision with regard to the optimized query plan. When plan is forced in the case of the joins more than 2 table the performance matters. Let us see the execution order of the table in both the cases.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/forceorder1.jpg" alt="" width="500" height="618" /></p>
<p style="text-align:justify;">Now, from the example, it is very clear when you force order the query, it evaluates the table Employee first and when it is not forced, it evaluates EmployeeAddress first. Even though both the tables have same number of rows, the query optimizer processes them differently and uses different types of join logic. When the order is not forced, it uses hash join; further, in case of forced order, it uses nested loop – this creates a significant difference in the query cost.</p>
<p style="text-align:justify;">The conclusion of this whole exercise is very simple.</p>
<ul style="text-align:justify;">
<li>SQL      Server Query Execution Engine is pretty smart to decide the best execution      plan with least query cost for any query.</li>
<li>Order      of the tables in any query can make a significant impact on the query.</li>
</ul>
<p style="text-align:justify;">Now the question for you: We have seen that using query hint of OPTION (FORCE ORDER) reduces the performance; give an example wherein we can use this hint to improve the performance?</p>
<p style="text-align:justify;">Please leave your comment here. I will publish the answer to this question with due credit and with my own example in a later post.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL Index, SQL Joins, SQL Optimization, SQL Performance, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6916/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6916&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/06/sql-server-interesting-observation-query-hint-force-order/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/forceorder.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/forceorder1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Observation &#8211; Index on Index View Used in Similar Query</title>
		<link>http://blog.sqlauthority.com/2009/09/24/sql-server-interesting-observation-index-on-index-view-used-in-similar-query/</link>
		<comments>http://blog.sqlauthority.com/2009/09/24/sql-server-interesting-observation-index-on-index-view-used-in-similar-query/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 01:30:36 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL White Papers]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6895</guid>
		<description><![CDATA[Recently, I was working on an optimization project for one of the large organizations. While working on one of the queries, we came across a very interesting observation. We found that there was a query on the base table and when the query was run, it used the index, which did not exist in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6895&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Recently, I was working on an optimization project for one of the large organizations. While working on one of the queries, we came across a very interesting observation. We found that there was a query on the base table and when the query was run, it used the index, which did not exist in the base table. On careful examination, we found that the query was using the index that was on another view. This was very interesting as I have personally never experienced a scenario like this. In simple words, &#8220;Query on the base table can use the index created on the indexed view of the same base table.&#8221;</p>
<p style="text-align:justify;">If you have skipped the first paragraph, I suggest you go over the first paragraph one more time and try to understand the intended meaning. According to me, this is a very interesting observation and we rarely come across such an instance. I have made very small attempt to recreate this particular behavior.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">tempdb<br />
GO<br />
</span><span style="color:green;">-- Create Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">IndexViewAggr </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert One Hundred Thousand Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">IndexViewAggr </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">100000 ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">) </span><span style="color:black;">RowID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.all_objects a<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">sys.all_objects b<br />
GO<br />
</span><span style="color:green;">-- Create View on the table<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">ViewAggr </span><span style="color:blue;">WITH </span><span style="color:black;">SCHEMABINDING<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">COUNT_BIG</span><span style="color:gray;">(*) </span><span style="color:black;">CountID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.IndexViewAggr<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:gray;">BETWEEN </span><span style="color:black;">1 </span><span style="color:gray;">AND </span><span style="color:black;">1000<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID<br />
GO<br />
</span><span style="color:green;">-- Following query is very identical to View<br />
-- Run the query on base table<br />
-- Please note that in execution plan there is Table Scan<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">COUNT_BIG</span><span style="color:gray;">(*) </span><span style="color:black;">CountID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.IndexViewAggr<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:gray;">BETWEEN </span><span style="color:black;">1 </span><span style="color:gray;">AND </span><span style="color:black;">1000<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID<br />
GO<br />
</span><span style="color:green;">-- Create Index on the View<br />
-- Please note that this index is on View and not on table<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">IndexView </span><span style="color:blue;">ON </span><span style="color:black;">ViewAggr</span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Run the query on base table again<br />
-- Please note that in execution plan there is Index Scan<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">COUNT_BIG</span><span style="color:gray;">(*) </span><span style="color:black;">CountID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.IndexViewAggr<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:gray;">BETWEEN </span><span style="color:black;">1 </span><span style="color:gray;">AND </span><span style="color:black;">1000<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID<br />
GO<br />
</span><span style="color:green;">-- Please note that there are difference in "Query Cost"<br />
-- Clean up Database<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">ViewAggr<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">IndexViewAggr<br />
GO</span></code>
</p>
<p style="text-align:justify;">Now let us quickly observe two performance comparison of two of the selects. One object is created before the creation of the index over the view, and the other object is created after the creation of the index over the view. Please note that we are talking about two different objects here. Let me explain this schematically.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/queryindex.jpg" alt="" width="500" height="513" /></p>
<p style="text-align:justify;">This is a very interesting behavior as the index created on view affects the query that runs on the base table. Let me show you the execution plan of the query before and after index creation.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexview1.jpg" alt="" width="500" height="285" /></p>
<p style="text-align:justify;">From this example, it is very clear that there is a very big difference between the query costs when the index is created on the table. In fact, there is a very simple explanation of this behavior in the white paper <a href="http://msdn.microsoft.com/en-us/library/dd171921.aspx" target="_blank">Improving Performance with SQL Server 2008 Indexed Views</a>.</p>
<p style="text-align:justify;">The reason for this behavior is that the query optimizer works when it checks for the index on view and finds that the execution plan of the query is similar to that of the view.</p>
<p style="text-align:justify;">Let me know your thoughts about this article. I do understand that this concept is slightly complicated. Further, this type of behavior is rarely encountered in real world. I hope that this article will give you a better insight to such a situation.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;">
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL White Papers, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6895/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6895&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/24/sql-server-interesting-observation-index-on-index-view-used-in-similar-query/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/queryindex.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/indexview1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Converting Stored Procedure into Table Valued Function</title>
		<link>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/</link>
		<comments>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6798</guid>
		<description><![CDATA[In one of my recent articles, I mentioned the use of Table Valued Function (TVF) instead of Stored Procedure (SP). I received a follow up email asking what type of SP can be converted into a TVF. This is indeed a very interesting question! In fact, not all the SPs qualify to be converted to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6798&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">In one of my recent articles, I mentioned the use of Table Valued Function (TVF) instead of Stored Procedure (SP). I received a follow up email asking what type of SP can be converted into a TVF. This is indeed a very interesting question! In fact, not all the SPs qualify to be converted to a TVF.</p>
<p style="text-align:justify;">Please note that I am not encouraging to convert all the SPs to TVFs. Each SPs have their own usage and need. Here, I shall discuss about the type of SP that can be converted to a TVF.</p>
<p style="text-align:justify;">First of all, you need to convert the SP that you are using to retrieve data and to insert it into any other table. Please read my original post wherein I have explained the scenario for the same. If you do not have to insert the data from the SP into another table, then there is no need to convert the SP to a TVF. This conversion will not necessarily enhance the performance.</p>
<p style="text-align:justify;">In general, SP that returns only one resultset qualifies for conversion to TVF. If there are more than one resultset, then the SP cannot be converted to TVF as the TVF always returns one resultset. Moreover, if the SP has DML statements such as DELETE, UPDATE, INSERT or MERGE, it cannot be converted to a TVF. The usage of TVF is limited to the SELECT statement only. If the SP must be executed by using EXEC, it cannot be converted to TVF.</p>
<p style="text-align:justify;">Do you know any other reason why a SP needs to be converted to a TVF?</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6798/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6798&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Server &#8211; Understanding Table Hints with Examples</title>
		<link>http://blog.sqlauthority.com/2009/06/20/sql-server-%e2%80%93understanding-table-hints-with-examples/</link>
		<comments>http://blog.sqlauthority.com/2009/06/20/sql-server-%e2%80%93understanding-table-hints-with-examples/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Query Hint]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5645</guid>
		<description><![CDATA[Today we have a very interesting subject to look at. I tried to look for help online but have not found any other documentation besides what we have from the Book Online.
Let us try to understand what are the different kinds of hints available in SQL Server and how they are helpful.
What is a Hint?
Hints [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5645&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Today we have a very interesting subject to look at. I tried to look for help online but have not found any other documentation besides what we have from the Book Online.</p>
<p style="text-align:justify;">Let us try to understand what are the different kinds of hints available in SQL Server and how they are helpful.</p>
<p style="text-align:justify;"><strong>What is a Hint?</strong></p>
<p style="text-align:justify;"><em>Hints </em> are options and strong suggestions specified for enforcement by the SQL Server query processor on DML statements. The hints override any execution plan the query optimizer might select for a query.</p>
<p style="text-align:justify;">Before we continue to explore this subject, we need to consider one very important fact and say some words of caution. SQL Server Query optimizer is a very smart tool and it makes a best selection of execution plan. Suggesting hints to the Query Optimizer should be attempted when absolutely necessary and by experienced developers who know exactly what they are doing (or in development as a way to experiment and learn).</p>
<p style="text-align:justify;">There are three different kinds of hints. Let us understand the basics of each of them separately.</p>
<h3 style="text-align:justify;"><a href="http://dotnetslackers.com/articles/sql/SQL-Server-Understanding-Table-Hints-with-examples.aspx" target="_blank">Please continue reading article here.</a></h3>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Query Hint <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5645/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5645&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/20/sql-server-%e2%80%93understanding-table-hints-with-examples/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Clustered Index on Separate Drive From Table Location</title>
		<link>http://blog.sqlauthority.com/2009/06/18/sql-server-clustered-index-on-separate-drive-from-table-location/</link>
		<comments>http://blog.sqlauthority.com/2009/06/18/sql-server-clustered-index-on-separate-drive-from-table-location/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 01:30:39 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5491</guid>
		<description><![CDATA[How to improve performance of SQL Server Queries is a common topic of discussion among many of us. Much has been said, much has been discussed. Few days back, I had an interesting discussion with one of the Junior developers regarding performance improvement of SQL Server Queries. We discussed on how by using a separate [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5491&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><strong>How to improve performance of SQL Server Queries</strong> is a common topic of discussion among many of us. Much has been said, much has been discussed. Few days back, I had an interesting discussion with one of the Junior developers regarding performance improvement of SQL Server Queries. We discussed on how by using a separate hard drive for several database objects can right away improve performance. I suggested him that non clustered index and tempdb can be created on a separate disk to improve performance.</p>
<p style="text-align:justify;">No sooner had I given my suggestion than I received a question &#8211; What will happen if we can create clustered index on a separate drive from the table on which it is built.</p>
<p style="text-align:justify;">My answer is : <strong>No! </strong>It is not possible at all.</p>
<p style="text-align:justify;">Let us first be clear about the difference between a clustered and a non clustered index.</p>
<p style="text-align:justify;"><strong>Clustered Index</strong></p>
<ul>
<li>Only 1 allowed per table</li>
<li>Physically rearranges data in the table to conform to the index constraints</li>
<li>For use on columns that are frequently searched for ranges of data</li>
<li>For use on columns with low selectivity</li>
</ul>
<p style="text-align:justify;"><strong>Non-Clustered Index</strong></p>
<ul>
<li>Up to 249 (for SQL Server 2005) and 999 (for SQL Server 2008) allowed per table</li>
<li>Creates a separate list of key values with pointers to the location of the data in the data pages</li>
<li>For use on columns that are searched for single values</li>
<li>For use on columns with high selectivity</li>
</ul>
<p style="text-align:justify;">A table devoid of primary key index is called heap, and here data is not arranged in a particular order, which gives rise to issues that adversely affect performance. Data must be stored in some kind of order. If we put clustered index on it then the order will be forced by that index and the data will be stored in that particular order.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5491/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5491&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/18/sql-server-clustered-index-on-separate-drive-from-table-location/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Query Optimizer Hint ROBUST PLAN &#8211; Question to You</title>
		<link>http://blog.sqlauthority.com/2009/06/15/sql-server-query-optimizer-hint-robust-plan-question-to-you/</link>
		<comments>http://blog.sqlauthority.com/2009/06/15/sql-server-query-optimizer-hint-robust-plan-question-to-you/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 01:30:28 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Query Hint]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5467</guid>
		<description><![CDATA[While cleaning up my bookmarks this week, I stumbled upon a very small interesting thing. I can proudly call myself a pro at finding stuffs, but after continuously hunting online I could not gather comprehensive information about this topic. I was actually looking for a practical example for Query Optimizer Hint &#8220;ROBUST PLAN&#8221;. Before I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5467&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">While cleaning up my bookmarks this week, I stumbled upon a very small interesting thing. I can proudly call myself a pro at finding stuffs, but after continuously hunting online I could not gather comprehensive information about this topic. I was actually looking for a practical example for Query Optimizer Hint &#8220;ROBUST PLAN&#8221;. Before I seek help from you, let us first try to understand what query optimizer hints is and then we will move on to the concept of &#8220;ROBUST PLAN&#8221;.</p>
<p style="text-align:justify;">To put it simply, <strong>Query hints</strong> is a T-SQL clause which on running directs T-SQL query to run in a particular manner using suggested index, joins or other logic. Query hints can force the kind of execution plan SQL Server needs. In general, SQL Server query optimizer selects the best execution plan itself to run any query; it is a good idea to just keep it at its default setting.</p>
<p style="text-align:justify;"><strong>&#8220;ROBUST PLAN&#8221;</strong> is a kind of query hint which works quite differently than other hints. It does not improve join or force any indexes to use; it just makes sure that a query does not crash due to over the limit size of row. Let me elaborate upon it. In a table, there is an outsized row that crashes any operation going over it. T-SQL has specific query hint where query engine ignores such an outsized row which will potentially crash it and stop from working. Using ROBUST PLAN, query optimizer can be forced to ignore such errors that are generated because of outsized rows. This usually does not happen with SQL Server 2005 because of its tremendous capacity of row size (2 GB). However, here we are not talking about row size beyond the limit; we are talking about SQL operation that considers the given row to be too large for this operation, even if the row is in MB or less.</p>
<p style="text-align:justify;">Now, after having thoroughly gone through the above two explanations, I request you all to find out which operation can generate this error and compel us to use ROBUST PLAN hint. I am disappointed after my extensive online search. I could not find a single example or information by anyone where they have suggested that they needed this hint. According to me, this is a useful feature which can be used wisely, as this hint reduces the performance as well in some cases. For queries where the entire execution is not working, this is at least a better option where you can expect to obtain some answer<strong>.</strong></p>
<p style="text-align:justify;"><em><strong>I am looking for a query which runs fine with option &#8220;ROBUST PLAN&#8221; but without it the query fails to produce any result and generates error.</strong></em></p>
<p style="text-align:justify;">If you know the answer, then please share it with me. I will post your answer here with due credit.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<p style="text-align:justify;"><em><strong><br />
</strong></em></p>
<div id="_mcePaste" style="overflow:hidden;position:absolute;left:-10000px;top:0;width:1px;height:1px;">While cleaning up my bookmarks this week, I stumbled upon a very small interesting thing. I can proudly call myself a pro at finding stuffs, but after continuously hunting online I could not gather comprehensive information about this topic. I was actually looking for a practical example for Query Optimizer Hint &#8220;ROBUST PLAN&#8221;. Before I seek help from you, let us first try to understand what query optimizer hints is and then we will move on to the concept of</div></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology Tagged: Query Hint <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5467/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5467&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/15/sql-server-query-optimizer-hint-robust-plan-question-to-you/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Effect of Normalization on Index and Performance</title>
		<link>http://blog.sqlauthority.com/2009/06/13/sql-server-effect-of-normalization-on-index-and-performance/</link>
		<comments>http://blog.sqlauthority.com/2009/06/13/sql-server-effect-of-normalization-on-index-and-performance/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 01:30:05 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5445</guid>
		<description><![CDATA[Of late, I have been using Twitter quite frequently, and I am gradually discovering its usefulness. I received a Direct Message (or DM in terms of twitter) asking if I can comment on the effect of normalization on Index and its performance in one twit! Now honestly speaking, this was new for me. I never [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5445&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Of late, I have been using <a href="http://twitter.com/pinaldave" target="_blank"><strong>Twitter</strong></a> quite frequently, and I am gradually discovering its usefulness. I received a Direct Message (or DM in terms of twitter) asking if I can comment on the effect of normalization on Index and its performance in one twit! Now honestly speaking, this was new for me. I never expected to be quizzed like this. If you are using Twitter, then you must be aware that one twit contains only 140 characters. I was supposed to give answer on such a big subject in just 140 letters. An interesting fact is that normalization and Index are not really closely related. The right question should have been &#8211; what is the effect of normalization on performance?</p>
<p style="text-align:justify;">In any case, let us go over my twit answer.</p>
<p style="text-align:justify;"><strong><em>&#8220;Normalization increases count of table along with clustered index which makes every single entity with its independent index improving perf.&#8221;</em></strong></p>
<p style="text-align:justify;">Within seconds of my posting the above twit I received a reply from  <a href="http://weblogs.sqlteam.com/mladenp/" target="_blank">Mladen Prajdic</a> &#8211; &#8220;which you loose on joins :)&#8221;</p>
<p style="text-align:justify;">This whole scenario is very intriguing. I will try to give a comprehensive explanation. Prior to normalization, usually data is in a large table. As it gets normalized, all the redundant information is removed and data is categorized and placed in their respective tables. It is a common practice to have PK in tables that leads to additional clustered indexes on the database. This means normalized database has more clustered indexes. As there is no redundancy, each table contains a unique entry for data. Because of this all the clustered indexes have fewer rows, which effectively increase the speed of database operations. Besides, as there is no redundancy, normalized database requires less storage and maintenance. Now when query is intricate and involves numerous tables, lots of joins are introduced. These joins effectively diminish performance. However, this loss of performance is less compared to the gain which was a result of normalization.</p>
<p style="text-align:justify;">In case you have skipped the above paragraph and straightaway reading this sentence, I urge you to go back and pay attention to the previous paragraph. This paragraph provides a good explanation to help you understand the logical backstage theory of normalization and performance relations.</p>
<p style="text-align:justify;">I requested Mladen Prajdic for his opinion in one twit and given below is his response. You can follow Mladen Prajdic on  <a href="http://twitter.com/MladenPrajdic" target="_blank">Twitter </a>here.</p>
<p style="text-align:justify;"><em>&#8220;@</em><a href="http://twitter.com/pinaldave" target="_blank"><strong><em>pinaldave </em></strong></a><em>Normalization is a data modularization concept. Indexes are data storage and retrieval concept. </em><a href="http://bit.ly/YMJJD" target="_blank"><em>http://bit.ly/YMJJD</em></a><em>&#8221; .</em></p>
<p style="text-align:justify;">Let me know what you think about this subject. If possible, participate here by giving your explanation within 140 characters. Let us see what you all can come up with. Follow me on <a href="http://twitter.com/pinaldave" target="_blank"><strong>Twitter</strong></a>.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5445/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5445&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/13/sql-server-effect-of-normalization-on-index-and-performance/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Automated Index Defragmentation Script</title>
		<link>http://blog.sqlauthority.com/2009/04/04/sql-server-automated-index-defragmentation-script/</link>
		<comments>http://blog.sqlauthority.com/2009/04/04/sql-server-automated-index-defragmentation-script/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 01:30:57 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority Website Review]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4232</guid>
		<description><![CDATA[Index Defragmentation is one of the key processes to significantly improve performance of any database. Index fragments occur when any transaction takes place in database table.  Fragmentation typically happens owing to insert, update and delete transactions. Having said that, fragmented data can produce unnecessary reads thereby reducing performance of heavy fragmented tables.
I have often been [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4232&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Index Defragmentation is one of the key processes to significantly improve performance of any database. Index fragments occur when any transaction takes place in database table.  Fragmentation typically happens owing to insert, update and delete transactions. Having said that, fragmented data can produce unnecessary reads thereby reducing performance of heavy fragmented tables.</p>
<p style="text-align:justify;">I have often been asked to share my personal Index Defragmentation Script. Well, I use Automated Index Defragmentation Script created by my friend &#8211; a SQL Expert &#8211; <strong><a href="http://sqlfool.com/about/" target="_blank">Michelle Ufford</a></strong> (a.k.a <strong>SQLFool</strong>). Michelle is a SQL Server Developer, DBA, a humble blogger, and an absolute geek! She is also the President of Eastern Iowa PASS Chapter, known as the I380 Corridor Professional Association of SQL Server. Currently, she is working with large, high volume, high performance SQL Server databases at GoDaddy.com. Her nickname might be SQLFool but honestly speaking, she is one of the best Gurus of SQL.  You can read her  <strong><a href="http://sqlfool.com/" target="_blank">blog </a></strong>here.</p>
<p style="text-align:justify;">Michelle has written an excellent article about Automated Index Defragmentation Script. I recommend this wonderful write-up to all those database developers who are searching for a good solution to improve database performance. Read her article <strong><a href="http://sqlfool.com/2009/03/automated-index-defrag-script/" target="_blank">Automated Index Defragmentation Script</a></strong> to equip yourself with better understanding on how to improve database performance.</p>
<p style="text-align:justify;">Here, I would to mention an interesting question taken from the above-mentioned article, which will clearly show how powerful this Automated Index Defragmentation Script is.</p>
<p style="text-align:justify;"><strong>How long will this Automated Index Defragmentation Script  take to run?</strong><br />
Well, it depends. I don’t necessarily recommend running it without specifying a database; at least, not unmonitored. You *can* do that, but it could take a while. For example, to run sys.dm_db_index_physical_stats for all databases and tables, totaling 2TB, took me 4.5 hours; that doesn’t even count the actual defrags.
</p>
<p style="text-align:justify;">You can <strong><a href="http://sqlfool.com/scripts/dba_indexDefrag_sp.txt" target="_blank">download the script</a></strong> from here.</p>
<p style="text-align:justify;">Please feel free to contact Michelle or me if you have any questions and doubts regarding this script.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;">
Posted in Best Practices, Database, Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLAuthority Website Review, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4232/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4232/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4232/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4232/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4232/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4232/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4232/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4232/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4232/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4232/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4232&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/04/04/sql-server-automated-index-defragmentation-script/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQLAuthority News &#8211; Ahmedabad SQL Server User Group Meeting Review &#8211; March 21, 2009</title>
		<link>http://blog.sqlauthority.com/2009/03/25/sqlauthority-news-ahmedabad-sql-server-user-group-meeting-review-march-21-2009/</link>
		<comments>http://blog.sqlauthority.com/2009/03/25/sqlauthority-news-ahmedabad-sql-server-user-group-meeting-review-march-21-2009/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 01:30:34 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL User Group]]></category>
		<category><![CDATA[SQLAuthority Author Visit]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Profiler]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4059</guid>
		<description><![CDATA[We had fun session with Ahmedabad SQL Server Usre Group last week on March 21, 2009. It was short session but one interesting one. We discussed about how query profiler works and how to find most popular query from SQL Server instance. We had also prepared Trace Template as well query which can ran to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4059&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">We had fun session with Ahmedabad SQL Server Usre Group last week on March 21, 2009. It was short session but one interesting one. We discussed about how query profiler works and how to find most popular query from SQL Server instance. We had also prepared Trace Template as well query which can ran to identify longest running query along with popular query. I received nearly 10 questions after my session and lots of time was spent answering them. The whole session was very interactive.</p>
<p style="text-align:justify;">I want to congratulate everybody who attended it, if you need my Profiler Template and Query to identify longest running query as well popular query, please let me know and I will send them to you.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/DSC03791.jpg" alt="" width="500" height="375" /></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/DSC03801.jpg" alt="" width="500" height="375" /></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Optimization, SQL Query, SQL Server, SQL Tips and Tricks, SQL User Group, SQLAuthority Author Visit, SQLAuthority News, T SQL, Technology Tagged: SQL Profiler <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4059/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4059/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4059/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4059/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4059/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4059/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4059/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4059/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4059/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4059/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4059&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/25/sqlauthority-news-ahmedabad-sql-server-user-group-meeting-review-march-21-2009/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/DSC03791.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/DSC03801.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Optimize for Ad hoc Workloads &#8211; Advance Performance Optimization</title>
		<link>http://blog.sqlauthority.com/2009/03/21/sql-server-2008-optimize-for-ad-hoc-workloads-advance-performance-optimization/</link>
		<comments>http://blog.sqlauthority.com/2009/03/21/sql-server-2008-optimize-for-ad-hoc-workloads-advance-performance-optimization/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL System Table]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=3819</guid>
		<description><![CDATA[Every batch (T-SQL, SP etc) when ran creates execution plan which is stored in system for re-use. Due to this reason large number of query plans are stored in system. However, there are plenty of plans which are only used once and have never re-used again. One time ran batch plans wastes memory and resources.
SQL [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3819&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Every batch (T-SQL, SP etc) when ran creates execution plan which is stored in system for re-use. Due to this reason large number of query plans are stored in system. However, there are plenty of plans which are only used once and have never re-used again. One time ran batch plans wastes memory and resources.</p>
<p style="text-align:justify;">SQL Server 2008 has feature of optimizing ad hoc workloads. Before we move to it, let us understand the behavior of SQL Server without optimizing ad hoc workload.</p>
<p style="text-align:justify;">Please run following script for testing. Make sure to not to run whole batch together. Just run each command separately to really see the effect of subject of article.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/bimg/CachePlan.zip" target="_blank"><strong>Download complete script of this article here.</strong></a></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Test 0 */<br />
/* Clean Cache and Buffers */<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">DROPCLEANBUFFERS<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">/* Run Adhoc Query First Time */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Shift<br />
GO<br />
</span><span style="color:green;">/* Check if Adhoc query is cached.<br />
It will return one result */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">usecounts</span><span style="color:gray;">, </span><span style="color:black;">cacheobjtype</span><span style="color:gray;">, </span><span style="color:black;">objtype</span><span style="color:gray;">, </span><span style="color:blue;">TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_cached_plans<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">plan_handle</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">usecounts </span><span style="color:gray;">&gt; </span><span style="color:black;">0 </span><span style="color:gray;">AND<br />
</span><span style="color:blue;">TEXT </span><span style="color:gray;">LIKE </span><span style="color:red;">'%SELECT * FROM HumanResources.Shift%'<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">usecounts </span><span style="color:blue;">DESC</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">Now let us check result of this script. It is clear from result that when we ran query once it cached its plan in memory. If we never run this again in future or if we have just ran as part of building longer query the cache plan of this query is just waste of memory.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cachetest0.jpg" alt="" width="553" height="516" /></p>
<p style="text-align:justify;">Let us now enable the option of optimizing ad hoc workload. This feature is available in all the versions of SQL Server 2008.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:black;"> </span><span style="color:darkred;">sp_CONFIGURE </span><span style="color:red;">'show advanced options'</span><span style="color:gray;">,</span><span style="color:black;">1<br />
</span><span style="color:blue;">RECONFIGURE<br />
</span><span style="color:black;">GO</span></code></p>
<p><span style="color:darkred;">sp_CONFIGURE </span><span style="color:red;">&#8216;optimize for ad hoc workloads&#8217;</span><span style="color:gray;">,</span><span style="color:black;">1<br />
</span><span style="color:blue;">RECONFIGURE<br />
</span><span style="color:black;">GO</span>
</p>
<p style="text-align:justify;">We will now run the code for Test 1 which is almost same as Test 0. Make sure to clean the cache and buffer before running the query to create real life scenario of live case.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Test 1 */<br />
/* Clean Cache and Buffers */<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">DROPCLEANBUFFERS<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">/* Run Adhoc Query First Time */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Shift<br />
GO<br />
</span><span style="color:green;">/* Check if Adhoc query is cached.<br />
It will not return any result */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">usecounts</span><span style="color:gray;">, </span><span style="color:black;">cacheobjtype</span><span style="color:gray;">, </span><span style="color:black;">objtype</span><span style="color:gray;">, </span><span style="color:blue;">TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_cached_plans<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">plan_handle</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">usecounts </span><span style="color:gray;">&gt; </span><span style="color:black;">0 </span><span style="color:gray;">AND<br />
</span><span style="color:blue;">TEXT </span><span style="color:gray;">LIKE </span><span style="color:red;">'SELECT * FROM HumanResources.Shift%'<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">usecounts </span><span style="color:blue;">DESC</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;">We can clear see now as we have advance option enabled we do not have query cache planed stored in database.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cachetest1.jpg" alt="" width="556" height="485" /></p>
<p style="text-align:justify;">We are interested to know now that if we run the batch more than 1 time it will cache its execution plan.  With advance option of optimizing ad hoc workload.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Test 2 */<br />
/* Clean Cache and Buffers */<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">DROPCLEANBUFFERS<br />
GO<br />
</span><span style="color:green;">/* Run Adhoc Query two  Time */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Shift<br />
GO 5<br />
</span><span style="color:green;">/* Check if Adhoc query is cached.<br />
It will return result with Adhoc Query ran two times*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">usecounts</span><span style="color:gray;">, </span><span style="color:black;">cacheobjtype</span><span style="color:gray;">, </span><span style="color:black;">objtype</span><span style="color:gray;">, </span><span style="color:blue;">TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_cached_plans<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">plan_handle</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">usecounts </span><span style="color:gray;">&gt; </span><span style="color:black;">0 </span><span style="color:gray;">AND<br />
</span><span style="color:blue;">TEXT </span><span style="color:gray;">LIKE </span><span style="color:red;">'%SELECT * FROM HumanResources.Shift%'<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">usecounts </span><span style="color:blue;">DESC</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">From our image it is quite clear that when the batch is ran for more than 1 time it caches its execution plan. This is generic behavior with or without turning on advance option.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cachetest2.jpg" alt="" width="561" height="859" /></p>
<p style="text-align:justify;">This may be very simple to see from the top but if you are using SQL Server 2008 and have millions of ad hoc query running every day you wil realize how important this feature is. This feature improves performance by relieving memory pressure by not storing the single time used compiled plans.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL System Table, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/3819/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3819&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/21/sql-server-2008-optimize-for-ad-hoc-workloads-advance-performance-optimization/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/cachetest0.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cachetest1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cachetest2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Difference Between Union vs. Union All &#8211; Optimal Performance Comparison</title>
		<link>http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-all-optimal-performance-comparison/</link>
		<comments>http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-all-optimal-performance-comparison/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 01:30:57 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=3724</guid>
		<description><![CDATA[More than a year ago I had written article SQL SERVER &#8211; Union vs. Union All &#8211; Which is better for performance? I have got many request to update this article. It is not fair to update already written article so I am rewriting it again with additional information.
UNION
The UNION command is used to select [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3724&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">More than a year ago I had written article <a href="http://blog.sqlauthority.com/2007/03/10/sql-server-union-vs-union-all-which-is-better-for-performance/" target="_blank">SQL SERVER &#8211; Union vs. Union All &#8211; Which is better for performance?</a> I have got many request to update this article. It is not fair to update already written article so I am rewriting it again with additional information.</p>
<p style="text-align:justify;"><strong>UNION</strong><br />
The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. With UNION, only distinct values are selected.</p>
<p style="text-align:justify;"><strong>UNION ALL</strong><br />
The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
</p>
<p style="text-align:justify;">The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.</p>
<p style="text-align:justify;"><strong><em>A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.</em></strong></p>
<p style="text-align:justify;">Run following script in SQL Server Management Studio to see the result between UNION ALL and UNION. <strong><a href="http://www.pinaldave.com/bimg/unionquery.zip" target="_blank">Download complete script from here.</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Declare First Table */<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Table1 </span><span style="color:blue;">TABLE </span><span style="color:gray;">(</span><span style="color:black;">ColDetail </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">))<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Second'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Third'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Fourth'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Fifth'<br />
</span><span style="color:green;">/* Declare Second Table */<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Table2 </span><span style="color:blue;">TABLE </span><span style="color:gray;">(</span><span style="color:black;">ColDetail </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">))<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table2<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Third'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Fifth'<br />
</span><span style="color:green;">/* Check the data using SELECT */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table2<br />
</span><span style="color:green;">/* UNION ALL */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table2<br />
</span><span style="color:green;">/* UNION */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table2<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;">In our example we have two tables: @Table1 and @Table2.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/union1.jpg" alt="" width="294" height="407" /></p>
<p style="text-align:justify;">Now let us run UNION ALL and UNION together and see the resultset as well as Execution Plan compared to complete set of query. You can always turn on actual execution plan using CTRL+M.</p>
<p style="text-align:justify;">We can see from the resultset of UNION ALL that it returns everything from both the table but from UNION it is very clear that only DISTINCT rows from both the table is only retrieved.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/union2.jpg" alt="" width="257" height="608" /></p>
<p style="text-align:justify;">Additionally, when comparing the execution plan of UNION ALL and UNION it is also quite clear that UNION ALL is way less expensive than UNION as it does not have DISTINCT SORT operation.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/union3.jpg" alt="" width="500" height="654" /></p>
<p style="text-align:justify;">Let me know what do you think about this article. If you have any suggestion for improvement please let me know and I will update articles according to that.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/3724/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/3724/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/3724/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/3724/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/3724/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/3724/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/3724/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/3724/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/3724/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/3724/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3724&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-all-optimal-performance-comparison/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/union1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/union2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/union3.jpg" medium="image" />
	</item>
	</channel>
</rss>