<?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>SQL Server Journey with SQL Authority &#187; SQL Index</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-index/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:09:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>SQL Server Journey with SQL Authority &#187; SQL Index</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.sqlauthority.com/osd.xml" title="SQL Server Journey with SQL Authority" />
	<atom:link rel='hub' href='http://blog.sqlauthority.com/?pushpress=hub'/>
		<item>
		<title>SQL SERVER &#8211; Video &#8211; Performance Improvement in Columnstore Index</title>
		<link>http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/</link>
		<comments>http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 01:30:48 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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[Video]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15635</guid>
		<description><![CDATA[I earlier wrote an article about SQL SERVER – Fundamentals of Columnstore Index and it got very well accepted in community. However, one of the suggestion I keep on receiving for that article is that many of the reader wanted to see columnstore index in the action but they were not able to do that. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15635&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I earlier wrote an article about <a href="http://blog.sqlauthority.com/2011/10/29/sql-server-fundamentals-of-columnstore-index/" target="_blank"><strong>SQL SERVER – Fundamentals of Columnstore Index</strong></a> and it got very well accepted in community. However, one of the suggestion I keep on receiving for that article is that many of the reader wanted to see columnstore index in the action but they were not able to do that. Some of the readers did not install SQL Server 2012 or some did not have good machine to recreate the big table involved in the demo.</p>
<p style="text-align:justify;">For the same reason, I have created small video for that.</p>
<p style="text-align:justify;"><span style="text-align:center; display: block;"><a href="http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/"><img src="http://img.youtube.com/vi/C-Ay6UxMfMo/2.jpg" alt="" /></a></span></p>
<p style="text-align:justify;">I have written two more article on columstore index. Please read them as followup to the video:</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/10/30/sql-server-how-to-ignore-columnstore-index-usage-in-query/" target="_blank"><strong>SQL SERVER – How to Ignore Columnstore Index Usage in Query</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/11/06/sql-server-updating-data-in-a-columnstore-index/" target="_blank"><strong>SQL SERVER – Updating Data in A Columnstore Index</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>, <a href='http://blog.sqlauthority.com/category/video/'>Video</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15635/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15635&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/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; Updating Data in A Columnstore Index</title>
		<link>http://blog.sqlauthority.com/2011/11/06/sql-server-updating-data-in-a-columnstore-index/</link>
		<comments>http://blog.sqlauthority.com/2011/11/06/sql-server-updating-data-in-a-columnstore-index/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 01:30:26 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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=15636</guid>
		<description><![CDATA[So far I have written two articles on Columnstore Indexes, and both of them got very interesting readership. In fact, just recently I got a query on my previous article on Columnstore Index. Read the following two articles to get familiar with the Columnstore Index. They will give you a reference to the question which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15636&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">So far I have written two articles on Columnstore Indexes, and both of them got very interesting readership. In fact, just recently I got a query on my previous article on Columnstore Index.</p>
<p style="text-align:justify;">Read the following two articles to get familiar with the Columnstore Index. They will give you a reference to the question which was asked by a certain reader:</p>
<ul style="text-align:justify;">
<li><a href="http://blog.sqlauthority.com/2011/10/29/sql-server-fundamentals-of-columnstore-index/" target="_blank"><strong>SQL SERVER – Fundamentals of Columnstore Index</strong></a></li>
<li><a href="http://blog.sqlauthority.com/2011/10/30/sql-server-how-to-ignore-columnstore-index-usage-in-query/" target="_blank"><strong>SQL SERVER – How to Ignore Columnstore Index Usage in Query</strong></a></li>
</ul>
<p style="text-align:justify;">Here is the reader’s question:</p>
<p style="text-align:justify;"><em>&#8221; When I tried to update my table after creating the Columnstore index, it gives me an error. What should I do?&#8221;</em></p>
<p style="text-align:justify;">When the Columnstore index is created on the table, the table becomes Read-Only table and it does not let any insert/update/delete on the table. The basic understanding is that Columnstore Index will be created on the table that is very huge and holds lots of data. If a table is small enough, there is no need to create a Columnstore index. The regular index should just help it. The reason why Columnstore index was needed is because the table was so big that retrieving the data was taking a really, really long time. Now, updating such a huge table is always a challenge by itself.</p>
<p style="text-align:justify;">If the Columnstore Index is created on the table, and the table needs to be updated, you need to know that there are various ways to update it. The easiest way is to disable the Index and enable it. Consider the following code:</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;">-- Create New Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">[dbo].[MySalesOrderDetail]</span><span style="color:gray;">(<br />
</span><span style="color:black;">[SalesOrderID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[SalesOrderDetailID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[CarrierTrackingNumber] [nvarchar]</span><span style="color:gray;">(</span><span style="color:black;">25</span><span style="color:gray;">) NULL,<br />
</span><span style="color:black;">[OrderQty] [smallint] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[ProductID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[SpecialOfferID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[UnitPrice] [money] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[UnitPriceDiscount] [money] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[LineTotal] [numeric]</span><span style="color:gray;">(</span><span style="color:black;">38</span><span style="color:gray;">, </span><span style="color:black;">6</span><span style="color:gray;">) NOT NULL,<br />
</span><span style="color:black;">[rowguid] [uniqueidentifier] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[ModifiedDate] [datetime] </span><span style="color:gray;">NOT NULL<br />
) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:green;">-- Create clustered index<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[CL_MySalesOrderDetail] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:gray;">( </span><span style="color:black;">[SalesOrderDetailID]</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create Sample Data Table<br />
-- WARNING: This Query may run upto 2-10 minutes based on your systems resources<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">S1.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail S1<br />
GO 100<br />
</span><span style="color:green;">-- Create ColumnStore Index<br />
</span><span style="color:blue;">CREATE NONCLUSTERED </span><span style="color:black;">COLUMNSTORE </span><span style="color:blue;">INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[MySalesOrderDetail]<br />
</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Attempt to Update the table<br />
</span><span style="color:blue;">UPDATE </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">SET </span><span style="color:black;">OrderQty </span><span style="color:blue;">= </span><span style="color:black;">OrderQty </span><span style="color:gray;">+</span><span style="color:black;">1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">[SalesOrderID] </span><span style="color:blue;">= </span><span style="color:black;">43659<br />
GO<br />
</span><span style="color:green;">/* It will throw following error<br />
Msg 35330, Level 15, State 1, Line 2<br />
UPDATE statement failed because data cannot be updated in a table<br />
with a columnstore index. Consider disabling the columnstore index before<br />
issuing the UPDATE statement,<br />
then rebuilding the columnstore index after UPDATE is complete.<br />
*/<br />
</span></code></p>
<p style="text-align:justify;">A similar error also shows up for Insert/Delete function. Here is the workaround. Disable the Columnstore Index and performance update, enable the Columnstore Index:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Disable the Columnstore Index<br />
</span><span style="color:blue;">ALTER INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail] DISABLE<br />
GO<br />
</span><span style="color:green;">-- Attempt to Update the table<br />
</span><span style="color:blue;">UPDATE </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">SET </span><span style="color:black;">OrderQty </span><span style="color:blue;">= </span><span style="color:black;">OrderQty </span><span style="color:gray;">+</span><span style="color:black;">1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">[SalesOrderID] </span><span style="color:blue;">= </span><span style="color:black;">43659<br />
GO<br />
</span><span style="color:green;">-- Rebuild the Columnstore Index<br />
</span><span style="color:blue;">ALTER INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail] REBUILD<br />
GO</span></code></p>
<p style="text-align:justify;">This time it will not throw an error while the update of the table goes successfully. Let us do a cleanup of our tables using this code:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Cleanup<br />
</span><span style="color:blue;">DROP INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
GO<br />
</span><span style="color:blue;">TRUNCATE TABLE </span><span style="color:black;">dbo.MySalesOrderDetail<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">dbo.MySalesOrderDetail<br />
GO</span></code></p>
<p style="text-align:justify;">In the next post we will see how we can use Partition to update the Columnstore 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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15636/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15636/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15636/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15636/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15636/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15636/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15636/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15636/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15636/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15636/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15636/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15636/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15636/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15636/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15636&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/06/sql-server-updating-data-in-a-columnstore-index/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; How to Ignore Columnstore Index Usage in Query</title>
		<link>http://blog.sqlauthority.com/2011/10/30/sql-server-how-to-ignore-columnstore-index-usage-in-query/</link>
		<comments>http://blog.sqlauthority.com/2011/10/30/sql-server-how-to-ignore-columnstore-index-usage-in-query/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 01:30:37 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[PostADay]]></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=15371</guid>
		<description><![CDATA[Earlier I wrote about SQL SERVER – Fundamentals of Columnstore Index and very first question I received in email was as following. &#8220;We are using SQL Server 2012 CTP3 and so far so good. In our data warehouse solution we have created 1 non-clustered columnstore index on our large fact table. We have very unique situation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15371&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier I wrote about <strong><a title="Permanent Link to SQL SERVER – Fundamentals of Columnstore Index" href="http://blog.sqlauthority.com/2011/10/29/sql-server-fundamentals-of-columnstore-index/" rel="bookmark">SQL SERVER – Fundamentals of Columnstore Index</a></strong> and very first question I received in email was as following.</p>
<p style="text-align:justify;padding-left:30px;">&#8220;We are using SQL Server 2012 CTP3 and so far so good. In our data warehouse solution we have created 1 non-clustered columnstore index on our large fact table. We have very unique situation but your article did not cover it. We are running few queries on our fact table which is working very efficiently but there is one query which earlier was running very fine but after creating this non-clustered columnstore index this query is running very slow. We dropped the columnstore index and suddenly this one query is running fast but other queries which were benefited by this columnstore index it is running slow.</p>
<p style="text-align:justify;padding-left:30px;">Any workaround in this situation?&#8221;</p>
<p style="text-align:justify;">In summary the question in simple words <em><strong>&#8220;How can we ignore using columnstore index in selective queries?&#8221;</strong></em></p>
<p style="text-align:justify;">Very interesting question &#8211; you can use I can understand there may be the cases when columnstore index is not ideal and needs to be ignored the same. You can use the query hint IGNORE_NONCLUSTERED_COLUMNSTORE_INDEX to ignore the columnstore index. SQL Server Engine will use any other index which is best after ignoring the columnstore index.</p>
<p style="text-align:justify;">Here is the quick script to prove the same.</p>
<p style="text-align:justify;">We will first create sample database and then create columnstore index on the same. Once columnstore index is created we will write simple query. This query will use columnstore index. We will then show the usage of the query hint.</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;">-- Create New Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">[dbo].[MySalesOrderDetail]</span><span style="color:gray;">(<br />
</span><span style="color:black;">[SalesOrderID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[SalesOrderDetailID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[CarrierTrackingNumber] [nvarchar]</span><span style="color:gray;">(</span><span style="color:black;">25</span><span style="color:gray;">) NULL,<br />
</span><span style="color:black;">[OrderQty] [smallint] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[ProductID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[SpecialOfferID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[UnitPrice] [money] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[UnitPriceDiscount] [money] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[LineTotal] [numeric]</span><span style="color:gray;">(</span><span style="color:black;">38</span><span style="color:gray;">, </span><span style="color:black;">6</span><span style="color:gray;">) NOT NULL,<br />
</span><span style="color:black;">[rowguid] [uniqueidentifier] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[ModifiedDate] [datetime] </span><span style="color:gray;">NOT NULL<br />
) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:green;">-- Create clustered index<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[CL_MySalesOrderDetail] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:gray;">( </span><span style="color:black;">[SalesOrderDetailID]</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create Sample Data Table<br />
-- WARNING: This Query may run upto 2-10 minutes based on your systems resources<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">S1.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail S1<br />
GO 100<br />
</span><span style="color:green;">-- Create ColumnStore Index<br />
</span><span style="color:blue;">CREATE NONCLUSTERED </span><span style="color:black;">COLUMNSTORE </span><span style="color:blue;">INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[MySalesOrderDetail]<br />
</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Now we have created columnstore index so if we run following query it will use for sure the same index.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Select Table with regular Index<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ProductID</span><span style="color:gray;">, </span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">SumUnitPrice</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">AvgUnitPrice</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">SumOrderQty</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">AvgOrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ProductID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/columnstore3.jpg" alt="" width="316" height="158" /></p>
<p style="text-align:justify;">We can specify Query Hint IGNORE_NONCLUSTERED_COLUMNSTORE_INDEX as described in following query and it will not use columnstore index.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Select Table with regular Index<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ProductID</span><span style="color:gray;">, </span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">SumUnitPrice</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">AvgUnitPrice</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">SumOrderQty</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">AvgOrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ProductID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID<br />
</span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:black;">IGNORE_NONCLUSTERED_COLUMNSTORE_INDEX</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/columnstore4.jpg" alt="" width="327" height="148" /></p>
<p style="text-align:justify;">Let us clean up the database.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Cleanup<br />
</span><span style="color:blue;">DROP INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
GO<br />
</span><span style="color:blue;">TRUNCATE TABLE </span><span style="color:black;">dbo.MySalesOrderDetail<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">dbo.MySalesOrderDetail<br />
GO</span></code></p>
<p style="text-align:justify;">Again, make sure that you use hint sparingly and understanding the proper implication of the same. Make sure that you test it with and without hint and select the best option after review of your administrator. Here is the question for you &#8211; have you started to use SQL Server 2012 for your validation and development (not on production)? It will be interesting to know the answer.</p>
<p style="text-align:justify;">Reference:<strong></strong> <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15371/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15371&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/10/30/sql-server-how-to-ignore-columnstore-index-usage-in-query/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/columnstore3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/columnstore4.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Fundamentals of Columnstore Index</title>
		<link>http://blog.sqlauthority.com/2011/10/29/sql-server-fundamentals-of-columnstore-index/</link>
		<comments>http://blog.sqlauthority.com/2011/10/29/sql-server-fundamentals-of-columnstore-index/#comments</comments>
		<pubDate>Sat, 29 Oct 2011 01:30:21 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></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=15283</guid>
		<description><![CDATA[There are two kind of storage in database. Row Store and Column Store. Row store does exactly as the name suggests – stores rows of data on a page – and column store stores all the data in a column on the same page. These columns are much easier to search – instead of a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15283&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">There are two kind of storage in database. Row Store and Column Store. Row store does exactly as the name suggests – stores rows of data on a page – and column store stores all the data in a column on the same page.  These columns are much easier to search – instead of a query searching all the data in an entire row whether the data is relevant or not, column store queries need only to search much lesser number of the columns.  This means major increases in search speed and hard drive use.  Additionally, the column store indexes are heavily compressed, which translates to even greater memory and faster searches. I am sure this looks very exciting and it does not mean that you convert every single index from row store to column store index. One has to understand the proper places where to use row store or column store indexes. Let us understand in this article what is the difference in Columnstore type of index.</p>
<p style="text-align:justify;">Column store indexes are run by Microsoft’s VertiPaq technology. However, all you really need to know is that this method of storing data is columns on a single page is much faster and more efficient.  Creating a column store index is very easy, and you don’t have to learn new syntax to create them.  You just need to specify the keyword “COLUMNSTORE” and enter the data as you normally would.  Keep in mind that once you add a column store to a table, though, you cannot delete, insert or update the data – it is READ ONLY.  However, since column store will be mainly used for data warehousing, this should not be a big problem.  You can always use partitioning to avoid rebuilding the index.</p>
<p style="text-align:justify;">A columnstore index stores each column in a separate set of disk pages, rather than storing multiple rows per page as data traditionally has been stored. The difference between column store and row store approaches is illustrated below:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/columnstore.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">In case of the row store indexes multiple pages will contain multiple rows of the columns spanning across multiple pages. In case of column store indexes multiple pages will contain multiple single columns. This will lead only the columns needed to solve a query will be fetched from disk. Additionally there is good chance that there will be redundant data in a single column which will further help to compress the data, this will have positive effect on buffer hit rate as most of the data will be in memory and due to same it will not need to be retrieved.</p>
<p style="text-align:justify;">Let us see small example of how columnstore index improves the performance of the query on a large table.</p>
<p style="text-align:justify;">As a first step let us create databaseset which is large enough to show performance impact of columnstore index. The time taken to create sample database may vary on different computer based on the resources.</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;">-- Create New Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">[dbo].[MySalesOrderDetail]</span><span style="color:gray;">(<br />
</span><span style="color:black;">[SalesOrderID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[SalesOrderDetailID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[CarrierTrackingNumber] [nvarchar]</span><span style="color:gray;">(</span><span style="color:black;">25</span><span style="color:gray;">) NULL,<br />
</span><span style="color:black;">[OrderQty] [smallint] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[ProductID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[SpecialOfferID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[UnitPrice] [money] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[UnitPriceDiscount] [money] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[LineTotal] [numeric]</span><span style="color:gray;">(</span><span style="color:black;">38</span><span style="color:gray;">, </span><span style="color:black;">6</span><span style="color:gray;">) NOT NULL,<br />
</span><span style="color:black;">[rowguid] [uniqueidentifier] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[ModifiedDate] [datetime] </span><span style="color:gray;">NOT NULL<br />
) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:green;">-- Create clustered index<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[CL_MySalesOrderDetail] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:gray;">(    </span><span style="color:black;">[SalesOrderDetailID]</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create Sample Data Table<br />
-- WARNING: This Query may run upto 2-10 minutes based on your systems resources<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">S1.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail S1<br />
GO 100<br />
</span></code></p>
<p style="text-align:justify;">Now let us do quick performance test. I have kept STATISTICS IO ON for measuring how much IO following queries take. In my test first I will run query which will use regular index. We will note the IO usage of the query. After that we will create columnstore index and will measure the IO of the same.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Performance Test<br />
-- Comparing Regular Index with ColumnStore Index<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SET STATISTICS </span><span style="color:black;">IO </span><span style="color:blue;">ON<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Select Table with regular Index<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ProductID</span><span style="color:gray;">, </span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">SumUnitPrice</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">AvgUnitPrice</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">SumOrderQty</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">AvgOrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ProductID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID<br />
GO<br />
</span><span style="color:green;">-- Table 'MySalesOrderDetail'. Scan count 1, logical reads 342261, physical reads 0, read-ahead reads 0.<br />
-- Create ColumnStore Index<br />
</span><span style="color:blue;">CREATE NONCLUSTERED </span><span style="color:black;">COLUMNSTORE </span><span style="color:blue;">INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[MySalesOrderDetail]<br />
</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Select Table with Columnstore Index<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ProductID</span><span style="color:gray;">, </span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">SumUnitPrice</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">UnitPrice</span><span style="color:gray;">) </span><span style="color:black;">AvgUnitPrice</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">SumOrderQty</span><span style="color:gray;">, </span><span style="color:magenta;">AVG</span><span style="color:gray;">(</span><span style="color:black;">OrderQty</span><span style="color:gray;">) </span><span style="color:black;">AvgOrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ProductID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/columnstore2.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">It is very clear from the results that query is performance extremely fast after creating ColumnStore Index. The amount of the pages it has to read to run query is drastically reduced as the column which are needed in the query are stored in the same page and query does not have to go through every single page to read those columns.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/bimg/columnstore1.jpg"><img class="alignnone" src="http://www.pinaldave.com/bimg/columnstore1.jpg" alt="" width="500" height="194" /></a></p>
<p style="text-align:justify;">If we enable execution plan and compare we can see that column store index performance way better than regular index in <strong>this</strong> case.</p>
<p style="text-align:justify;">Let us clean up the database.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Cleanup<br />
</span><span style="color:blue;">DROP INDEX </span><span style="color:black;">[IX_MySalesOrderDetail_ColumnStore] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MySalesOrderDetail]<br />
GO<br />
</span><span style="color:blue;">TRUNCATE TABLE </span><span style="color:black;">dbo.MySalesOrderDetail<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">dbo.MySalesOrderDetail<br />
GO</span></code></p>
<p style="text-align:justify;">In future posts we will see cases where Columnstore index is not appropriate solution as well few other tricks and tips of the columnstore 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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15283/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15283&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/10/29/sql-server-fundamentals-of-columnstore-index/feed/</wfw:commentRss>
		<slash:comments>13</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/columnstore.jpg" medium="image" />

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

		<media:content url="http://www.pinaldave.com/bimg/columnstore1.jpg" medium="image" />
	</item>
		<item>
		<title>SQLAuthority News &#8211; Tomorrow Online Session &#8211; Ancient Trade of Performance Tuning &#8211; Index, Beyond Index and No Index</title>
		<link>http://blog.sqlauthority.com/2011/09/28/sqlauthority-news-tomorrow-online-session-ancient-trade-of-performance-tuning-index-beyond-index-and-no-index/</link>
		<comments>http://blog.sqlauthority.com/2011/09/28/sqlauthority-news-tomorrow-online-session-ancient-trade-of-performance-tuning-index-beyond-index-and-no-index/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 01:30:34 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14621</guid>
		<description><![CDATA[Today in few hours I am going to present on my very favorite subject of performance tuning. You can read more about this sessions over here. This presentation is based on the famous book &#8216;The Art of War&#8217; written in sixth century BC by Sun Tzu. Index is usually a favorite tool of many when [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14621&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today in few hours I am going to present on my very favorite subject of performance tuning.</p>
<p style="text-align:justify;">You can read more about this sessions over here. <img class="alignleft" style="margin:3px;" src="http://www.pinaldave.com/bimg/art-of-war-poster.jpg" alt="" width="134" height="201" /></p>
<p style="text-align:justify;"><strong></strong>This presentation is based on the famous book <strong>&#8216;The Art of War&#8217;</strong> written in sixth century BC by Sun Tzu. Index is usually a favorite tool of many when it is about performance tuning. However, Index is not everything. Performance tuning is much very deep subject and one needs to understand various aspect of the performance tuning. In today&#8217;s session I will cover performance tuning beyond indexes. I have created some real interesting demos.</p>
<h3 style="text-align:justify;">Sessions Details</h3>
<p style="text-align:justify;">Title: <em><strong>Ancient Trade of Performance Tuning &#8211; Index, Beyond Index and No Index</strong></em><br />
Timing: <em><strong>September 29, 2011 11:45am-12:45pm IST</strong></em><br />
SQL Server Indexes are double edge sword. If used properly it gives tremendous performance improvement of the server. However, there is a dark side of the Index as well, which one needs to learn before using them to improve performance of the query. In this session we will go over various concepts of the Indexes which are not common but extremely important.<br />
Location: <a href="http://www.virtualtechdays.com/agendaDay2.aspx?WT.mc_id=aff-n-in-loc--pd" target="_blank">Register for Virtual Tech Days </a></p>
<h3 style="text-align:justify;">Giveaways</h3>
<p style="text-align:justify;"><img class="alignleft" style="margin:4px;" src="http://www.pinaldave.com/bimg/j2pwait_s.jpg" alt="" width="92" height="139" />During my session I will ask a simple question. I will give away <strong>5 copies</strong> of my <strong><a href="http://bit.ly/waitstats" target="_blank">SQL Server Wait Stats</a></strong> books to five random person who will answer it correctly. If you have already have this book, I strongly suggest you attend this session as this session will take the performance tuning concepts to next level. I will make sure that I autograph and send this copies to your way.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/best-practices/'>Best Practices</a>, <a href='http://blog.sqlauthority.com/category/database/'>Database</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-news/'>SQLAuthority News</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14621/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14621&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/28/sqlauthority-news-tomorrow-online-session-ancient-trade-of-performance-tuning-index-beyond-index-and-no-index/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>

		<media:content url="http://www.pinaldave.com/bimg/art-of-war-poster.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/j2pwait_s.jpg" medium="image" />
	</item>
		<item>
		<title>SQLAuthority News &#8211; Online Session &#8211; Ancient Trade of Performance Tuning &#8211; Index, Beyond Index and No Index</title>
		<link>http://blog.sqlauthority.com/2011/09/26/sqlauthority-news-online-session-ancient-trade-of-performance-tuning-index-beyond-index-and-no-index/</link>
		<comments>http://blog.sqlauthority.com/2011/09/26/sqlauthority-news-online-session-ancient-trade-of-performance-tuning-index-beyond-index-and-no-index/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 01:30:49 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14594</guid>
		<description><![CDATA[Performance Tuning has been my favorite subject always. I love this subject the most. I personally have enjoyed every aspect of performance tuning. Quite often I have seen that when it is about performance, people end up talking about Indexes. Index for sure can help performance, but it is like secret weapon and it must [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14594&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Performance Tuning has been my favorite subject always. I love this subject the most. I personally have enjoyed every aspect of performance tuning. Quite often I have seen that when it is about performance, people end up talking about Indexes. Index for sure can help performance, but it is like secret weapon and it must be used carefully as the same thing can be dangerous. I have personally attended many sessions that are related to Indexes as well as how to identify the correct index and remove useless indexes. I always wanted Indexing presentation to bring much more than these usual topics.</p>
<h3 style="text-align:justify;">Session Inspiration</h3>
<p style="text-align:justify;"><img class="alignleft" style="margin:3px;" src="http://www.pinaldave.com/bimg/art-of-war-poster.jpg" alt="" width="158" height="236" /></p>
<p style="text-align:justify;">Driven with coming up something different around indexes, I created this presentation. This presentation is based on the famous book <strong>&#8216;The Art of War&#8217;</strong> written in sixth century BC by Sun Tzu. This book is composed of 13 chapters, each of which is devoted to one aspect of warfare, and it is said to be the definitive work on military strategies and tactics of its time, and it is still read for its military insights. I am heavily inspired by this book, and I have always treated performance tuning as a war.</p>
<p style="text-align:justify;">Here are the three subjects which I will be covering in my online presentation.</p>
<h3 style="text-align:justify;">How to defeat an opponent</h3>
<ul style="text-align:justify;">
<li>Using Weapon</li>
</ul>
<blockquote>
<ul>
<li>Carry a convenient weapon</li>
<li>Use anything as weapon</li>
</ul>
</blockquote>
<ul style="text-align:justify;">
<li>Use Counter Intelligence</li>
</ul>
<blockquote>
<ul>
<li>Use spy network</li>
<li>Use statistical data points</li>
</ul>
</blockquote>
<ul style="text-align:justify;">
<li>The Lords of War</li>
</ul>
<blockquote>
<ul>
<li>Extra-ordinary intellect</li>
<li>Supreme heroic ability</li>
</ul>
</blockquote>
<p style="text-align:justify;">I have created three demos to support the above three sections. I am very sure that this is one of the most interesting and inspiring sessions for performance tuning. Here are the details for the session.</p>
<h3 style="text-align:justify;">Sessions Details</h3>
<p style="text-align:justify;">Title: <em><strong>Ancient Trade of Performance Tuning &#8211; Index, Beyond Index and No Index</strong></em><br />
Timing: <em><strong>September 29, 2011 11:45am-12:45pm IST</strong></em><br />
SQL Server Indexes are like a double-sided sword. If used properly it gives tremendous performance improvement of the server. However, there is a dark side of the Index as well, which one needs to learn before using them to improve the performance of the query. In this session, we will go over various concepts of the Indexes, which are not common but extremely important.<br />
Location: <a href="http://www.virtualtechdays.com/agendaDay2.aspx?WT.mc_id=aff-n-in-loc--pd" target="_blank">Register for Virtual Tech Days </a></p>
<h3 style="text-align:justify;">Giveaways</h3>
<p style="text-align:justify;"><img class="alignleft" style="margin:4px;" src="http://www.pinaldave.com/bimg/j2pwait_s.jpg" alt="" width="94" height="142" /></p>
<p style="text-align:justify;">During my session, I will ask a simple question. I will give away <strong>5 copies</strong> of my <a href="http://bit.ly/waitstats" target="_blank"><strong>SQL Server Wait Stats</strong></a> books to five random people who will answer it correctly. If you already have this book, I strongly suggest you attend this session as this session will take the performance tuning concepts to next level. I will make sure that I personally autograph and send these copies to your way.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/best-practices/'>Best Practices</a>, <a href='http://blog.sqlauthority.com/category/database/'>Database</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-news/'>SQLAuthority News</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14594/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14594/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14594/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14594/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14594/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14594/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14594/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14594/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14594/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14594/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14594/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14594/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14594/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14594/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14594&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/26/sqlauthority-news-online-session-ancient-trade-of-performance-tuning-index-beyond-index-and-no-index/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/art-of-war-poster.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/j2pwait_s.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Query to Find Duplicate Indexes &#8211; Script to Find Redundant Indexes</title>
		<link>http://blog.sqlauthority.com/2011/07/13/sql-server-query-to-find-duplicate-indexes-script-to-find-redundant-indexes/</link>
		<comments>http://blog.sqlauthority.com/2011/07/13/sql-server-query-to-find-duplicate-indexes-script-to-find-redundant-indexes/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 01:30:41 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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=13564</guid>
		<description><![CDATA[Download Duplicate Index Script with Unused Index Script and Missing Index Script I was recently delivering session on Performance Tuning subject. I was asking if there is any harm having duplicate indexes. Of course, duplicate indexes are nothing but overhead on the database system. Database system has to maintain two sets of indexes when it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13564&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><strong><a href="http://www.pinaldave.com/best-sql-server-download.cfm?Download=MissingIndex_UnusedIndex" target="_blank">Download Duplicate Index Script with Unused Index Script and Missing Index Script</a></strong></p>
<p style="text-align:justify;">I was recently delivering session on Performance Tuning subject. I was asking if there is any harm having duplicate indexes. Of course, duplicate indexes are nothing but overhead on the database system. Database system has to maintain two sets of indexes when it has to do update, delete, insert on the table which has duplicate indexes. There is also a possibility that indexes are overlapped.</p>
<p style="text-align:justify;">For example,</p>
<p style="text-align:justify;"><strong>Index1</strong> have <em>Col1, Col2, Col3</em> but <strong>Index2</strong> have <em>Col1,Col2,Col3,Col4,Col5. </em>Here Index1 and Index2 are overlapping and there is no need of Index1, which should be removed.</p>
<p style="text-align:justify;">Following is the script which does the same task. You can run the script, get duplicate indexes and overlapping indexes. Carefully review each of them first and test this by deploying on your development server (not on production). If this script has any issue, do write back and I will do necessary modifications.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">MyDuplicate </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">Sch.[name] </span><span style="color:blue;">AS </span><span style="color:black;">SchemaName</span><span style="color:gray;">,<br />
</span><span style="color:black;">Obj.[name] </span><span style="color:blue;">AS </span><span style="color:black;">TableName</span><span style="color:gray;">,<br />
</span><span style="color:black;">Idx.[name] </span><span style="color:blue;">AS </span><span style="color:black;">IndexName</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col1</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col2</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">3</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col3</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">4</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col4</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">5</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col5</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">6</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col6</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">7</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col7</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col8</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">9</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col9</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">10</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col10</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">11</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col11</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">12</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col12</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">13</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col13</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">14</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col14</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">15</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col15</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">INDEX_COL</span><span style="color:gray;">(</span><span style="color:black;">Sch.[name] </span><span style="color:gray;">+ </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:black;">Obj.[name]</span><span style="color:gray;">, </span><span style="color:black;">Idx.index_id</span><span style="color:gray;">, </span><span style="color:black;">16</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Col16<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.indexes Idx<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.objects Obj </span><span style="color:blue;">ON </span><span style="color:black;">Idx.[object_id] </span><span style="color:blue;">= </span><span style="color:black;">Obj.[object_id]<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.schemas Sch </span><span style="color:blue;">ON </span><span style="color:black;">Sch.[schema_id] </span><span style="color:blue;">= </span><span style="color:black;">Obj.[schema_id]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">index_id </span><span style="color:gray;">&gt; </span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT    </span><span style="color:black;">MD1.SchemaName</span><span style="color:gray;">, </span><span style="color:black;">MD1.TableName</span><span style="color:gray;">, </span><span style="color:black;">MD1.IndexName</span><span style="color:gray;">,<br />
</span><span style="color:black;">MD2.IndexName </span><span style="color:blue;">AS </span><span style="color:black;">OverLappingIndex</span><span style="color:gray;">,<br />
</span><span style="color:black;">MD1.Col1</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col2</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col3</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col4</span><span style="color:gray;">,<br />
</span><span style="color:black;">MD1.Col5</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col6</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col7</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col8</span><span style="color:gray;">,<br />
</span><span style="color:black;">MD1.Col9</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col10</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col11</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col12</span><span style="color:gray;">,<br />
</span><span style="color:black;">MD1.Col13</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col14</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col15</span><span style="color:gray;">, </span><span style="color:black;">MD1.Col16<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">MyDuplicate MD1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">MyDuplicate MD2 </span><span style="color:blue;">ON </span><span style="color:black;">MD1.tablename </span><span style="color:blue;">= </span><span style="color:black;">MD2.tablename<br />
</span><span style="color:gray;">AND </span><span style="color:black;">MD1.indexname </span><span style="color:gray;">&lt;&gt; </span><span style="color:black;">MD2.indexname<br />
</span><span style="color:gray;">AND </span><span style="color:black;">MD1.Col1 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col1<br />
</span><span style="color:gray;">AND (</span><span style="color:black;">MD1.Col2 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col2 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col2 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col2</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col3 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col3 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col3 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col3</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col4 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col4 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col4 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col4</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col5 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col5 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col5 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col5</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col6 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col6 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col6 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col6</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col7 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col7 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col7 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col7</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col8 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col8 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col8 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col8</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col9 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col9 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col9 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col9</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col10 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col10 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col10 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col10</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col11 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col11 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col11 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col11</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col12 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col12 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col12 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col12</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col13 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col13 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col13 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col13</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col14 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col14 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col14 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col14</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col15 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col15 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col15 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col15</span><span style="color:gray;">)<br />
AND (</span><span style="color:black;">MD1.Col16 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD2.Col16 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">MD1.Col16 </span><span style="color:blue;">= </span><span style="color:black;">MD2.Col16</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">MD1.SchemaName</span><span style="color:gray;">,</span><span style="color:black;">MD1.TableName</span><span style="color:gray;">,</span><span style="color:black;">MD1.IndexName<br />
</span></code></p>
<p style="text-align:justify;">Related Posts:</p>
<p style="text-align:justify;"><strong></strong><strong><a href="http://blog.sqlauthority.com/2011/01/03/sql-server-2008-missing-index-script-download/">SQL SERVER – 2008 – Missing Index Script – Download</a></strong></p>
<p style="text-align:justify;"><strong></strong><strong></strong><strong><a href="http://blog.sqlauthority.com/2011/01/04/sql-server-2008-unused-index-script-download/">SQL SERVER – 2008 – Unused Index Script – Download</a></strong></p>
<p style="text-align:justify;">Update: Additionally, please read the <a href="http://blog.sqlauthority.com/2011/07/13/sql-server-query-to-find-duplicate-indexes-script-to-find-redundant-indexes/#comment-147850" target="_blank">comment </a>left by <a href="http://sqlskills.com" target="_blank">Paul Randal</a>, why there is more modifications required to this query.</p>
<p style="text-align:justify;">This is version one query. Please provide all your suggestions and based on the same, I will have final query posted on blog.<br />
<strong></strong></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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/13564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/13564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/13564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/13564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/13564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/13564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/13564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/13564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/13564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/13564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/13564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/13564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/13564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/13564/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13564&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/07/13/sql-server-query-to-find-duplicate-indexes-script-to-find-redundant-indexes/feed/</wfw:commentRss>
		<slash:comments>8</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; Solution &#8211; Puzzle &#8211; Statistics are not Updated but are Created Once</title>
		<link>http://blog.sqlauthority.com/2011/06/17/sql-server-solution-puzzle-statistics-are-not-updated-but-are-created-once/</link>
		<comments>http://blog.sqlauthority.com/2011/06/17/sql-server-solution-puzzle-statistics-are-not-updated-but-are-created-once/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 01:30:02 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=13228</guid>
		<description><![CDATA[Earlier I asked puzzle why statistics are not updated. Read the complete details over here: Statistics are not Updated but are Created Once In the question I have demonstrated even though statistics should have been updated after lots of insert in the table are not updated.(Read the details SQL SERVER – When are Statistics Updated – [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13228&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier I asked puzzle why statistics are not updated. Read the complete details over here: <strong></strong><strong><a title="Permanent Link to SQL SERVER – Puzzle – Statistics are not Updated but are Created Once" href="http://blog.sqlauthority.com/2011/06/02/sql-server-puzzle-statistics-are-not-updated-but-are-created-once/" rel="bookmark">Statistics are not Updated but are Created Once</a></strong></p>
<p style="text-align:justify;">In the question I have demonstrated even though statistics should have been updated after lots of insert in the table are not updated.(Read the details <strong></strong><strong><a href="http://blog.sqlauthority.com/2010/04/21/sql-server-when-are-statistics-updated-what-triggers-statistics-to-update/">SQL SERVER – When are Statistics Updated – What triggers Statistics to Update</a></strong>)</p>
<p style="text-align:justify;">In this example I have created following situation:</p>
<ul style="text-align:justify;">
<li>Create Table</li>
<li>Insert 1000 Records</li>
<li>Check the Statistics</li>
<li>Now insert 10 times more 10,000 indexes</li>
<li>Check the Statistics – it will be <strong>NOT</strong> updated</li>
<li><strong>Auto Update Statistics and Auto Create Statistics for database is TRUE</strong></li>
</ul>
<p style="text-align:justify;">Now I have requested two things in the example 1) Why this is happening? 2) How to fix this issue?</p>
<p style="text-align:justify;">I have many answers &#8211; here is the how I fixed it which has resolved the issue for me. NOTE: There are multiple answers to this problem and I will do my best to list all.</p>
<h3 style="text-align:justify;">Solution:</h3>
<ul style="text-align:justify;">
<li><strong>Create nonclustered Index on column City</strong></li>
</ul>
<p style="text-align:justify;">Here is the working example for the same. Let us understand this script and there is added explanation at the end.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Execution Plans Difference<br />
-- Estimated Execution Plan Vs Actual Execution Plan<br />
-- Create Sample Database<br />
</span><span style="color:blue;">CREATE DATABASE </span><span style="color:black;">SampleDB<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">SampleDB<br />
GO<br />
</span><span style="color:green;">-- Create Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">ExecTable </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:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">IX_ExecTable1<br />
</span><span style="color:blue;">ON </span><span style="color:black;">ExecTable </span><span style="color:gray;">(</span><span style="color:black;">City</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert One Thousand Records<br />
-- INSERT 1<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">ExecTable </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;">1000 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 </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;">20 </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;">20 </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;">20 </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;">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;">20 </span><span style="color:blue;">= </span><span style="color:black;">7 </span><span style="color:blue;">THEN </span><span style="color:red;">'La Cinega'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">13 </span><span style="color:blue;">THEN </span><span style="color:red;">'San Diego'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">17 </span><span style="color:blue;">THEN </span><span style="color:red;">'Las Vegas'<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<br />
</span><span style="color:green;">-- Display statistics of the table<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">N'ExecTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Select Statement<br />
</span><span style="color:blue;">SELECT </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<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">ExecTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City  </span><span style="color:blue;">= </span><span style="color:red;">'New York'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Display statistics of the table<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">N'ExecTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Replace your Statistics over here<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">SHOW_STATISTICS</span><span style="color:gray;">(</span><span style="color:red;">'ExecTable'</span><span style="color:gray;">, </span><span style="color:black;">IX_ExecTable1</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">--------------------------------------------------------------<br />
-- Round 2<br />
-- Insert One Thousand Records<br />
-- INSERT 2<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">ExecTable </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;">1000 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 </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;">20 </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;">20 </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;">20 </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;">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;">20 </span><span style="color:blue;">= </span><span style="color:black;">7 </span><span style="color:blue;">THEN </span><span style="color:red;">'La Cinega'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">13 </span><span style="color:blue;">THEN </span><span style="color:red;">'San Diego'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">17 </span><span style="color:blue;">THEN </span><span style="color:red;">'Las Vegas'<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<br />
</span><span style="color:green;">-- Select Statement<br />
</span><span style="color:blue;">SELECT </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<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">ExecTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City  </span><span style="color:blue;">= </span><span style="color:red;">'New York'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Display statistics of the table<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">N'ExecTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Replace your Statistics over here<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">SHOW_STATISTICS</span><span style="color:gray;">(</span><span style="color:red;">'ExecTable'</span><span style="color:gray;">, </span><span style="color:black;">IX_ExecTable1</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up Database<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">ExecTable<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">When I created non clustered index on the column city, it also created statistics on the same column with same name as index. When we populate the data in the column the index is update &#8211; resulting execution plan to be invalided &#8211; this leads to the statistics to be updated in next execution of SELECT. This behavior does not happen on Heap or column where index is auto created. If you explicitly update the index, often you can see the statistics are updated as well. You can see this is for sure happening if you follow the tell of <a href="http://www.johnsansom.com/" target="_blank">John Sansom</a>.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/06/02/sql-server-puzzle-statistics-are-not-updated-but-are-created-once/#comment-138359" target="_blank">John Sansom</a>&#8216;s suggestion:</p>
<p style="padding-left:30px;text-align:justify;">That was fun!</p>
<p style="padding-left:30px;text-align:justify;">Although the column statistics are invalidated by the time the second select statement is executed, the query is not compiled/recompiled but instead the existing query plan is reused.</p>
<p style="padding-left:30px;text-align:justify;">It is the “next” compiled query against the column statistics that will see that they are out of date and will then in turn instantiate the action of updating statistics.</p>
<p style="padding-left:30px;text-align:justify;">You can see this in action by forcing the second statement to recompile.</p>
<p style="padding-left:30px;text-align:justify;">SELECT FirstName, LastName, City<br />
FROM ExecTable<br />
WHERE City = ‘New York’ option(RECOMPILE)<br />
GO</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/06/02/sql-server-puzzle-statistics-are-not-updated-but-are-created-once/#comment-138398" target="_blank">Kevin Cross</a> also have another suggestion:</p>
<p style="padding-left:30px;text-align:justify;">I agree with John. It is reusing the Execution Plan. Aside from OPTION(RECOMPILE), clearing the Execution Plan Cache before the subsequent tests will also work.</p>
<p style="padding-left:30px;text-align:justify;">i.e., run this before round 2:<br />
————————————————————–<br />
– Clear execution plan cache before next test<br />
DBCC FREEPROCCACHE WITH NO_INFOMSGS;<br />
————————————————————–</p>
<p style="padding-left:30px;text-align:justify;">Nice puzzle!</p>
<p style="padding-left:30px;text-align:justify;">Kevin</p>
<p style="text-align:justify;">As this was puzzle John and Kevin both got the correct answer, there was no condition for answer to be part of best practices. I know John and he is finest DBA around &#8211; his tremendous knowledge has always impressed me. John and Kevin both will agree that clearing cache either using DBCC FREEPROCCACHE and recompiling each query every time is for sure <em><strong>not good advice</strong></em> on production server. It is correct answer but not best practice.</p>
<p style="text-align:justify;">By the way, if you have better solution or have better suggestion please advise. I am open to change my answer and publish further improvement to this solution. On very separate note, I like to have clustered index on my Primary Key, which I have not mentioned here as it is out of the scope of this puzzle.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/readers-contribution/'>Readers Contribution</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/statistics/'>Statistics</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/13228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/13228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/13228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/13228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/13228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/13228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/13228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/13228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/13228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/13228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/13228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/13228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/13228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/13228/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13228&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/06/17/sql-server-solution-puzzle-statistics-are-not-updated-but-are-created-once/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; Puzzle &#8211; Statistics are not Updated but are Created Once</title>
		<link>http://blog.sqlauthority.com/2011/06/02/sql-server-puzzle-statistics-are-not-updated-but-are-created-once/</link>
		<comments>http://blog.sqlauthority.com/2011/06/02/sql-server-puzzle-statistics-are-not-updated-but-are-created-once/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 01:30:52 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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>
		<category><![CDATA[SQL Statistics]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=13068</guid>
		<description><![CDATA[After having excellent response to my quiz &#8211; Why SELECT * throws an error but SELECT COUNT(*) does not?I have decided to ask another puzzling question to all of you. I am running this test on SQL Server 2008 R2. Here is the quick scenario about my setup. Create Table Insert 1000 Records Check the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13068&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">After having excellent response to my quiz &#8211; <strong><a href="http://blog.sqlauthority.com/2011/05/30/sql-server-puzzle-select-vs-select-count/" target="_blank">Why SELECT * throws an error but SELECT COUNT(*) does not?</a></strong>I have decided to ask another puzzling question to all of you.</p>
<p style="text-align:justify;">I am running this test on SQL Server 2008 R2. Here is the quick scenario about my setup.</p>
<ul style="text-align:justify;">
<li>Create Table</li>
<li>Insert 1000 Records</li>
<li>Check the Statistics</li>
<li>Now insert 10 times more 10,000 indexes</li>
<li>Check the Statistics &#8211; it will be <strong>NOT</strong> updated</li>
</ul>
<p><strong>Note: Auto Update Statistics and Auto Create Statistics for database is TRUE</strong></p>
<p style="text-align:justify;">Expected Result &#8211; Statistics should be updated &#8211; <strong></strong><strong><a href="http://blog.sqlauthority.com/2010/04/21/sql-server-when-are-statistics-updated-what-triggers-statistics-to-update/">SQL SERVER – When are Statistics Updated – What triggers Statistics to Update</a></strong></p>
<p style="text-align:justify;">Now the question is why the statistics are not updated?</p>
<p style="text-align:justify;">The common answer is &#8211; we can update the statistics ourselves using</p>
<pre>UPDATE STATISTICS TableName WITH FULLSCAN, ALL</pre>
<p style="text-align:justify;">However, the solution I am looking is where statistics should be updated automatically based on algorithm mentioned <strong><a href="http://blog.sqlauthority.com/2010/04/21/sql-server-when-are-statistics-updated-what-triggers-statistics-to-update/" target="_blank">here</a></strong>.</p>
<p style="text-align:justify;">Now the solution is to ____________________.</p>
<p style="text-align:justify;"><a href="http://blogs.extremeexperts.com/" target="_blank">Vinod Kumar</a> is not allowed to take participate over here as he is the one who has helped me to build this puzzle.</p>
<p style="text-align:justify;">I will publish the solution on next week. Please leave a comment and if your comment consist valid answer, I will publish with due credit.</p>
<p style="text-align:justify;">Here is the script to reproduce the scenario which I mentioned.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Execution Plans Difference<br />
-- Create Sample Database<br />
</span><span style="color:blue;">CREATE DATABASE </span><span style="color:black;">SampleDB<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">SampleDB<br />
GO<br />
</span><span style="color:green;">-- Create Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">ExecTable </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 Thousand Records<br />
-- INSERT 1<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">ExecTable </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;">1000 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 </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;">20 </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;">20 </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;">20 </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;">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;">20 </span><span style="color:blue;">= </span><span style="color:black;">7 </span><span style="color:blue;">THEN </span><span style="color:red;">'La Cinega'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">13 </span><span style="color:blue;">THEN </span><span style="color:red;">'San Diego'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">17 </span><span style="color:blue;">THEN </span><span style="color:red;">'Las Vegas'<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<br />
</span><span style="color:green;">-- Display statistics of the table - none listed<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">N'ExecTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Select Statement<br />
</span><span style="color:blue;">SELECT </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<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">ExecTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City  </span><span style="color:blue;">= </span><span style="color:red;">'New York'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Display statistics of the table<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">N'ExecTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/statsrows1.JPG" alt="" width="500" height="69" /><br />
</span><span style="color:green;">-- Replace your Statistics over here<br />
-- NOTE: Replace your _WA_Sys with stats from above query<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">SHOW_STATISTICS</span><span style="color:gray;">(</span><span style="color:red;">'ExecTable'</span><span style="color:gray;">, </span><span style="color:black;">_WA_Sys_00000004_7D78A4E7</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">--------------------------------------------------------------<br />
-- Round 2<br />
-- Insert Ten Thousand Records<br />
-- INSERT 2<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">ExecTable </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;">10000 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 </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;">20 </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;">20 </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;">20 </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;">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;">20 </span><span style="color:blue;">= </span><span style="color:black;">7 </span><span style="color:blue;">THEN </span><span style="color:red;">'La Cinega'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">13 </span><span style="color:blue;">THEN </span><span style="color:red;">'San Diego'<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;">20 </span><span style="color:blue;">= </span><span style="color:black;">17 </span><span style="color:blue;">THEN </span><span style="color:red;">'Las Vegas'<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<br />
</span><span style="color:green;">-- Select Statement<br />
</span><span style="color:blue;">SELECT </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<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">ExecTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City  </span><span style="color:blue;">= </span><span style="color:red;">'New York'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Display statistics of the table<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">N'ExecTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Replace your Statistics over here<br />
-- NOTE: Replace your _WA_Sys with stats from above query<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">SHOW_STATISTICS</span><span style="color:gray;">(</span><span style="color:red;">'ExecTable'</span><span style="color:gray;">, </span><span style="color:black;">_WA_Sys_00000004_7D78A4E7</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- You will notice that Statistics are still updated with 1000 rows<img class="alignnone" src="http://www.pinaldave.com/bimg/statsrows.JPG" alt="" width="496" height="46" /><br />
-- Clean up Database<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">ExecTable<br />
GO<br />
</span><span style="color:blue;">USE MASTER<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">ALTER DATABASE </span><span style="color:black;">SampleDB<br />
</span><span style="color:blue;">SET </span><span style="color:black;">SINGLE_USER </span><span style="color:blue;">WITH ROLLBACK IMMEDIATE</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">DROP DATABASE </span><span style="color:black;">SampleDB<br />
GO</span><span style="color:green;"><br style="color:black;" /></span></code></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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/sql-statistics/'>SQL Statistics</a>, <a href='http://blog.sqlauthority.com/tag/statistics/'>Statistics</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/13068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/13068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/13068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/13068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/13068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/13068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/13068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/13068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/13068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/13068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/13068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/13068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/13068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/13068/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13068&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/06/02/sql-server-puzzle-statistics-are-not-updated-but-are-created-once/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/statsrows1.JPG" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/statsrows.JPG" medium="image" />
	</item>
		<item>
		<title>SQLAuthority News &#8211; Win Windows Phone from Idera in 140 Characters &#8211; A Cartoon Challenge of SQL</title>
		<link>http://blog.sqlauthority.com/2011/04/05/sqlauthority-news-win-windows-phone-from-idera-in-140-characters-a-cartoon-challenge-of-sql/</link>
		<comments>http://blog.sqlauthority.com/2011/04/05/sqlauthority-news-win-windows-phone-from-idera-in-140-characters-a-cartoon-challenge-of-sql/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=11786</guid>
		<description><![CDATA[I personally have Windows Phone and I love it. The user friendliness and integration with social media is remarkable. My wife Nupur is big fan of Windows Live tools and Windows Phone as well. Well, this blog post is not about our preference of Windows Phone but about YOU a unlocked Windows Phone. The Windows [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11786&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I personally have Windows Phone and I love it. The user friendliness and integration with social media is remarkable. My wife <strong><a href="http://www.nupurdave.com">Nupur </a></strong>is big fan of Windows Live tools and Windows Phone as well. Well, this blog post is not about our preference of Windows Phone but about YOU a unlocked Windows Phone. The Windows Phone will be directly sponsored by <strong><a href="http://www.idera.com/?s=BN_SQLAuth" target="_blank">Idera</a></strong>.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/wp7.jpg" alt="" width="493" height="462" /></p>
<p style="text-align:justify;">If you want to win Windows Phone. Just do one thing, complete following cartoon. Every day queries go slow and we think it is SQL Server but the reality is that it is us who need to know the right technology. <strong><a href="http://www.idera.com/?s=BN_SQLAuth" target="_blank">Idera</a></strong> provides tools for Microsoft SQL Server, SharePoint and PowerShell management and administration.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/slowquery3.jpg" alt="" width="500" /></p>
<h3 style="text-align:justify;">Contest Rules:</h3>
<ul style="text-align:justify;">
<li>Answer must be of maximum 140 character.</li>
<li>Winner will get Windows Phone or USD 500 Gift Card from Idera.</li>
<li>No purchase is required.</li>
<li>This contest is open to all SQL enthusiasts in the world.</li>
<li>Recipient will be responsible for local taxes.</li>
<li>Idera has all the rights to alter, or modify the competition.</li>
<li>To participate please a comment to this blog post.</li>
<li>One person can participate multiple times.</li>
<li>The contest is open till April 20th, 2011.</li>
<li>Winner will be announced on April 25st, 2011.</li>
<li>Phone image is for illustration purpose.</li>
</ul>
<h3 style="text-align:justify;">Note:</h3>
<ul style="text-align:justify;">
<li>Promote at one more social media place the answer to the question.</li>
<li>Comments are moderated for spams and SEO marketers, if your comment does not appear, please send me email.</li>
<li>Mentioning <strong><a href="http://www.idera.com/?s=BN_SQLAuth" target="_blank">Idera </a></strong>in answer is encouraged but not necessary.</li>
</ul>
<p style="text-align:justify;">Reference:<strong> Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-news/'>SQLAuthority News</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/11786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/11786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/11786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/11786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/11786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/11786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/11786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/11786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/11786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/11786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/11786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/11786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/11786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/11786/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11786&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/04/05/sqlauthority-news-win-windows-phone-from-idera-in-140-characters-a-cartoon-challenge-of-sql/feed/</wfw:commentRss>
		<slash:comments>212</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/wp7.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/slowquery3.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; What is Fill Factor and What is the Best Value for Fill Factor</title>
		<link>http://blog.sqlauthority.com/2011/01/31/sql-server-what-is-fill-factor-and-what-is-the-best-value-for-fill-factor/</link>
		<comments>http://blog.sqlauthority.com/2011/01/31/sql-server-what-is-fill-factor-and-what-is-the-best-value-for-fill-factor/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 01:30:28 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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=11826</guid>
		<description><![CDATA[Working in performance tuning area, one has to know about Index and Index Maintenance. For any Index the most important property is Fill Factor. Fill factor is the value that determines the percentage of space on each leaf-level page to be filled with data. In an SQL Server, the smallest unit is a page, which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11826&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Working in performance tuning area, one has to know about Index and Index Maintenance. For any Index the most important property is Fill Factor.</p>
<p style="text-align:justify;"><strong>Fill factor</strong> is the value that determines the percentage of space on each  leaf-level page to be filled with data. In an SQL Server, the smallest  unit is a page, which is made of  Page with size 8K. Every page can  store one or more rows based on the size of the row. The default value  of the Fill Factor is 100, which is same as value 0. The default Fill  Factor (100 or 0) will allow the SQL Server to fill the leaf-level pages  of an index with the maximum numbers of the rows it can fit. There will  be no or very little empty space left in the page, when the fill factor  is 100.</p>
<p style="text-align:justify;">I have written following two article about Fill Factor.</p>
<p style="text-align:justify;"><strong><a href="http://www.sqlmag.com/blogs/BeginningSQLServerOneStepataTime/tabid/3472/entryid/76069/What-is-Fill-factor-Index-Fill-Factor-and-Performance-Part-1.aspx" target="_blank">What is Fill factor? &#8211; Index, Fill Factor and Performance &#8211; Part 1</a></strong></p>
<p style="text-align:justify;"><strong><a href="http://www.sqlmag.com/blogs/BeginningSQLServerOneStepataTime/tabid/3472/entryid/76109/What-is-the-best-value-for-the-Fill-Factor-Index-Fill-Factor-and-Performance-Part-2.aspx" target="_blank">What is the best value for the Fill Factor? &#8211; Index, Fill Factor and Performance – Part 2</a></strong></p>
<p style="text-align:justify;">I strongly encourage read them and provide your feedback.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/11826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/11826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/11826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/11826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/11826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/11826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/11826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/11826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/11826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/11826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/11826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/11826/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/11826/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/11826/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11826&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/01/31/sql-server-what-is-fill-factor-and-what-is-the-best-value-for-fill-factor/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; Shrinking Database is Bad &#8211; Increases Fragmentation &#8211; Reduces Performance</title>
		<link>http://blog.sqlauthority.com/2011/01/19/sql-server-shrinking-database-is-bad-increases-fragmentation-reduces-performance/</link>
		<comments>http://blog.sqlauthority.com/2011/01/19/sql-server-shrinking-database-is-bad-increases-fragmentation-reduces-performance/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 01:30:56 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></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=11622</guid>
		<description><![CDATA[Earlier, I had written two articles related to Shrinking Database. I wrote about why Shrinking Database is not good. SQL SERVER – SHRINKDATABASE For Every Database in the SQL Server SQL SERVER – What the Business Says Is Not What the Business Wants I received many comments on Why Database Shrinking is bad. Today we will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11622&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier, I had written two articles related to Shrinking Database. I wrote about why Shrinking Database is not good.</p>
<ul style="text-align:justify;">
<li><strong><a href="http://blog.sqlauthority.com/2010/08/12/sql-server-shrinkdatabase-for-every-database-in-the-sql-server/">SQL SERVER – SHRINKDATABASE For Every Database in the SQL Server</a></strong></li>
<li><strong><a href="http://blog.sqlauthority.com/2010/12/14/sql-server-what-the-business-says-is-not-what-the-business-wants/">SQL SERVER – What the Business Says Is Not What the Business Wants</a></strong></li>
</ul>
<p style="text-align:justify;">I received many comments on Why Database Shrinking is bad. Today we will go over a very interesting example that I have created for the same. Here are the quick steps of the example.</p>
<ul style="text-align:justify;">
<li>Create a test database</li>
<li>Create two tables and populate      with data</li>
<li>Check the size of both the      tables
<ul>
<li>Size of database is very low</li>
</ul>
</li>
<li>Check the Fragmentation of one      table
<ul>
<li>Fragmentation will be very low</li>
</ul>
</li>
<li>Truncate another table</li>
<li>Check the size of the table</li>
<li>Check the fragmentation of the      one table
<ul>
<li>Fragmentation will be very low</li>
</ul>
</li>
<li>SHRINK Database</li>
<li>Check the size of the table</li>
<li>Check the fragmentation of the      one table
<ul>
<li>Fragmentation will be very       HIGH</li>
</ul>
</li>
<li>REBUILD index on one table</li>
<li>Check the size of the table
<ul>
<li>Size of database is very HIGH</li>
</ul>
</li>
<li>Check the fragmentation of the      one table
<ul>
<li>Fragmentation will be very low</li>
</ul>
</li>
</ul>
<p style="text-align:justify;">Here is the script for the same.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE MASTER<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE DATABASE </span><span style="color:black;">ShrinkIsBed<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">ShrinkIsBed<br />
GO<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Create FirstTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">FirstTable </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;">-- Create Clustered Index on ID<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_FirstTable_ID] </span><span style="color:blue;">ON </span><span style="color:black;">FirstTable<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<br />
</span><span style="color:green;">-- Create SecondTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">SecondTable </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;">-- Create Clustered Index on ID<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_SecondTable_ID] </span><span style="color:blue;">ON </span><span style="color:black;">SecondTable<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<br />
</span><span style="color:green;">-- Insert One Hundred Thousand Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">FirstTable </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 </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<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
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;">SecondTable </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 </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<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Check Fragmentations in the database<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_fragmentation_in_percent</span><span style="color:gray;">, </span><span style="color:black;">fragment_count<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats </span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(), </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'SecondTable'</span><span style="color:gray;">), NULL, NULL, </span><span style="color:red;">'LIMITED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Let us check the table size and fragmentation.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/ShrinkFrag1.jpg" alt="" width="426" height="444" /></p>
<p style="text-align:justify;">Now let us TRUNCATE the table and check the size and Fragmentation.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE MASTER<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE DATABASE </span><span style="color:black;">ShrinkIsBed<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">ShrinkIsBed<br />
GO<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Create FirstTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">FirstTable </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;">-- Create Clustered Index on ID<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_FirstTable_ID] </span><span style="color:blue;">ON </span><span style="color:black;">FirstTable<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<br />
</span><span style="color:green;">-- Create SecondTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">SecondTable </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;">-- Create Clustered Index on ID<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_SecondTable_ID] </span><span style="color:blue;">ON </span><span style="color:black;">SecondTable<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<br />
</span><span style="color:green;">-- Insert One Hundred Thousand Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">FirstTable </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 </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<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
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;">SecondTable </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 </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<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Check Fragmentations in the database<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_fragmentation_in_percent</span><span style="color:gray;">, </span><span style="color:black;">fragment_count<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats </span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(), </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'SecondTable'</span><span style="color:gray;">), NULL, NULL, </span><span style="color:red;">'LIMITED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/ShrinkFrag2.jpg" alt="" width="500" height="468" /></p>
<p style="text-align:justify;">You can clearly see that after TRUNCATE, the size of the database is not reduced and it is still the same as before TRUNCATE operation. After the Shrinking database operation, we were able to reduce the size of the database. If you notice the fragmentation, it is considerably high.</p>
<p style="text-align:justify;">The major problem with the Shrink operation is that it increases fragmentation of the database to very high value. Higher fragmentation reduces the performance of the database as reading from that particular table becomes very expensive.</p>
<p style="text-align:justify;">One of the ways to reduce the fragmentation is to rebuild index on the database. Let us rebuild the index and observe fragmentation and database size.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Rebuild Index on FirstTable<br />
</span><span style="color:blue;">ALTER INDEX </span><span style="color:black;">IX_SecondTable_ID </span><span style="color:blue;">ON </span><span style="color:black;">SecondTable REBUILD<br />
GO<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Check Fragmentations in the database<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_fragmentation_in_percent</span><span style="color:gray;">, </span><span style="color:black;">fragment_count<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats </span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(), </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'SecondTable'</span><span style="color:gray;">), NULL, NULL, </span><span style="color:red;">'LIMITED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/ShrinkFrag3.jpg" alt="" width="430" height="228" /></p>
<p style="text-align:justify;">You can notice that after rebuilding, Fragmentation reduces to a very low value (almost same to original value); however the database size increases way higher than the original. Before rebuilding, the size of the database was 5 MB, and after rebuilding, it is around 20 MB. Regular rebuilding the index is rebuild in the same user database where the index is placed. This usually increases the size of the database.</p>
<p style="text-align:justify;"><strong><em>Look at irony of the Shrinking database. One person shrinks the database to gain space (thinking it will help performance), which leads to increase in fragmentation (reducing performance). To reduce the fragmentation, one rebuilds index, which leads to size of the database to increase way more than the original size of the database (before shrinking). Well, by Shrinking, one did not gain what he was looking for usually.</em></strong></p>
<p style="text-align:justify;">Rebuild indexing is not the best suggestion as that will create database grow again.</p>
<p style="text-align:justify;">I have always remembered the excellent post from <a href="http://www.sqlskills.com/BLOGS/PAUL/post/Why-you-should-not-shrink-your-data-files.aspx" target="_blank">Paul Randal</a> regarding Shrinking the database is bad. I suggest every one to read that for accuracy and interesting conversation.</p>
<p style="text-align:justify;">Let us run following script where we Shrink the database and REORGANIZE.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Check Fragmentations in the database<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_fragmentation_in_percent</span><span style="color:gray;">, </span><span style="color:black;">fragment_count<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats </span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(), </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'SecondTable'</span><span style="color:gray;">), NULL, NULL, </span><span style="color:red;">'LIMITED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Shrink the Database<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">SHRINKDATABASE </span><span style="color:gray;">(</span><span style="color:black;">ShrinkIsBed</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Check Fragmentations in the database<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_fragmentation_in_percent</span><span style="color:gray;">, </span><span style="color:black;">fragment_count<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats </span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(), </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'SecondTable'</span><span style="color:gray;">), NULL, NULL, </span><span style="color:red;">'LIMITED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Rebuild Index on FirstTable<br />
</span><span style="color:blue;">ALTER INDEX </span><span style="color:black;">IX_SecondTable_ID </span><span style="color:blue;">ON </span><span style="color:black;">SecondTable REORGANIZE<br />
GO<br />
</span><span style="color:green;">-- Name of the Database and Size<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">name</span><span style="color:gray;">, (</span><span style="color:black;">size</span><span style="color:gray;">*</span><span style="color:black;">8</span><span style="color:gray;">) </span><span style="color:black;">Size_KB<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.database_files<br />
GO<br />
</span><span style="color:green;">-- Check Fragmentations in the database<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_fragmentation_in_percent</span><span style="color:gray;">, </span><span style="color:black;">fragment_count<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats </span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(), </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'SecondTable'</span><span style="color:gray;">), NULL, NULL, </span><span style="color:red;">'LIMITED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/ShrinkFrag4.jpg" alt="" width="515" height="571" /></p>
<p style="text-align:justify;">You can see that REORGANIZE does not increase the size of the database or remove the fragmentation.</p>
<p style="text-align:justify;">Again, I no way suggest that REORGANIZE is the solution over here. This is purely observation using demo. Read the blog post of Paul Randal.</p>
<p style="text-align:justify;">Following script will clean up the database<br />
<code style="font-size:12px;"><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">USE MASTER<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">ALTER DATABASE </span><span style="color:black;">ShrinkIsBed<br />
</span><span style="color:blue;">SET </span><span style="color:black;">SINGLE_USER </span><span style="color:blue;">WITH ROLLBACK IMMEDIATE<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">DROP DATABASE </span><span style="color:black;">ShrinkIsBed<br />
GO</span></code></p>
<p style="text-align:justify;">There are few valid cases of the Shrinking database as well, but that is not covered in this blog post. We will cover that area some other time in future. Additionally, one can rebuild index in the tempdb as well, and we will also talk about the same in future. <a href="http://www.brentozar.com/archive/2009/08/stop-shrinking-your-database-files-seriously-now/" target="_blank">Brent </a>has written a good summary blog post as well.</p>
<p style="text-align:justify;"><em><strong>Are you Shrinking your database? Well, when are you going to stop Shrinking it? </strong></em></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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/11622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/11622/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/11622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/11622/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/11622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/11622/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/11622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/11622/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/11622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/11622/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/11622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/11622/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/11622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/11622/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11622&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/01/19/sql-server-shrinking-database-is-bad-increases-fragmentation-reduces-performance/feed/</wfw:commentRss>
		<slash:comments>13</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/ShrinkFrag1.jpg" medium="image" />

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

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

		<media:content url="http://www.pinaldave.com/bimg/ShrinkFrag4.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; A Funny Cartoon on Index</title>
		<link>http://blog.sqlauthority.com/2011/01/17/sql-server-a-funny-cartoon-on-index/</link>
		<comments>http://blog.sqlauthority.com/2011/01/17/sql-server-a-funny-cartoon-on-index/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 01:30:05 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Humor]]></category>
		<category><![CDATA[SQL Index]]></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=11660</guid>
		<description><![CDATA[Performance Tuning has been my favorite subject and I have done it for many years now. Today I will list one of the most common conversation about Index I have heard in my life. Every single time, I am at consultation for performance tuning I hear following conversation among various team members. I want to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11660&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/sql-server-performance-tuning/" target="_blank">Performance Tuning</a></strong> has been my favorite subject and I have done it for many years now. Today I will list one of the most common conversation about Index I have heard in my life.</p>
<p style="text-align:justify;">Every single time, I am at consultation for performance tuning I hear following conversation among various team members. I want to ask you, does this kind of conversation happens in your organization?</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexcartoon.jpg" alt="" width="500" height="197" /></p>
<p style="text-align:justify;">Any way, If you think Index solves all of your performance problem I think it is not true. There are many other reason one has to consider along with Indexes. For example I consider following various topic one need to understand for performance tuning.</p>
<ul style="text-align:justify;">
<li>Logical Query Processing</li>
<li>Efficient Join Techniques</li>
<li>Query Tuning Considerations</li>
<li>Avoiding Common Performance Tuning Issues</li>
<li>Statistics and Best Practices</li>
<li>TempDB Tuning</li>
<li>Hardware Planning</li>
<li>Understanding Query Processor</li>
<li>Using SQL Server 2005 and 2008 Updated Feature Sets</li>
<li>CPU, Memory, I/O Bottleneck</li>
<li>Index Tuning (of course)</li>
<li>Many more…</li>
</ul>
<p style="text-align:justify;">Well, I have written this blog thinking I will keep this blog post a bit easy and not load up. I will in future discuss about other performance tuning concepts. Let me know what do you think about the cartoon I made.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-humor/'>SQL Humor</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/11660/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/11660/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/11660/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/11660/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/11660/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/11660/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/11660/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/11660/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/11660/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/11660/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/11660/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/11660/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/11660/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/11660/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11660&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/01/17/sql-server-a-funny-cartoon-on-index/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/indexcartoon.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Unused Index Script &#8211; Download</title>
		<link>http://blog.sqlauthority.com/2011/01/04/sql-server-2008-unused-index-script-download/</link>
		<comments>http://blog.sqlauthority.com/2011/01/04/sql-server-2008-unused-index-script-download/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Index]]></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=11429</guid>
		<description><![CDATA[Download Missing Index Script with Unused Index Script and Duplicate Index Script Performance Tuning is quite interesting and Index plays a vital role in it. A proper index can improve the performance and a bad index can hamper the performance. Here is the script from my script bank which I use to identify unused indexes [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11429&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><strong><a href="http://www.pinaldave.com/sql-downloads/script-downloads/sql-server-missing-index-unused-index-and-duplicate-index-script-2/" target="_blank">Download Missing Index Script with Unused Index Script and Duplicate Index Script</a></strong></p>
<p>Performance Tuning is quite interesting and Index plays a vital role in it. A proper index can improve the performance and a bad index can hamper the performance.</p>
<p style="text-align:justify;">Here is the script from my script bank which I use to identify unused indexes on any database.</p>
<p style="text-align:justify;">Please note, if you should not drop all the unused indexes this script suggest. This is just for guidance. You should not create more than 5-10 indexes per table. Additionally, this script sometime does not give accurate information so use your common sense.</p>
<p style="text-align:justify;">Any way, the scripts is good starting point. You should pay attention to User Scan, User Lookup and User Update when you are going to drop index. The generic understanding is if this values are all high and User Seek is low, the index needs tuning. The index drop script is also provided in the last column.</p>
<p style="text-align:justify;"><strong><a href="http://www.pinaldave.com/sql-downloads/script-downloads/sql-server-missing-index-unused-index-and-duplicate-index-script-2/" target="_blank">Download Missing Index Script with Unused Index Script</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Unused Index Script<br />
-- Original Author: Pinal Dave (C) 2011<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">25<br />
o.name </span><span style="color:blue;">AS </span><span style="color:black;">ObjectName<br />
</span><span style="color:gray;">, </span><span style="color:black;">i.name </span><span style="color:blue;">AS </span><span style="color:black;">IndexName<br />
</span><span style="color:gray;">, </span><span style="color:black;">i.index_id </span><span style="color:blue;">AS </span><span style="color:black;">IndexID<br />
</span><span style="color:gray;">, </span><span style="color:black;">dm_ius.user_seeks </span><span style="color:blue;">AS </span><span style="color:black;">UserSeek<br />
</span><span style="color:gray;">, </span><span style="color:black;">dm_ius.user_scans </span><span style="color:blue;">AS </span><span style="color:black;">UserScans<br />
</span><span style="color:gray;">, </span><span style="color:black;">dm_ius.user_lookups </span><span style="color:blue;">AS </span><span style="color:black;">UserLookups<br />
</span><span style="color:gray;">, </span><span style="color:black;">dm_ius.user_updates </span><span style="color:blue;">AS </span><span style="color:black;">UserUpdates<br />
</span><span style="color:gray;">, </span><span style="color:black;">p.TableRows<br />
</span><span style="color:gray;">, </span><span style="color:red;">'DROP INDEX ' </span><span style="color:gray;">+ </span><span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:black;">i.name</span><span style="color:gray;">)<br />
+ </span><span style="color:red;">' ON ' </span><span style="color:gray;">+ </span><span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:black;">s.name</span><span style="color:gray;">) + </span><span style="color:red;">'.' </span><span style="color:gray;">+ </span><span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">dm_ius.</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">)) </span><span style="color:blue;">AS </span><span style="color:red;">'drop statement'<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_usage_stats dm_ius<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.indexes i </span><span style="color:blue;">ON </span><span style="color:black;">i.index_id </span><span style="color:blue;">= </span><span style="color:black;">dm_ius.index_id </span><span style="color:gray;">AND </span><span style="color:black;">dm_ius.</span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:black;">i.</span><span style="color:magenta;">OBJECT_ID<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.objects o </span><span style="color:blue;">ON </span><span style="color:black;">dm_ius.</span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:black;">o.</span><span style="color:magenta;">OBJECT_ID<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.schemas s </span><span style="color:blue;">ON </span><span style="color:black;">o.schema_id </span><span style="color:blue;">= </span><span style="color:black;">s.schema_id<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">p.rows</span><span style="color:gray;">) </span><span style="color:black;">TableRows</span><span style="color:gray;">, </span><span style="color:black;">p.index_id</span><span style="color:gray;">, </span><span style="color:black;">p.</span><span style="color:magenta;">OBJECT_ID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.partitions p </span><span style="color:blue;">GROUP BY </span><span style="color:black;">p.index_id</span><span style="color:gray;">, </span><span style="color:black;">p.</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">) </span><span style="color:black;">p<br />
</span><span style="color:blue;">ON </span><span style="color:black;">p.index_id </span><span style="color:blue;">= </span><span style="color:black;">dm_ius.index_id </span><span style="color:gray;">AND </span><span style="color:black;">dm_ius.</span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:black;">p.</span><span style="color:magenta;">OBJECT_ID<br />
</span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECTPROPERTY</span><span style="color:gray;">(</span><span style="color:black;">dm_ius.</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">,</span><span style="color:red;">'IsUserTable'</span><span style="color:gray;">) </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:gray;">AND </span><span style="color:black;">dm_ius.database_id </span><span style="color:blue;">= </span><span style="color:magenta;">DB_ID</span><span style="color:gray;">()<br />
AND </span><span style="color:black;">i.type_desc </span><span style="color:blue;">= </span><span style="color:red;">'nonclustered'<br />
</span><span style="color:gray;">AND </span><span style="color:black;">i.is_primary_key </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:gray;">AND </span><span style="color:black;">i.is_unique_constraint </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:gray;">(</span><span style="color:black;">dm_ius.user_seeks </span><span style="color:gray;">+ </span><span style="color:black;">dm_ius.user_scans </span><span style="color:gray;">+ </span><span style="color:black;">dm_ius.user_lookups</span><span style="color:gray;">) </span><span style="color:blue;">ASC<br />
</span><span style="color:black;">GO<br />
</span></code></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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-download/'>SQL Download</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/11429/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/11429/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/11429/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/11429/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/11429/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/11429/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/11429/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/11429/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/11429/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/11429/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/11429/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/11429/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/11429/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/11429/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11429&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/01/04/sql-server-2008-unused-index-script-download/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; 2008 &#8211;  Missing Index Script &#8211; Download</title>
		<link>http://blog.sqlauthority.com/2011/01/03/sql-server-2008-missing-index-script-download/</link>
		<comments>http://blog.sqlauthority.com/2011/01/03/sql-server-2008-missing-index-script-download/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 01:30:58 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Index]]></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=11423</guid>
		<description><![CDATA[Download Missing Index Script with Unused Index Script and Duplicate Index Script Performance Tuning is quite interesting and Index plays a vital role in it. A proper index can improve the performance and a bad index can hamper the performance. Here is the script from my script bank which I use to identify missing indexes [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11423&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><a href="http://www.pinaldave.com/sql-downloads/script-downloads/sql-server-missing-index-unused-index-and-duplicate-index-script-2/" target="_blank"><strong>Download Missing Index Script with Unused Index Script and Duplicate Index Script</strong></a></p>
<p>Performance Tuning is quite interesting and Index plays a vital role in it. A proper index can improve the performance and a bad index can hamper the performance.</p>
<p style="text-align:justify;">Here is the script from my script bank which I use to identify missing indexes on any database.</p>
<p style="text-align:justify;">Please note, if you should not create all the missing indexes this script suggest. This is just for guidance. You should not create more than 5-10 indexes per table. Additionally, this script sometime does not give accurate information so use your common sense.</p>
<p style="text-align:justify;">Any way, the scripts is good starting point. You should pay attention to Avg_Estimated_Impact when you are going to create index. The index creation script is also provided in the last column.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/sql-downloads/script-downloads/sql-server-missing-index-unused-index-and-duplicate-index-script-2/" target="_blank"><strong>Download Missing Index Script with Unused Index Script</strong></a><br />
<code style="font-size:12px;"><span style="color:green;">-- Missing Index Script<br />
-- Original Author: Pinal Dave (C) 2011<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">25<br />
dm_mid.database_id </span><span style="color:blue;">AS </span><span style="color:black;">DatabaseID</span><span style="color:gray;">,<br />
</span><span style="color:black;">dm_migs.avg_user_impact</span><span style="color:gray;">*(</span><span style="color:black;">dm_migs.user_seeks</span><span style="color:gray;">+</span><span style="color:black;">dm_migs.user_scans</span><span style="color:gray;">) </span><span style="color:black;">Avg_Estimated_Impact</span><span style="color:gray;">,<br />
</span><span style="color:black;">dm_migs.last_user_seek </span><span style="color:blue;">AS </span><span style="color:black;">Last_User_Seek</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">dm_mid.</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">,</span><span style="color:black;">dm_mid.database_id</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">[TableName]</span><span style="color:gray;">,<br />
</span><span style="color:red;">'CREATE INDEX [IX_' </span><span style="color:gray;">+ </span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">dm_mid.</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">,</span><span style="color:black;">dm_mid.database_id</span><span style="color:gray;">) + </span><span style="color:red;">'_'<br />
</span><span style="color:gray;">+ </span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:black;">dm_mid.equality_columns</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">),</span><span style="color:red;">', '</span><span style="color:gray;">,</span><span style="color:red;">'_'</span><span style="color:gray;">),</span><span style="color:red;">'['</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">),</span><span style="color:red;">']'</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">) +<br />
</span><span style="color:magenta;">CASE<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">dm_mid.equality_columns </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL AND </span><span style="color:black;">dm_mid.inequality_columns </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL </span><span style="color:blue;">THEN </span><span style="color:red;">'_'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">''<br />
</span><span style="color:blue;">END<br />
</span><span style="color:gray;">+ </span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:black;">dm_mid.inequality_columns</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">),</span><span style="color:red;">', '</span><span style="color:gray;">,</span><span style="color:red;">'_'</span><span style="color:gray;">),</span><span style="color:red;">'['</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">),</span><span style="color:red;">']'</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">)<br />
+ </span><span style="color:red;">']'<br />
</span><span style="color:gray;">+ </span><span style="color:red;">' ON ' </span><span style="color:gray;">+ </span><span style="color:black;">dm_mid.statement<br />
</span><span style="color:gray;">+ </span><span style="color:red;">' (' </span><span style="color:gray;">+ </span><span style="color:magenta;">ISNULL </span><span style="color:gray;">(</span><span style="color:black;">dm_mid.equality_columns</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">)<br />
+ </span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">dm_mid.equality_columns </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL AND </span><span style="color:black;">dm_mid.inequality_columns </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL </span><span style="color:blue;">THEN </span><span style="color:red;">',' </span><span style="color:blue;">ELSE<br />
</span><span style="color:red;">'' </span><span style="color:blue;">END<br />
</span><span style="color:gray;">+ </span><span style="color:magenta;">ISNULL </span><span style="color:gray;">(</span><span style="color:black;">dm_mid.inequality_columns</span><span style="color:gray;">, </span><span style="color:red;">''</span><span style="color:gray;">)<br />
+ </span><span style="color:red;">')'<br />
</span><span style="color:gray;">+ </span><span style="color:magenta;">ISNULL </span><span style="color:gray;">(</span><span style="color:red;">' INCLUDE (' </span><span style="color:gray;">+ </span><span style="color:black;">dm_mid.included_columns </span><span style="color:gray;">+ </span><span style="color:red;">')'</span><span style="color:gray;">, </span><span style="color:red;">''</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Create_Statement<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_missing_index_groups dm_mig<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.dm_db_missing_index_group_stats dm_migs<br />
</span><span style="color:blue;">ON </span><span style="color:black;">dm_migs.group_handle </span><span style="color:blue;">= </span><span style="color:black;">dm_mig.index_group_handle<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.dm_db_missing_index_details dm_mid<br />
</span><span style="color:blue;">ON </span><span style="color:black;">dm_mig.index_handle </span><span style="color:blue;">= </span><span style="color:black;">dm_mid.index_handle<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">dm_mid.database_ID </span><span style="color:blue;">= </span><span style="color:magenta;">DB_ID</span><span style="color:gray;">()<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">Avg_Estimated_Impact </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO</span></code></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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-download/'>SQL Download</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/11423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/11423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/11423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/11423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/11423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/11423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/11423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/11423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/11423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/11423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/11423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/11423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/11423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/11423/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11423&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/01/03/sql-server-2008-missing-index-script-download/feed/</wfw:commentRss>
		<slash:comments>20</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; Last Day to Participate in my Questions at SQL Quiz</title>
		<link>http://blog.sqlauthority.com/2010/11/06/sqlauthority-news-last-day-to-participate-in-my-questions-at-sql-quiz/</link>
		<comments>http://blog.sqlauthority.com/2010/11/06/sqlauthority-news-last-day-to-participate-in-my-questions-at-sql-quiz/#comments</comments>
		<pubDate>Sat, 06 Nov 2010 01:30:15 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10761</guid>
		<description><![CDATA[My very good friend, Jacob Sebastian, is running a month-long SQL Quiz Series where the best-of-the-best experts from around the globe would be the quiz masters. They will ask one question every day, and users are expected to answer them correctly. The winning prizes include cool gadgets like iPAD, Kindle and many more. I am [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10761&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">My very good friend, Jacob Sebastian, is  running a month-long SQL Quiz Series where the best-of-the-best experts  from around the globe would be the quiz masters. They will ask one  question every day, and users are expected to answer them correctly. The  winning prizes include cool gadgets like iPAD, Kindle and many more.</p>
<p style="text-align:justify;">I am one of the quiz masters, and my question is published here: <a href="http://beyondrelational.com/quiz/SQLServer/General/2010/questions/sqlserver-quiz-general-2010-Pinal-Dave-How-many-clustered-indexes-will-you-have.aspx" target="_blank"><strong>The View, The Table and The Clustered Index Confusion</strong></a>. I have asked there three questions.</p>
<p style="text-align:justify;">Q1. Does the table use an index created on itself?</p>
<p style="text-align:justify;">Q2. Does the view use an index created on itself?</p>
<p style="text-align:justify;">Q3. Do both queries use the same index? If yes, why? If no, why not?</p>
<p style="text-align:justify;">Today is the last day to participate in the SQL Quiz.</p>
<p style="text-align:justify;">I have already written the answer as a hint over here: <strong> </strong><strong><a href="http://blog.sqlauthority.com/2010/10/12/sql-server-indexed-view-always-use-index-on-index/">SQL SERVER – Indexed View always Use Index on Index </a></strong></p>
<p style="text-align:justify;">Additionally, if above problem interest you, I suggest you read my article series on limitations of the view <strong> </strong><strong><a href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong></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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/readers-contribution/'>Readers Contribution</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10761/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10761/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10761/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10761/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10761/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10761/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10761/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10761&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/11/06/sqlauthority-news-last-day-to-participate-in-my-questions-at-sql-quiz/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; Indexed View always Use Index on Table</title>
		<link>http://blog.sqlauthority.com/2010/10/12/sql-server-indexed-view-always-use-index-on-index/</link>
		<comments>http://blog.sqlauthority.com/2010/10/12/sql-server-indexed-view-always-use-index-on-index/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 01:30:31 +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 Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Misconception]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10489</guid>
		<description><![CDATA[This blog post is written in response to T-SQL Tuesday hosted by Shankar Reddy. I have been recently writing about Views and their Limitations. While writing this article series, I got inspired to write about SQL Server Quiz Questions. You can view the Quiz Question posted over here. In SQL Server 2005, a single table [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10489&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><img class="alignleft" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="150" height="150" />This blog post is written in response to  <a href="http://sankarreddy.com/2010/10/invitation-to-participate-in-t-sql-tuesday-11-misconceptions-in-sql-server/" target="_blank">T-SQL Tuesday</a> hosted by <strong>Shankar Reddy</strong>.</p>
<p style="text-align:justify;">I have been recently writing about <a href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" target="_blank">Views and their Limitations</a>. While writing this article series, I got inspired to write about SQL Server Quiz Questions. You can view the Quiz Question posted over <a href="http://beyondrelational.com/quiz/SQLServer/General/2010/questions/sqlserver-quiz-general-2010-Pinal-Dave-How-many-clustered-indexes-will-you-have.aspx" target="_blank">here</a>.</p>
<p style="text-align:justify;">In SQL Server 2005, a single table can have maximum 249 non clustered indexes and 1 clustered index. In SQL Server 2008, a single table can have maximum 999 non clustered indexes and 1 clustered index. It is widely believed that a table can have only 1 clustered index, and this belief is true. I have some questions for all of you. Let us assume that I am creating view from the table itself and then create a clustered index on it. In my view, I am selecting the complete table itself.</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 sample Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">mySampleTable</span><span style="color:gray;">(</span><span style="color:black;">ID1 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">ID2 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">SomeData </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:blue;">INSERT INTO </span><span style="color:black;">mySampleTable</span><span style="color:gray;">(</span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">1000 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;">o1.name</span><span style="color:gray;">),<br />
</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;">o1.name </span><span style="color:blue;">DESC</span><span style="color:gray;">),<br />
</span><span style="color:black;">o1.name<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.all_objects o1<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">sys.all_objects o2<br />
GO<br />
</span><span style="color:green;">-- Create Index on Table<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_SampleView] </span><span style="color:blue;">ON </span><span style="color:black;">mySampleTable<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID1 </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create sample View<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">SampleView<br />
</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;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.mySampleTable<br />
GO<br />
</span><span style="color:green;">-- Create Index on View<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_ViewSample] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[SampleView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID2 </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Enable Execution Plan using CTRL + M<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">mySampleTable<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">SampleView<br />
GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">SampleView<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">mySampleTable<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Now run the following script and answer these questions:</p>
<p style="text-align:justify;">Q1. Does the table use an index created on itself?<br />
Q2. Does the view use an index created on itself?<br />
Q3. Do both the queries use the same index? If yes, why? If no, why not?</p>
<p style="text-align:justify;">The answers are very clear.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexview1_1.jpg" alt="" width="500" height="562" /></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexview2.jpg" alt="" width="500" height="563" /></p>
<p style="text-align:justify;">The answers are very clear.</p>
<p style="text-align:justify;">A1: Yes<br />
A2: No<br />
A3: Read the rest of the blog! or visit <a href="http://beyondrelational.com/forums/t/4939.aspx" target="_blank">BeyondRelationa.com</a> (where you can win iPAD as well).</p>
<p style="text-align:justify;">Usually, the assumption is that Index on table will use Index on table and Index on view will be used by view. However, that is the misconception. It does not happen this way. In fact, if you notice the image, you will find the both of them (table and view) use both the index created on the table. The index created on the view is not used. The reason for the same as listed in <a href="http://msdn.microsoft.com/en-us/library/dd171921%28SQL.100%29.aspx" target="_blank">BOL</a>.</p>
<p style="text-align:justify;">The cost of using the indexed view may exceed the cost of getting the data from the base tables, or the query is so simple that a query against the base tables is fast and easy to find. This often happens when the indexed view is defined on small tables. You can use the NOEXPAND hint if you want to force the query processor to use the indexed view. This may require you to rewrite your query if you don&#8217;t initially reference the view explicitly. You can get the actual cost of the query with NOEXPAND and compare it to the actual cost of the query plan that doesn&#8217;t reference the view. If they are close, this may give you the confidence that the decision of whether or not to use the indexed view doesn&#8217;t matter.</p>
<p style="text-align:justify;">Various answers to this question are given over <a href="http://beyondrelational.com/forums/t/4939.aspx" target="_blank">here</a>.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/misconception/'>Misconception</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10489/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10489&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/10/12/sql-server-indexed-view-always-use-index-on-index/feed/</wfw:commentRss>
		<slash:comments>6</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/TSQL2sDay.jpg" medium="image" />

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

		<media:content url="http://www.pinaldave.com/bimg/indexview2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; View Over the View Not Possible with Index View &#8211; Limitations of the View 11</title>
		<link>http://blog.sqlauthority.com/2010/09/29/sql-server-view-over-the-view-not-possible-with-index-view-limitations-of-the-view-11/</link>
		<comments>http://blog.sqlauthority.com/2010/09/29/sql-server-view-over-the-view-not-possible-with-index-view-limitations-of-the-view-11/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 01:30:08 +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 Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10358</guid>
		<description><![CDATA[Update: Please read the summary post of all the 11 Limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more… When I wrote the article about SQL SERVER – Adding Column is Expensive by Joining Table Outside View – Limitation of the Views Part 2, I had received a comment that said: “If joining [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10358&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Update</span>: Please read the summary post of all the 11 Limitations of the view <strong><a rel="bookmark" href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" target="_blank">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong></p>
<p style="text-align:justify;">When I wrote the article about <a href="http://blog.sqlauthority.com/2010/08/26/sql-server-adding-column-is-expensive-limitation-of-the-views-part-2/"><strong>SQL SERVER – Adding Column is Expensive by Joining Table Outside View – Limitation of the Views Part 2</strong></a>, I had received a comment that said:</p>
<p style="text-align:justify;">“If joining column is expensive to the view, why can’t I create a view over the view and create an index on it?”</p>
<p style="text-align:justify;">The answer is simple: <strong>It’s actually another limitation of the View.</strong></p>
<p style="text-align:justify;">You cannot create an Index on a nested View situation. The following example where we can demonstrate the issue is attached below. In this example, there is already one view, and another view is created on the top of the view. When attempting to create an index on the outer view, it would not be allowed and would throw an error.</p>
<p style="text-align:justify;">Let us see the example:</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:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sys.views </span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">N'[dbo].[vw_ViewLimit1]'</span><span style="color:gray;">))<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">[dbo].[vw_ViewLimit1]<br />
GO<br />
</span><span style="color:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sys.views </span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">N'[dbo].[vw_ViewLimit2]'</span><span style="color:gray;">))<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">[dbo].[vw_ViewLimit2]<br />
GO<br />
</span><span style="color:green;">-- Create View on sample tables<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">vw_ViewLimit1<br />
</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;">[SalesOrderID]</span><span style="color:gray;">,</span><span style="color:black;">[SalesOrderDetailID]</span><span style="color:gray;">,</span><span style="color:black;">[CarrierTrackingNumber]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[OrderQty]</span><span style="color:gray;">,</span><span style="color:black;">sod.[ProductID]</span><span style="color:gray;">,</span><span style="color:black;">[SpecialOfferID]</span><span style="color:gray;">,</span><span style="color:black;">[UnitPrice]</span><span style="color:gray;">,</span><span style="color:black;">[UnitPriceDiscount]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[LineTotal]</span><span style="color:gray;">,</span><span style="color:black;">[ReferenceOrderID]<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail sod<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Production.TransactionHistory th </span><span style="color:blue;">ON </span><span style="color:black;">sod.SalesOrderID </span><span style="color:blue;">= </span><span style="color:black;">th.ReferenceOrderID<br />
GO<br />
</span><span style="color:green;">-- Another View created on the view using the same view cread earlier<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">vw_ViewLimit2<br />
</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;">[SalesOrderID]</span><span style="color:gray;">,</span><span style="color:black;">[SalesOrderDetailID]</span><span style="color:gray;">,</span><span style="color:black;">[CarrierTrackingNumber]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[OrderQty]</span><span style="color:gray;">,</span><span style="color:black;">v1.[ProductID]</span><span style="color:gray;">,</span><span style="color:black;">[SpecialOfferID]</span><span style="color:gray;">,</span><span style="color:black;">[UnitPrice]</span><span style="color:gray;">,</span><span style="color:black;">[UnitPriceDiscount]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[LineTotal]</span><span style="color:gray;">,</span><span style="color:black;">v1.[ReferenceOrderID]<br />
</span><span style="color:gray;">,</span><span style="color:black;">th.[Quantity]<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.vw_ViewLimit1 v1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Production.TransactionHistory th </span><span style="color:blue;">ON </span><span style="color:black;">v1.SalesOrderID </span><span style="color:blue;">= </span><span style="color:black;">th.ReferenceOrderID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderDetailID </span><span style="color:gray;">&gt; </span><span style="color:black;">111111<br />
GO<br />
</span><span style="color:green;">-- Following statement will fail as view contains another view<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_vw_ViewLimit1] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[vw_ViewLimit2]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ProductID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/*<br />
Msg 1937, Level 16, State 1, Line 1<br />
Cannot create index on view 'AdventureWorks.dbo.vw_ViewLimit2' because it references another view 'dbo.vw_ViewLimit1'. Consider expanding referenced view's definition by hand in indexed view definition.<br />
*/<br />
</span></code></p>
<p style="text-align:justify;">So here it is &#8212; one more limitation. The reason for this is that another view over a view is difficult to maintain. The workaround is very simple as explained in the error messages itself. Instead of creating nesting View, just bring over the code of the inner view to the outer view. After this, it will work just fine, letting you create an index (considering the earlier 10 limitations). How many of you know this issue?</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10358/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10358/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10358/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10358/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10358/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10358/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10358/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10358&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/29/sql-server-view-over-the-view-not-possible-with-index-view-limitations-of-the-view-11/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 – Keywords View Definition Must Not Contain for Indexed View – Limitation of the View 10</title>
		<link>http://blog.sqlauthority.com/2010/09/27/sql-server-keywords-view-definition-must-not-contain-for-indexed-view-limitation-of-the-view-10/</link>
		<comments>http://blog.sqlauthority.com/2010/09/27/sql-server-keywords-view-definition-must-not-contain-for-indexed-view-limitation-of-the-view-10/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 01:30:00 +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 Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10317</guid>
		<description><![CDATA[Update: Please read the summary post of all the 11 Limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more… I have recently wrote many articles on the limitation of the views. In this article, I have tried to sum up all the keywords which are not allowed in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10317&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Update</span>: Please read the summary post of all the 11 Limitations of the view <strong><a rel="bookmark" href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" target="_blank">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong></p>
<p style="text-align:justify;">I have recently wrote many articles on the limitation of the views. In this article, I have tried to sum up all the keywords which are not allowed in the indexed view. If any of the following keyword is used in the View, Index is not possible to create on the same.</p>
<p style="text-align:justify;">I think this list can be used as quick reference for anybody who wants to create view and index it to get best out of the views.</p>
<ul style="text-align:justify;">
<li>ANY, NOT ANY</li>
<li>Arithmetic on imprecise (float, real) values</li>
<li>COMPUTE, COMPUTE BY</li>
<li>Contradictions SQL Server can detect that mean the view would be empty (for example, where 0=1 and &#8230;)</li>
<li>CONVERT producing an imprecise result</li>
<li>COUNT(*)</li>
<li>Derived tables (subquery in FROM list)</li>
<li>DISTINCT</li>
<li>EXISTS, NOT EXISTS</li>
<li>Expressions on aggregate results (for example, SUM(x)+SUM(x))</li>
<li>Full-text predicates (CONTAINS, FREETEXT, CONTAINSTABLE, FREETEXTTABLE)</li>
<li>GROUP BY ALL</li>
<li>Imprecise constants (for example, 2.34e5)</li>
<li>Inline or table-valued functions</li>
<li>MIN, MAX</li>
<li>Nondeterministic expressions</li>
<li>Non-Unicode collations</li>
<li>OPENROWSET, OPENQUERY, OPENDATASOURCE</li>
<li>OPENXML</li>
<li>ORDER BY</li>
<li>OUTER join</li>
<li>References to a base table with a disabled clustered index</li>
<li>References to a table or function in a different database</li>
<li>References to another view</li>
<li>ROWSET functions</li>
<li>Self-joins</li>
<li>STDEV, STDEVP, VAR, VARP, AVG</li>
<li>Subqueries</li>
<li>SUM on nullable expressions</li>
<li>Table hints (for example, NOLOCK)</li>
<li>text, ntext, image, filestream, or xml columns</li>
<li>TOP</li>
<li>UNION</li>
</ul>
<p style="text-align:justify;">After looking at the long list which contains Self Join, TOP, UNION, OUTER JOIN and many other useful keywords, one has to wonder how limited the usage of the query is in the view. One can not utilize the full potentials of the views.</p>
<p style="text-align:justify;">Views have many limitations and the list is limitless!</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>), <a href="http://msdn.microsoft.com/en-us/library/dd171921.aspx" target="_blank">MSDN Article</a><br />
</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10317/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10317/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10317/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10317/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10317/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10317/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10317/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10317/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10317/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10317/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10317/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10317/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10317/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10317/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10317&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/27/sql-server-keywords-view-definition-must-not-contain-for-indexed-view-limitation-of-the-view-10/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 – SELF JOIN Not Allowed in Indexed View – Limitation of the View 9</title>
		<link>http://blog.sqlauthority.com/2010/09/26/sql-server-self-join-not-allowed-in-indexed-view-limitation-of-the-view-9/</link>
		<comments>http://blog.sqlauthority.com/2010/09/26/sql-server-self-join-not-allowed-in-indexed-view-limitation-of-the-view-9/#comments</comments>
		<pubDate>Sun, 26 Sep 2010 01:30:32 +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 Joins]]></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 View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10322</guid>
		<description><![CDATA[Update: Please read the summary post of all the 11 Limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more… Previously, I wrote an article about SQL SERVER – The Self Join – Inner Join and Outer Join, and that blog post seems very popular because of its interesting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10322&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Update</span>: Please read the summary post of all the 11 Limitations of the view <strong><a rel="bookmark" href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" target="_blank">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong><br />
Previously, I wrote an article about <a href="http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/" target="_blank"><strong>SQL SERVER – The Self Join – Inner Join and Outer Join</strong></a>, and that blog post seems very popular because of its interesting points. It is quite common to think that Self Join is also only Inner Join, but the reality is that it can be anything. The concept of Self Join is very useful that we use it quite often in our coding. However, this is not allowed in the Index View. I will be using the same example  that I have created earlier for the said article.</p>
<p>Let us first create the same table for an employee. One of the columns in this table contains the ID of the manger, who is an employee of that company, at the same time. This way, all the employees and their managers are present in the same table. If we want to find the manager of a particular employee, we need to use Self Join.</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 a Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Employee</span><span style="color:gray;">(<br />
</span><span style="color:black;">EmployeeID </span><span style="color:blue;">INT PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">Name </span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">),<br />
</span><span style="color:black;">ManagerID </span><span style="color:blue;">INT<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert Sample Data<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Employee<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'Mike'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">, </span><span style="color:red;">'David'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">, </span><span style="color:red;">'Roger'</span><span style="color:gray;">, NULL<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">, </span><span style="color:red;">'Marry'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">, </span><span style="color:red;">'Joseph'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7</span><span style="color:gray;">, </span><span style="color:red;">'Ben'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
GO<br />
</span><span style="color:green;">-- Check the data<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/selfjoini1.jpg" alt="" width="243" height="155" /></p>
<p>We will now utilize Inner Join to find the employees and their managers’ details.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Inner Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/selfjoini2.jpg" alt="" width="235" height="136" /></p>
<p>Now let us try to create View on the table. This will allow well construction of the View without any issues associated with it.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Create a View<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">myJoinView<br />
</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;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">dbo.Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO<br />
</span></code></p>
<p>Now let us try to create a Clustered Index on the View.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Attempt to Create Index on View will thrown an error<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_MyJoinView] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[myJoinView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[EmployeeName] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p>Unfortunately, the above attempt will not allow you to create the Clustered Index, as evidenced by an error message. It will throw following error suggesting that SELF JOIN is now allowed in the table.</p>
<p style="text-align:justify;"><span style="color:#ff0000;">Msg 1947, Level 16, State 1, Line 2<br />
Cannot create index on view &#8220;tempdb.dbo.myJoinView&#8221;. The view contains a self join on &#8220;tempdb.dbo.Employee&#8221;.</span></p>
<p>The generic reason provided is that it is very expensive to manage the view for SQL Server when SELF JOIN is implemented in the query.</p>
<p>If any of you has a better explanation of this subject, please post it here through your comments, and I will publish it with due credit.</p>
<p>The complete script for the example is given below:</p>
<p><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 a Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Employee</span><span style="color:gray;">(<br />
</span><span style="color:black;">EmployeeID </span><span style="color:blue;">INT PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">Name </span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">),<br />
</span><span style="color:black;">ManagerID </span><span style="color:blue;">INT<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert Sample Data<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Employee<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'Mike'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">, </span><span style="color:red;">'David'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">, </span><span style="color:red;">'Roger'</span><span style="color:gray;">, NULL<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">, </span><span style="color:red;">'Marry'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">, </span><span style="color:red;">'Joseph'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7</span><span style="color:gray;">, </span><span style="color:red;">'Ben'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
GO<br />
</span><span style="color:green;">-- Check the data<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee<br />
GO<br />
</span><span style="color:green;">-- Inner Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO<br />
</span><span style="color:green;">-- Create a View<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">myJoinView<br />
</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;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">dbo.Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO<br />
</span><span style="color:green;">-- Attempt to Create Index on View will thrown an error<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_MyJoinView] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[myJoinView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[EmployeeName] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/*<br />
Msg 1947, Level 16, State 1, Line 2<br />
Cannot create index on view "tempdb.dbo.myJoinView". The view contains a self join on "tempdb.dbo.Employee".<br />
*/<br />
-- Clean up<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">myJoinView<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">Employee<br />
GO</span></code></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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10322/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10322&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/26/sql-server-self-join-not-allowed-in-indexed-view-limitation-of-the-view-9/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/selfjoini1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/selfjoini2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; UNION Not Allowed but OR Allowed in Index View &#8211; Limitation of the View 6</title>
		<link>http://blog.sqlauthority.com/2010/09/22/sql-server-union-not-allowed-but-or-allowed-in-index-view-limitation-of-the-view-6/</link>
		<comments>http://blog.sqlauthority.com/2010/09/22/sql-server-union-not-allowed-but-or-allowed-in-index-view-limitation-of-the-view-6/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 01:30:11 +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 Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Union clause]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10291</guid>
		<description><![CDATA[Update: Please read the summary post of all the 11 Limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more… If you want to create an Indexed View, you ought to know that UNION Operation is now allowed in Indexed View. It is quite surprising at times when the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10291&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Update</span>: Please read the summary post of all the 11 Limitations of the view <strong><a rel="bookmark" href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" target="_blank">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong></p>
<p style="text-align:justify;">If you want to create an Indexed View, you ought to know that UNION Operation is now allowed in Indexed View. It is quite surprising at times when the UNION operation looks very innocent and seems that it cannot be used in the View.</p>
<p style="text-align:justify;">Before an in-depth understanding this subject, let me show you a script where UNION is now allowed in Indexed View:</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:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sys.views </span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">N'[dbo].[SampleView]'</span><span style="color:gray;">))<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">[dbo].[SampleView]<br />
GO<br />
</span><span style="color:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sys.objects </span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">N'[dbo].[mySampleTable]'</span><span style="color:gray;">) AND </span><span style="color:blue;">TYPE IN </span><span style="color:gray;">(</span><span style="color:red;">N'U'</span><span style="color:gray;">))<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">[dbo].[mySampleTable]<br />
GO<br />
</span><span style="color:green;">-- Create SampleTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">mySampleTable </span><span style="color:gray;">(</span><span style="color:black;">ID1 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">ID2 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">SomeData </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:blue;">INSERT INTO </span><span style="color:black;">mySampleTable </span><span style="color:gray;">(</span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData</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;">o1.name</span><span style="color:gray;">),<br />
</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;">o2.name</span><span style="color:gray;">),<br />
</span><span style="color:black;">o2.name<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.all_objects o1<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">sys.all_objects o2<br />
GO<br />
</span><span style="color:green;">-- Create View<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">SampleView<br />
</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;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.mySampleTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID1 </span><span style="color:gray;">&lt; </span><span style="color:black;">1000<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.mySampleTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID2 </span><span style="color:gray;">&lt; </span><span style="color:black;">1000<br />
GO<br />
</span><span style="color:green;">-- Create Index on View<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_ViewSample] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[SampleView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID2 </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Above statement will thrown an error<br />
Msg 10116, Level 16, State 1, Line 1<br />
Cannot create index on view 'tempdb.dbo.SampleView' because it contains one or more UNION, INTERSECT, or EXCEPT operators. Consider creating a separate indexed view for each query that is an input to the UNION, INTERSECT, or EXCEPT operators of the original view.<br />
*/<br />
-- Aleter View to replace COUNT with BIG_COUNT<br />
</span><span style="color:blue;">ALTER VIEW </span><span style="color:black;">SampleView<br />
</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;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.mySampleTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID1 </span><span style="color:gray;">&lt; </span><span style="color:black;">1000 </span><span style="color:gray;">OR </span><span style="color:black;">ID2 </span><span style="color:gray;">&lt; </span><span style="color:black;">1000<br />
GO<br />
</span><span style="color:green;">-- Now let us create Index again - this time successfully<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_ViewSample] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[SampleView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID2 </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">During the script, the following ERROR would occur if you try to create the index while UNION operation is in the view:</p>
<p style="text-align:justify;"><span style="color:#ff0000;">Msg 10116, Level 16, State 1, Line 1<br />
Cannot create index on view &#8216;tempdb.dbo.SampleView&#8217; because it contains one or more UNION, INTERSECT, or EXCEPT operators. Consider creating a separate indexed view for each query that is an input to the UNION, INTERSECT, or EXCEPT operators of the original view.</span></p>
<p style="text-align:justify;">In contrast to this converting the UNION to OR operation would give the same result, plus it would allow you to create an index on the View. Well, our example is one in which we are able to re-write the script with OR clause. However, keep in mind that there can be cases where it is not possible to re-write and you might end up not using Views with Index.</p>
<p style="text-align:justify;">In this series, I have been writing about many limitations and their explanation. Now here are my questions for you:</p>
<ul style="text-align:justify;">
<li><strong><em>What do you think is the reason behind these limitations</em></strong>?</li>
<li><strong><em>Why UNION is not allowed in the View?</em></strong></li>
</ul>
<p style="text-align:justify;">I will publish your answer with due credit on the blog.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/sql-union-clause/'>SQL Union clause</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10291/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10291&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/22/sql-server-union-not-allowed-but-or-allowed-in-index-view-limitation-of-the-view-6/feed/</wfw:commentRss>
		<slash:comments>12</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; COUNT(*) Not Allowed but COUNT_BIG(*) Allowed &#8211; Limitation of the View 5</title>
		<link>http://blog.sqlauthority.com/2010/09/21/sql-server-count-not-allowed-but-count_big-allowed-limitation-of-the-view-5/</link>
		<comments>http://blog.sqlauthority.com/2010/09/21/sql-server-count-not-allowed-but-count_big-allowed-limitation-of-the-view-5/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 01:30:03 +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 Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10284</guid>
		<description><![CDATA[Update: Please read the summary post of all the 11 Limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more… One of the most prominent limitations of the View it is that it does not support COUNT(*); however, it can support COUNT_BIG(*) operator. In the following case, you see [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10284&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Update</span>: Please read the summary post of all the 11 Limitations of the view <strong><a rel="bookmark" href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" target="_blank">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong></p>
<p style="text-align:justify;">One of the most prominent limitations of the View it is that it does not support COUNT(*); however, it can support COUNT_BIG(*) operator. In the following case, you see that if View has COUNT (*) in it already, it cannot have a clustered index on it. On the other hand, a similar index would be created if we change the COUNT (*) to COUNT_BIG (*).For an easier understanding of this topic, let us see the example here.</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:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sys.views </span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">N'[dbo].[SampleView]'</span><span style="color:gray;">))<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">[dbo].[SampleView]<br />
GO<br />
</span><span style="color:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sys.objects </span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">N'[dbo].[mySampleTable]'</span><span style="color:gray;">) AND </span><span style="color:blue;">TYPE IN </span><span style="color:gray;">(</span><span style="color:red;">N'U'</span><span style="color:gray;">))<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">[dbo].[mySampleTable]<br />
GO<br />
</span><span style="color:green;">-- Create SampleTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">mySampleTable </span><span style="color:gray;">(</span><span style="color:black;">ID1 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">ID2 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">SomeData </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:blue;">INSERT INTO </span><span style="color:black;">mySampleTable </span><span style="color:gray;">(</span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">SomeData</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;">o1.name</span><span style="color:gray;">),<br />
</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;">o2.name</span><span style="color:gray;">),<br />
</span><span style="color:black;">o2.name<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.all_objects o1<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">sys.all_objects o2<br />
GO<br />
</span><span style="color:green;">-- Create View<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">SampleView<br />
</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:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:black;">TableCount</span><span style="color:gray;">, </span><span style="color:black;">ID2<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.mySampleTable<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID2<br />
GO<br />
</span><span style="color:green;">-- Create Index on View<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_ViewSample] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[SampleView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID2 </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Above statement will thrown an error<br />
Msg 10136, Level 16, State 1, Line 1<br />
Cannot create index on view "tempdb.dbo.SampleView" because it uses the aggregate COUNT. Use COUNT_BIG instead.<br />
*/<br />
-- Aleter View to replace COUNT with BIG_COUNT<br />
</span><span style="color:blue;">ALTER VIEW </span><span style="color:black;">SampleView<br />
</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;">COUNT_BIG</span><span style="color:gray;">(*) </span><span style="color:black;">TableCount</span><span style="color:gray;">, </span><span style="color:black;">ID2<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.mySampleTable<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID2<br />
GO<br />
</span><span style="color:green;">-- Now let us create Index again - this time successfully<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_ViewSample] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[SampleView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID2 </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Here is a screenshot of an error that occurred when Views used COUNT(*) and there was an attempt to create an Index on it.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/limitview_5.jpg" alt="" width="498" height="106" /></p>
<p style="text-align:justify;">If you are wondering about the reason behind allowing COUNT_BIG and COUNT, here is a quick explanation for this. <a href="http://www.sql.co.il/books/insidetsql2008/" target="_blank"><strong>Itzik Ben-Gan</strong></a> explained me the reason for this situation. He has also clarified the reasons in his book series, &#8216;<strong>Inside T-SQL</strong>&#8216;. Here is a concise summary of the explanation. This response from <strong>Itzik </strong>is produced unaltered and with his permissions:</p>
<p style="text-align:justify;padding-left:30px;"><em>If the query is a grouped query, SQL Server needs to keep track of the count in each group in order to known whether a group needs to be modified or removed altogether upon DELETE/UPDATE of rows against the underlying tables. As for why the COUNT_BIG and not just COUNT, since SQL Server materializes the counts along with the rest of the view’s data, I guess this has to do with support for groups with more rows than the maximum four-byte integer.</em></p>
<p style="text-align:justify;padding-left:30px;"><em>BTW, unrelated to views but along similar lines, see what happens if you add to a clustered table more than the maximum four-byte integer number of rows with the same non-unique clustered index key. The uniqueifiers SQL Server uses internally to distinguish between rows with the same clustered index key is a four-byte integer. Once it overflows, you get error 666 and are not allowed to add more rows with the same clustered index key.</em></p>
<p style="text-align:justify;padding-left:30px;"><em>Now, with uniqueifiers for clustering keys I understand the choice to go for four bytes since there are great space savings and therefore read performance benefits as a result, and we are talking about an extreme case for this to happen . But with grouped queries, usually the number of groups is not too large, but groups themselves can be large. Imagine a situation where you try to add more rows to a table that has an indexed view and SQL Server rejects the insert because of a four-byte int overflow in the target group count.</em></p>
<p style="text-align:justify;">I hope it is clear now. If you want to learn more about this, you can continue reading his book <a href="http://www.sql.co.il/books/insidetsql2008/" target="_blank">Inside T-SQL</a>.</p>
<p style="text-align:justify;">Let me know what you think of these limitations, as well as your opinions about the example.</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10284/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10284&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/21/sql-server-count-not-allowed-but-count_big-allowed-limitation-of-the-view-5/feed/</wfw:commentRss>
		<slash:comments>8</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/limitview_5.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Disabled Index and Index Levels and B-Tree</title>
		<link>http://blog.sqlauthority.com/2010/09/14/sql-server-disabled-index-and-index-levels-and-b-tree/</link>
		<comments>http://blog.sqlauthority.com/2010/09/14/sql-server-disabled-index-and-index-levels-and-b-tree/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 01:30:14 +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=10170</guid>
		<description><![CDATA[This blog post is written in response to T-SQL Tuesday hosted by Michael J. Swart. Recently, I presented a session at the Microsoft Bangalore office. Everybody eagerly wanted to learn more, to the extent that they wanted a mentor to train each of them in order to move on to the next level. I have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10170&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">This blog post is written in response to  <a href="http://michaeljswart.com/?p=844" target="_blank">T-SQL Tuesday</a> hosted by <strong>Michael J. Swart</strong>.</p>
<p style="text-align:justify;"><img class="alignleft" style="margin:5px;" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="150" height="150" />Recently, I presented a session at the Microsoft Bangalore office. Everybody eagerly wanted to learn more, to the extent that they wanted a mentor to train each of them in order to move on to the next level. I have many mentors worldwide as I keep on traveling, in addition to being already a part of Solid Quality Mentors. However, if I have to take one name in India, I will take the name of <a href="http://blogs.sqlxml.org/vinodkumar/archive/2010/09/03/performance-tuning-statistics-io.aspx" target="_blank"><strong>Vinod Kumar</strong></a>, who has given me many insights and helped me to become a better presenter of my sessions. I recently met him at his office at Microsoft Bangalore. He was very kind and helped several attendees to think about the very subject I was discussing with them. During the discussion, he has asked a single interesting question:</p>
<p style="text-align:justify;"><strong>What will be the status of the B-Tree structure when index is disabled?</strong></p>
<p style="text-align:justify;">He also gave a hint: use the script which I have used earlier over<a href="http://blog.sqlauthority.com/2010/07/04/sql-server-index-levels-page-count-record-count-and-dmv-%C2%A0sys-dm_db_index_physical_stats/" target="_blank"><strong> SQL SERVER – Index Levels, Page Count, Record Count and DMV &#8211; sys.dm_db_index_physical_stats</strong></a>.</p>
<p style="text-align:justify;">Well, let us follow the same advice and hint given by Vinod Kumar so we can try to answer his question.</p>
<p style="text-align:justify;">In the following script, the following operations are to be done:</p>
<ul style="text-align:justify;">
<li>Create Table</li>
<li>Create Clustered Index</li>
<li>Check the Index Levels</li>
<li>Disable Index</li>
<li>Check the Index Levels</li>
</ul>
<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 IndexTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">IndexTable </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">800</span><span style="color:gray;">),<br />
</span><span style="color:black;">FirstName </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">2000</span><span style="color:gray;">),<br />
</span><span style="color:black;">LastName </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">3000</span><span style="color:gray;">),<br />
</span><span style="color:black;">City </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">2200</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create Clustered Index<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_IndexTable_ID] </span><span style="color:blue;">ON </span><span style="color:black;">IndexTable<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<br />
</span><span style="color:green;">-- Insert one Million Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">IndexTable </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;">100 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 </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<br />
</span><span style="color:green;">-- Check the Index Level<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_page_space_used_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_fragmentation_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">index_level<br />
</span><span style="color:gray;">,</span><span style="color:black;">record_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">page_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">fragment_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_record_size_in_bytes<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats</span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(</span><span style="color:red;">'TempDb'</span><span style="color:gray;">),</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'IndexTable'</span><span style="color:gray;">),NULL,NULL,</span><span style="color:red;">'DETAILED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Disable the Index<br />
</span><span style="color:blue;">ALTER INDEX </span><span style="color:black;">[IX_IndexTable_ID] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[IndexTable] DISABLE<br />
GO<br />
</span><span style="color:green;">-- Check the Index Level<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_page_space_used_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_fragmentation_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">index_level<br />
</span><span style="color:gray;">,</span><span style="color:black;">record_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">page_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">fragment_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_record_size_in_bytes<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats</span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(</span><span style="color:red;">'TempDb'</span><span style="color:gray;">),</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'IndexTable'</span><span style="color:gray;">),NULL,NULL,</span><span style="color:red;">'DETAILED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">IndexTable<br />
GO<br />
</span></code><br />
Let us now observe the output now.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/disabledIndex.jpg" alt="" width="500" height="189" /></p>
<p style="text-align:justify;">We can clearly see from the output that B-Tree structure is no more maintained after disabling the index. This means that the index is no more applicable.</p>
<p style="text-align:justify;">I want to thank Vinod for bringing up a very interesting subject. He made me realize I had not blogged about it yet before.</p>
<p style="text-align:justify;">I suggest that you also read my other articles in the series by searching <strong>&#8216;Disabled Index&#8217;</strong> at <strong><a href="http://search.sqlauthority.com" target="_blank">Search@SQLAuthority.com</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10170/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10170&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/14/sql-server-disabled-index-and-index-levels-and-b-tree/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/TSQL2sDay.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/disabledIndex.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Index Levels and Delete Operations &#8211; Page Level Observation</title>
		<link>http://blog.sqlauthority.com/2010/09/07/sql-server-index-levels-and-delete-operations-page-level-observation/</link>
		<comments>http://blog.sqlauthority.com/2010/09/07/sql-server-index-levels-and-delete-operations-page-level-observation/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 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 Index]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></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=10131</guid>
		<description><![CDATA[I wrote an article before on SQL SERVER – Index Levels, Page Count, Record Count and DMV &#8211; sys.dm_db_index_physical_stats. In that article, I promised that I would give a follow up post with a few more interesting details. I suggest that you go over the earlier article first to understand the details on B-Tree and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10131&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I wrote an article before on <a href="http://blog.sqlauthority.com/2010/07/04/sql-server-index-levels-page-count-record-count-and-dmv-%C2%A0sys-dm_db_index_physical_stats/" target="_blank"><strong>SQL SERVER – Index Levels, Page Count, Record Count and DMV &#8211; sys.dm_db_index_physical_stats</strong></a>. In that article, I promised that I would give a follow up post with a few more interesting details. I suggest that you go over the earlier article first to understand the details on B-Tree and Index Level. Today we will see one of the fascinating aspects of Delete Operations.</p>
<p style="text-align:justify;"><strong>Update:</strong> This blog post contained few factual errors and they were clearly pointed out by <strong><a href="http://twitter.com/HrvojePiasevoli" target="_blank">Hrvoje Piasevoli</a></strong> over <a href="http://blog.sqlauthority.com/2010/09/07/sql-server-index-levels-and-delete-operations-page-level-observation/#comment-86941" target="_blank">here</a>. Based on his comment, I have modified this blog post. I will include the comment at the bottom of the blog post for additional clarification. I thank <strong>Hrvoje </strong>for taking time to correct the details.</p>
<p style="text-align:justify;">There are a few questions I often encounter during my training sessions. Let me try to answer two of them today.</p>
<p style="text-align:justify;"><strong>Q:</strong> When I delete any data from a table, does SQL Server reduce the size of that table?</p>
<p style="text-align:justify;"><strong>A:</strong> When data are deleted from any table, the SQL Server does not reduce the size of the table right away, but marks those pages as <strong>free pages,</strong> showing that they belong to the table. When new data are inserted, they are put into those pages first. Once those pages are filled up, SQL Server will allocate new pages. If you wait for sometime background process de-allocates the pages and finally reducing the page size.  Follow the example below.</p>
<p style="text-align:justify;"><strong>Q:</strong> When I delete any data from a table, does SQL Server reduce the size of the B-Tree or change the level of the B-Tree since there are lesser data?</p>
<p style="text-align:justify;"><strong>A:</strong> No. It does very different behavior. Follow the example.</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 FragTable<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">FragTable </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">800</span><span style="color:gray;">),<br />
</span><span style="color:black;">FirstName </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">2000</span><span style="color:gray;">),<br />
</span><span style="color:black;">LastName </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">3000</span><span style="color:gray;">),<br />
</span><span style="color:black;">City </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">2200</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create Clustered Index<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_FragTable_ID] </span><span style="color:blue;">ON </span><span style="color:black;">FragTable<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<br />
</span><span style="color:green;">-- Insert one Million Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">FragTable </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;">100 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 </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<br />
</span><span style="color:green;">-- Check the spaces<br />
</span><span style="color:darkred;">sp_spaceused </span><span style="color:red;">'FragTable'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Check the percentages<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_page_space_used_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_fragmentation_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">index_level<br />
</span><span style="color:gray;">,</span><span style="color:black;">record_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">page_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">fragment_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_record_size_in_bytes<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats</span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(</span><span style="color:red;">'TempDb'</span><span style="color:gray;">),</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'FragTable'</span><span style="color:gray;">),NULL,NULL,</span><span style="color:red;">'DETAILED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Delete all from table<br />
</span><span style="color:blue;">DELETE<br />
FROM </span><span style="color:black;">FragTable<br />
GO<br />
</span><span style="color:green;">-- Check the spaces<br />
</span><span style="color:darkred;">sp_spaceused </span><span style="color:red;">'FragTable'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Check the percentages<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">avg_page_space_used_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_fragmentation_in_percent<br />
</span><span style="color:gray;">,</span><span style="color:black;">index_level<br />
</span><span style="color:gray;">,</span><span style="color:black;">record_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">page_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">fragment_count<br />
</span><span style="color:gray;">,</span><span style="color:black;">avg_record_size_in_bytes<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_physical_stats</span><span style="color:gray;">(</span><span style="color:magenta;">DB_ID</span><span style="color:gray;">(</span><span style="color:red;">'TempDb'</span><span style="color:gray;">),</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'FragTable'</span><span style="color:gray;">),NULL,NULL,</span><span style="color:red;">'DETAILED'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">FragTable<br />
GO</span></code></p>
<p style="text-align:justify;">Now let us check the details. Here, we check the index details before and after the delete statement.</p>
<p style="text-align:justify;">
<div class="mceTemp" style="text-align:justify;">
<dl class="wp-caption alignnone">
<dt class="wp-caption-dt"><img src="http://www.pinaldave.com/bimg/indexlevel.jpg" alt="" width="499" height="229" /></dt>
<dd class="wp-caption-dd">Click on Image to See Larger Image</dd>
</dl>
</div>
<p style="text-align:justify;">
<p style="text-align:justify;">It is very clear from the example that after deleting data,  the size of the table is not reduced; same goes with the levels of the indexes- they are not resized by SQL Server. What really changes is the leaf level pages where data are stored.</p>
<p style="text-align:justify;">However if you wait for some time, and run once again fragmentation script you will noticed that levels are still the same but the page_count has been reduced to 1. This is clearly explained by <strong>Hrvoje </strong>in his comment correcting the blog post over <strong><a href="http://blog.sqlauthority.com/2010/09/07/sql-server-index-levels-and-delete-operations-page-level-observation/#comment-86941" target="_blank">here</a></strong>. The part of technical explanation is reproduced here.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexlevel1.jpg" alt="" width="501" height="109" /></p>
<p style="text-align:justify;">&#8220;The behavior differs for Heaps and tables with a clustered index.  (The following applies to SQL Server 2008 SP1 and it might differ on  other versions)</p>
<p style="text-align:justify;">Heaps:<br />
BOL: “When rows are deleted from a heap the Database Engine may use row  or page locking for the operation. As a result, the pages made empty by  the delete operation remain allocated to the heap. When empty pages are  not deallocated, the associated space cannot be reused by other objects  in the database.”<br />
While it may appear that the pages are not deallocated due to you  example set size, try increasing the number of inserted rows and you  will see that the table size gets reduced to around 25MB. The number of  index pages reduces accordingly. (Levels do not apply to heaps)<br />
If on the other hand you put a TABLOCK hint in the delete statement all pages will get deallocated.</p>
<p style="text-align:justify;">Tables with a Clustered Index:<br />
Deleting from a table with a clustered index will deallocate deleted  pages regardless of the TABLOCK hint. The difference is that without the  hint it will happen by a background clean up process and therefore  might not be visible immediately after the delete. Try waiting for a few  seconds and check space used. TABLOCK will make this operation  synchronous.<br />
Number index levels are kept the same, BUT the size of ALL levels gets reduced and not just the leaf levels.</p>
<p style="text-align:justify;">Reducing the size of the heap or the levels of indexes in the B-Tree  can be achieved in several ways: if the table is empty (and other  constraints satisfied) TRUNCATE TABLE will do the work. Other methods  are rebuilding the table/clustered index.&#8221; Read original comment <strong><a href="http://blog.sqlauthority.com/2010/09/07/sql-server-index-levels-and-delete-operations-page-level-observation/#comment-86941" target="_blank">here.</a></strong></p>
<p style="text-align:justify;">Do you find this information useful? What is your opinion about this matter?</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>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-interview-questions-and-answers/'>SQL Interview Questions and Answers</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10131/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10131&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/07/sql-server-index-levels-and-delete-operations-page-level-observation/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/indexlevel.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/indexlevel1.jpg" medium="image" />
	</item>
		<item>
		<title>SQLAuthority News – SQL Server Health Check Service – Speed UP SQL Server</title>
		<link>http://blog.sqlauthority.com/2010/09/02/sqlauthority-news-sql-server-health-check-service-speed-up-sql-server/</link>
		<comments>http://blog.sqlauthority.com/2010/09/02/sqlauthority-news-sql-server-health-check-service-speed-up-sql-server/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 01:30:23 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[About Me]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<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[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=9984</guid>
		<description><![CDATA[In my earlier article SQLAuthority News – Training and Consultancy and Travel – Story of Last 30 Days I had mentioned that I prefer to do 50% consultation and 50% training. Since then I often receive what do I do consultation for and what is my expertise. I am basically man of the performance tuning. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9984&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><img class="alignleft" style="margin:5px;" src="http://www.pinaldave.com/bimg/healthcheck.jpg" alt="" width="250" height="146" />In my earlier article <strong><a href="http://blog.sqlauthority.com/2010/06/03/sqlauthority-news-training-and-consultancy-and-travel-story-of-30-last-30-days/" target="_blank">SQLAuthority News – Training and Consultancy and Travel – Story of Last 30 Days</a></strong> I had mentioned that I prefer to do 50% consultation and 50% training. Since then I often receive what do I do consultation for and what is my expertise. I am basically man of the performance tuning. I love to tune servers and I love to speed up queries. I often get queries what do I do when I go to performance tuning. Here I am listing my complete service descriptions.</p>
<p style="text-align:justify;">This whole exercise can be done remotely as well on site. The advantage of on site is that I am more involved with local DBAs and the training part is much more enjoyable. I have often got very good relations with the local DBA and we still talk when they have issues with server. There is no replacement of the human connections. The advantage of the remote is there is no travel involved and can be economic when the location is very remote or there is urgent need of performance.</p>
<h3 style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-performance-tuning/" target="_blank">Please read more about this service over here</a></h3>
<p style="text-align:justify;">
<p style="text-align:justify;"><strong>SQL Server Health Check Service</strong></p>
<p style="text-align:justify;">SQL Server Health Check is a very challenging subject that requires expertise in Database Administration and Database Development. Solid Quality Mentors provides specialized health check service where we dive deep into your SQL Server configurations and potential issues and propose solutions to resolve your issues. We also provide maintenance scripts and train your team on few of the key areas in order to empower them to understand and tune the systems in the future. We also provide Hands-On sessions for tuning your queries and propose solutions for your deadlock situations.</p>
<h3 style="text-align:justify;"><strong><a href="http://www.pinaldave.com/sql-download/SQLServerHealthCheck.pdf" target="_blank">Download the HealthCheck PDF details from here</a></strong></h3>
<p style="text-align:justify;">If you need this service drop me an email at<strong> pinal &#8220;at&#8221; SQLAuthority.com</strong> and<span style="color:#888888;"><strong> <span style="color:#000000;">IndiaInfo &#8220;at&#8221; SolidQ.com</span></strong></span>. If you specify <span style="color:#ff0000;"><strong>SQLAuthority.com</strong></span> in Title, you will avail special discount in overall rates.</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;">
<br />Filed under: <a href='http://blog.sqlauthority.com/category/about-me/'>About Me</a>, <a href='http://blog.sqlauthority.com/category/best-practices/'>Best Practices</a>, <a href='http://blog.sqlauthority.com/category/database/'>Database</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sql-training/'>SQL Training</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-news/'>SQLAuthority News</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9984/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9984/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9984/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9984/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9984/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9984/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9984/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9984/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9984/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9984/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9984/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9984/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9984/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9984/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9984&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/02/sqlauthority-news-sql-server-health-check-service-speed-up-sql-server/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/healthcheck.jpg" medium="image" />
	</item>
	</channel>
</rss>
