﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Luke Campbell  / SQL Server 2005 Index Maintenance / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 11:53:07 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: SQL Server 2005 Index Maintenance</title><link>http://www.sqlservercentral.com/Forums/Topic477322-1239-1.aspx</link><description>I have modified the script for removing and for improving the compatibility.....USE [master]GOSET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGO/*********** 03/26/2008 - Luke Campbell - uspIndexMaintenance. This procedure reorganizes any indexes (NON XML) that have a pagecount greater than 10 and a fragmentation level greater than 20%. If the fragmentation level is 30% or above the index will be rebuilt using the ONLINE parameter (enterprise edition only). Sorting will be done in the TEMPDB to decrease the maintenance time interval. TEMPDB must have sufficient space available otherwise remove "SORT_IN_TEMPDB=ON"before creating this script in the Master db. Modified by lli :   2009/02/09				Bug : double the reindex on the same table				Bug : Partion Table not fully compatible				Bug : remove  Maint.dbo.DBIndexInfo for logging purposes									*/create PROCEDURE [dbo].[uspIndexMaintenance] @dbname varchar(50)--ASBEGIN--DECLARE @dbname varchar(50)--Set @dbname = ''--Setup dynamic SQL to run. Otherwise the Procedure needs to be deployed to each db. DECLARE @SQL varchar(4000)SET @SQL = 'Use ' + @dbname + '--Declare variables hereDECLARE @objectid intDECLARE @indexid intDECLARE @partitioncount bigintDECLARE @schemaname varchar(130)DECLARE @objectname varchar(130)DECLARE @indexname varchar(130)DECLARE @partitionnum bigintDECLARE @partitions bigintDECLARE @frag intDECLARE @command varchar(4000)--Used for loopingDECLARE @max intDECLARE @min int--Create tables to hold Index information about each table in the databaseCREATE TABLE #IndexList (ID int IDENTITY(1,1), DBName varchar(50), objectID int,indexID int,IndexType varchar(30),frag int,avg_fragment_size_in_pages int,page_count int,partition_number int)--Create indexes on #IndexListCREATE CLUSTERED INDEX IX_CL_IndexList ON #IndexList(ID)--Get Index statsINSERT #IndexList (DBName, objectID, indexID, IndexType, frag, avg_fragment_size_in_pages, page_count, partition_number)	SELECT DB_Name(database_id) AS DBName,		object_id,		index_id,		index_type_desc AS IndexType,		avg_fragmentation_in_percent, 		avg_fragment_size_in_pages,		page_count,		partition_number FROM sys.dm_db_index_physical_stats 		(DB_ID(), NULL, NULL, NULL, ''LIMITED'')	WHERE page_count &amp;gt; 10 AND index_id &amp;gt; 0 AND avg_fragmentation_in_percent &amp;gt; 20 AND		index_type_desc NOT LIKE ''%XML%''--Begin reorganizing or rebuilding indexes. Determined by level of fragmentation.--Rebuilding of indexes will use the SORT_IN_TEMPDB and ONLINE parameters.SELECT @max = (SELECT max(ID) FROM #IndexList)SELECT @min = 1WHILE @min &amp;lt;= @maxBEGIN	SELECT @objectid = objectID,		@indexid = indexID,		@partitionnum = partition_number,		@frag = frag FROM #IndexList 	WHERE ID = @min	SELECT @objectname = o.name, @schemaname = s.name	FROM sys.objects AS o	JOIN sys.schemas AS s ON s.schema_id = o.schema_id	WHERE o.object_id = @objectid	SELECT @indexname = name	FROM sys.indexes	WHERE object_id = @objectid AND index_id = @indexid;	SELECT @partitioncount = count(*)	FROM sys.partitions	WHERE object_id = @objectid AND index_id = @indexid--Reorganize or Rebuild--Inserts Index information into Maint.dbo.DBIndexInfo for logging purposes	--INSERT Maint.dbo.DBIndexInfo (DBName, TableName, IndexName, Status, CreationDate)	--VALUES ('''+ @dbname +''', @objectname, @indexname, ''Start'', Getdate())IF @frag &amp;lt; 30	begin		SET @command = ''ALTER INDEX '' + @indexname + '' ON '' + @schemaname + ''.'' + @objectname +'' REORGANIZE'';		IF @partitioncount &amp;gt; 1				SET @command = @command + '' PARTITION ='' + CAST(@partitionnum AS varchar(10));		--PRINT @command		endIF @frag &amp;gt;= 30	begin		SET @command = ''ALTER INDEX '' + @indexname + '' ON '' + @schemaname + ''.'' + @objectname +'' REBUILD ''		IF @partitioncount &amp;gt; 1				SET @command = @command + '' PARTITION ='' + CAST(@partitionnum AS varchar(10));				SET @command = @command + ''WITH(SORT_IN_TEMPDB = ON)''; --FILLFACTOR = 90, ONLINE = ON, 		--PRINT @command		end 		PRINT @commandEXEC (@command)--INSERT Maint.dbo.DBIndexInfo (DBName, TableName, IndexName, Status, CreationDate)--	VALUES ('''+ @dbname +''', @objectname, @indexname, ''End'', Getdate())SET @min = @min+1ENDPRINT ''Index maintenance on the '+@dbname +' database has completed.''--select * from maint.dbo.DBIndexInfoDROP TABLE #IndexList'--PRINT @SQLEXEC (@SQL)ENDGOUSE [master]GOSET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGO/*********** 03/26/2008 - Luke Campbell uspDBIndexCycle ************Script will cycle through each database on the server and execute the sp_MaintAutoReindex2005 procedure.*/create PROCEDURE [dbo].[uspDBIndexCycle]ASBEGINDECLARE @sql varchar(300)DECLARE @dbname sysnameDECLARE @min intDECLARE @max int--Create our temp table to hold all database names on the serverCREATE TABLE #database ([ID] int IDENTITY(1,1), [Name] varchar(60))----Create Clustered Index on the name column &amp; and insert valuesCREATE CLUSTERED INDEX IX_CL_database ON #database([Name])INSERT INTO #database ([Name])SELECT [name] FROM sys.databases WHERE [name] NOT IN('master','tempdb','model','msdb')--Build our loopSELECT @max = (SELECT max(ID) FROM #database)SELECT @min = 1WHILE @min &amp;lt;= @maxBEGINSELECT @dbname = [Name] FROM #database WHERE ID = @minSELECT @sql = 'EXEC uspIndexMaintenance ''' + @dbname +''''PRINT @sqlEXEC (@sql)SET @min = @min+1END--Clean upDROP TABLE #databaseEND</description><pubDate>Mon, 09 Feb 2009 04:08:53 GMT</pubDate><dc:creator>zoltix</dc:creator></item><item><title>SQL Server 2005 Index Maintenance</title><link>http://www.sqlservercentral.com/Forums/Topic477322-1239-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Maintenance/62695/"&gt;SQL Server 2005 Index Maintenance&lt;/A&gt;[/B]</description><pubDate>Mon, 31 Mar 2008 18:00:57 GMT</pubDate><dc:creator>Luke C</dc:creator></item></channel></rss>