Run the following script to view the size of database index fragments:
Copy codeThe Code is as follows:
Dbcc showcontig with fast, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS
The following scripts are used to process maintenance tasks:
Copy codeThe Code is as follows:
/* Perform a 'USE <database name>' to select the database in which to run the script .*/
-- Declare variables
Set nocount on;
DECLARE @ tablename varchar (255 );
DECLARE @ execstr varchar (400 );
DECLARE @ objectid int;
Declare @ IndexName varchar (500 );
DECLARE @ indexid int;
DECLARE @ frag decimal;
DECLARE @ maxfrag decimal;
DECLARE @ TmpName varchar (500 );
-- Declare @ TmpName =''
Set @ TmpName =''
-- Decide on the maximum fragmentation to allow.
SELECT @ maxfrag = 30.0;
-- Declare a cursor.
DECLARE tables CURSOR
SELECT TABLE_SCHEMA + '.' + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'base table ';
-- Create the table.
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
SELECT ObjectName, ObjectId, IndexName, IndexId, LogicalFrag
FROM # fraglist
Where indexproperty (ObjectId, IndexName, 'indexdepth ')> 0;
-- Open the cursor.
OPEN indexes;
-- Loop through the indexes.
FETCH NEXT
FROM indexes
INTO @ tablename, @ objectid, @ IndexName, @ indexid, @ frag;
WHILE @ FETCH_STATUS = 0
BEGIN;
If @ frag <@ maxfrag
Begin
SELECT @ execstr = 'alter INDEX ['+ RTRIM (@ IndexName) +'] ON ['+ RTRIM (@ tablename) +'] reorganize with (LOB_COMPACTION = ON )'
Print @ maxfrag + ''+ @ execstr
End
Else
Begin
SELECT @ execstr = 'alter INDEX ['+ RTRIM (@ IndexName) +'] ON ['+ RTRIM (@ tablename) +'] rebuild with (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, SORT_IN_TEMPDB = OFF, ONLINE = OFF )'
Print @ maxfrag + ''+ @ execstr
End
EXEC (@ execstr );
-- Update statistics
IF @ TmpName <> @ tablename
BEGIN
SET @ tmpName = @ tableName
PRINT 'Update STATISTICS '+ @ TableName +' with fullscan'
EXEC ('Update STATISTICS '+ @ TableName +' with fullscan ')
END
FETCH NEXT
FROM indexes
INTO @ tablename, @ objectid, @ IndexName, @ indexid, @ frag;
END;
-- Close and deallocate the cursor.
CLOSE indexes;
DEALLOCATE indexes;
-- Delete the temporary table.
Drop table # fraglist;
GO