2. Index defragmentation
To see how much all your indexes in each database are fragmentet, you can run this script (Code 2). The script can be set to execute a defragmentation of all indexes greater than ex. 30%.
The output from the above code is something like this, where 2 indexes are more than 30% fragmentet:
WOULD be executing ALTER INDEX spt_valuesclust ON spt_values REORGANIZE WITH ( LOB_COMPACTION = ON ) - Fragmentation currently 30%
WOULD be executing ALTER INDEX ix2_spt_values_nu_nc ON spt_values REORGANIZE WITH ( LOB_COMPACTION = ON ) - Fragmentation currently 33%
If you want to see many details of all indexes in one database, you can use this script (Code 3).
Now we will create a Index defragmentattion job under our Defrag-Cleanup-MaintenancePlan.
- Open the Defrag-Cleanup-MaintenancePlan, click Add Subplan, name it ex. 'Index Defrag', write some description and choose a Schedule. Look for other jobs and plans in Sql Manager, in the Windows Scheduler or maybe a scheduled backup program, so you dont begin your defragmentation in the middle of something else. I have choosen a schedule every week on a sunday at 2.00 AM. Save the new subplan (Ctrl-S).
- Now the big question is: Do we want to Rebuild or Reorganize the Index? For a technical explanation see BOL or a good article here.Rebuilding an Index is generally more effective that Reorganizing, but it takes more resources and can give locking while it is running. So in this case we choose a Reorganization of our indexes. Drag a 'Reorganize Index Task' box from the left pane to the subplan area, double-click it and select 'All user databases'. Click 'Ok' and save it, Ctrl + S.
That's it! You can test it by right-clicking on the job in Sql Server Agent->Jobs list and click on Start Job at Step...
Code 2. Index defragmentation code
The following code is for Sql server 2005 + 2008.
The variable @ViewOnly is set to 1 for viewing all indexes that is fragmentet more than what the variable @maxfrag is set to. In this case the viables are set to View and 30%.
If you want to defragmentate, set @ViewOnly to 0 (null)
USE [databasename] -- enter the name of your database to test
GO
-- Declare variables
SET NOCOUNT ON
DECLARE @tablename VARCHAR(128)
DECLARE @execstr VARCHAR(255)
DECLARE @objectid INT
DECLARE @indexid INT
DECLARE @frag decimal
DECLARE @maxfrag decimal
DECLARE @IdxName varchar(128)
DECLARE @ViewOnly bit
-- Set to 1 to view proposed actions, set to 0 to Execute proposed actions:
SET @ViewOnly=1
-- Decide on the maximum fragmentation to allow for.
SET @maxfrag = 30.0
-- Declare a cursor.
DECLARE tables CURSOR FOR
SELECT CAST(TABLE_SCHEMA AS VARCHAR(100))+'.'+CAST(TABLE_NAME AS VARCHAR(100)) AS Table_Name
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
-- Create the temporary table.
if exists (select name from tempdb.dbo.sysobjects where name like '#fraglist%')
drop table #fraglist
CREATE TABLE #fraglist (
ObjectName CHAR(255),
ObjectId INT,
IndexName CHAR(255),
IndexId INT,
Lvl INT,
CountPages INT,
CountRows INT,
MinRecSize INT,
MaxRecSize INT,
AvgRecSize INT,
ForRecCount INT,
Extents INT,
ExtentSwitches INT,
AvgFreeBytes INT,
AvgPageDensity INT,
ScanDensity decimal,
BestCount INT,
ActualCount INT,
LogicalFrag decimal,
ExtentFrag decimal)
-- Open the cursor.
OPEN tables
-- Loop through all the tables in the database.
FETCH NEXT
FROM tables
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')
FETCH NEXT
FROM tables
INTO @tablename
END
-- Close and deallocate the cursor.
CLOSE tables
DEALLOCATE tables
-- Declare the cursor for the list of indexes to be defragged.
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag, IndexName
FROM #fraglist
WHERE LogicalFrag >= @maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
-- Open the cursor.
OPEN indexes
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @tablename, @objectid, @indexid, @frag, @IdxName
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@ViewOnly=1)
BEGIN
PRINT 'WOULD be executing ALTER INDEX ' + RTRIM(@IdxName) + ' ON ' + RTRIM(@tablename) + ' REORGANIZE WITH ( LOB_COMPACTION = ON ) - Fragmentation currently ' + RTRIM(CONVERT(VARCHAR(15),@frag)) + '%'
END
ELSE
BEGIN
PRINT 'Now executing ALTER INDEX ' + RTRIM(@IdxName) + ' ON ' + RTRIM(@tablename) + ' REORGANIZE WITH ( LOB_COMPACTION = ON ) - Fragmentation currently ' + RTRIM(CONVERT(VARCHAR(15),@frag)) + '%'
SELECT @execstr = 'ALTER INDEX ' + RTRIM(@IdxName) + ' ON ' + RTRIM(@tablename) + ' REORGANIZE WITH ( LOB_COMPACTION = ON )'
EXEC (@execstr)
END
FETCH NEXT
FROM indexes
INTO @tablename, @objectid, @indexid, @frag, @IdxName
END
-- Close and deallocate the cursor.
CLOSE indexes
DEALLOCATE indexes
-- Delete the temporary table.
DROP TABLE #fraglist
GO
Code 3. Index defragmentation code for one database.
To view details about all indexes in one database you can use the following code.
USE databasename;
GO
DBCC SHOWCONTIG WITH TABLERESULTS, ALL_INDEXES
GO
There are many informations to get here, for details see the Books On Line. One of them is the last one, ExtentFragmentation, which shows the fragmentation in percent.
Also, from this article, i have found another way to check all indexes in one database.
SELECT *
FROM sys.dm_db_index_physical_stats(DB_ID('databasename'), NULL, NULL, NULL , NULL);
Look under avg_fragmentation_in_percent.