Rebuild and Reorganize Fragmented Indexes

This script will automatically determine whether a rebuild or a reorganize should be used according to the fragmentation of the index. It will then execute the appropriate command. Note that performing index rebuilds online during production hours will cause contention.

[cc lang=”sql”]
SET NOCOUNT ON;
DECLARE @objectid int;
DECLARE @indexid int;
DECLARE @partitioncount bigint;
DECLARE @schemaname nvarchar(258);
DECLARE @objectname nvarchar(258);
DECLARE @indexname nvarchar(258);
DECLARE @partitionnum bigint;
DECLARE @partitions bigint;
DECLARE @frag float;
DECLARE @command varchar(8000);
— ensure the temporary table does not exist
IF EXISTS (SELECT name FROM sys.objects WHERE name = ‘work’)
DROP TABLE work;
— conditionally select from the function, converting object and index IDs
— to names.
SELECT
object_id AS objectid,
index_id AS indexid,
partition_number AS partitionnum,
avg_fragmentation_in_percent AS frag
INTO work
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ‘LIMITED’)
WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
— Declare the cursor for the list of partitions to be processed.
DECLARE partitions CURSOR FOR SELECT * FROM work;

— Open the cursor.
OPEN partitions;

— Loop through the partitions.
FETCH NEXT
FROM partitions
INTO @objectid, @indexid, @partitionnum, @frag;

WHILE @@FETCH_STATUS = 0
BEGIN;
SELECT @objectname = QUOTENAME(o.name),
@schemaname = QUOTENAME(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 = QUOTENAME(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;

— 30 is the decision point at which to switch
— between reorganizing and rebuilding
IF @frag < 30.0 BEGIN; SELECT @command = 'ALTER INDEX ' + @indexname + ' ON ' + @schemaname + '.' + @objectname + ' REORGANIZE'; IF @partitioncount > 1
SELECT @command = @command + ‘ PARTITION=’
+ CONVERT (CHAR, @partitionnum);
EXEC (@command);
END;

IF @frag >= 30.0
BEGIN;
SELECT @command = ‘ALTER INDEX ‘ + @indexname +’ ON ‘ + @schemaname
+ ‘.’ + @objectname + ‘ REBUILD’;
IF @partitioncount > 1
SELECT @command = @command + ‘ PARTITION=’
+ CONVERT (CHAR, @partitionnum);
EXEC (@command);
END;
PRINT ‘Executed ‘ + @command;

FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag;
END;
— Close and deallocate the cursor.
CLOSE partitions;
DEALLOCATE partitions;

— drop the temporary table
IF EXISTS (SELECT name FROM sys.objects WHERE name = ‘work’)
DROP TABLE work;
GO
[/cc]

5 comments
JD 09 Apr 2014 at 8:15 am

Hi folks,

Can you please post the script using TEMP table?

ALso, when I try to run the above script it complaints:

Msg 4145, Level 15, State 1, Line 54
An expression of non-boolean type specified in a context where a condition is expected, near ‘;’.

Any help would be much appreciated!

Faiz 17 Dec 2014 at 9:33 am

Hi,

You need to replace &lt; with

Derek Dieter 03 Mar 2015 at 4:53 am

Thanks, should be fixed now.

Erik 27 Jul 2010 at 2:15 pm

Good script, but why not use Temporary Tables?

Derek Dieter 02 Sep 2010 at 9:00 am

Oh wait..posted that out of context. 🙂 you’re totally right. The script would be better with temp tables. This was part of an old script library I haven’t updated.

Featured Articles

 Site Author

  • Thanks for visiting!
css.php