<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; indexes</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/indexes/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Managing Index Fragmentation with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622</link>
		<comments>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:24:41 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[index fragmentation]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622</guid>
		<description><![CDATA[Here&#8217;s a Powershell script that can be used to manage index fragmentation in SQL Server databases. The strategy I&#8217;ve used in the script is based on a recommendation from Pinal Dave (blog &#124; twitter) in his article&#160; Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script. Just set the $sqlserver and $database variables [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622">Managing Index Fragmentation with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that can be used to manage index fragmentation in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> databases. The strategy I&#8217;ve used in the script is based on a recommendation from Pinal Dave (<a href="http://blog.sqlauthority.com" target="_blank">blog</a> | <a href="http://twitter.com/pinaldave" target="_blank">twitter</a>) in his article&nbsp; <a href="http://blog.sqlauthority.com/2007/12/22/sql-server-difference-between-index-rebuild-and-index-reorganize-explained-with-t-sql-script/" target="_blank">Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script</a>. Just set the <strong>$sqlserver </strong>and <strong>$database </strong>variables to something appropriate for your environment. Enjoy!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p622code2'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6222"><td class="code" id="p622code2"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Load SMO</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">'Microsoft.SqlServer.SMO'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
&nbsp;
<span style="color: #008000;"># Set sql server and database name here</span>
<span style="color: #800080;">$sqlserver</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost\sql2005&quot;</span>;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;AdventureWorks&quot;</span>;
&nbsp;
<span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Server&quot;</span> <span style="color: #800080;">$sqlserver</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Database&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800080;">$database</span><span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #008000;"># Get table count</span>
<span style="color: #800080;">$table_count</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Tables.Count;
<span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># First script out the drops</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$table</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Tables<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking table $table&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$table_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing indexes&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
	<span style="color: #800080;">$i</span><span style="color: pink;">++</span>;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$index</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$table</span>.Indexes<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$index_name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$index</span>.Name;
		<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking table $table&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$table_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing index $index_name&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
		<span style="color: #008000;"># Get the fragmentation stats</span>
		<span style="color: #800080;">$frag_stats</span> <span style="color: pink;">=</span> <span style="color: #800080;">$index</span>.EnumFragmentation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #008000;"># Get the properties we need to work with the index</span>
		<span style="color: #800080;">$frag_stats</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ForEach-Object</span> <span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$Index_Name</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index_Name;
						<span style="color: #800080;">$Index_Type</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index_Type;
						<span style="color: #800080;">$Average_Fragmentation</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.AverageFragmentation;
									<span style="color: #000000;">&#125;</span>;
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$Index_Type $Index_Name has a fragmentation percentage of $Average_Fragmentation&quot;</span>;
&nbsp;
		<span style="color: #008000;"># Here we decide what to do based on the level on fragmentation</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">40.00</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;$Index_Name is more than 40% fragmented and will be rebuilt.&quot;</span>;
			<span style="color: #800080;">$index</span>.Rebuild<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$Index_Name has been rebuilt.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-ge</span> <span style="color: #804000;">10.00</span> <span style="color: #FF0000;">-and</span> <span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">40.00</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;$Index_Name is between 10-40% fragmented and will be reorganized.&quot;</span>;
			<span style="color: #800080;">$index</span>.Reorganize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$Index_Name has been reorganized.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;$Index_Name is healthy, with $Average_Fragmentation% fragmentation, and will be left alone.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Finished processing <span style="color: #008080; font-weight: bold;">`&quot;</span>$database<span style="color: #008080; font-weight: bold;">`&quot;</span> indexes.&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Done&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #804000;">2</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWork_Index_Management.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="AdventureWork Index Management with Powershell" border="0" alt="AdventureWork Index Management thumb Managing Index Fragmentation with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWork_Index_Management_thumb.png" width="644" height="373" /></a></p>
<p><map name='google_ad_map_622_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/622?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_622_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=622&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmanaging-index-fragmentation-with-powershell%2F622' title="Managing Index Fragmentation with Powershell" alt=" Managing Index Fragmentation with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622">Managing Index Fragmentation with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

