原文发布时间为:2011-02-23 —— 来源于本人的百度文章 [由搬家工具导入]

reindex是比较好的选择,速度快,但是他不能在线操作
INDEXDEFRAG 比较慢,但是可以在线操作
rebuild建议在碎片较少时采用。

附上微软的重建索引脚本,从里面也可以看出微软根据碎片大小推荐的方式,不过这个要随每个不同的数据库而定。
SQL索引碎片整理脚本-- ensure a USE <databasename> statement has been executed first.
SQL索引碎片整理脚本SET NOCOUNT ON;
SQL索引碎片整理脚本DECLARE @objectid int;
SQL索引碎片整理脚本DECLARE @indexid int;
SQL索引碎片整理脚本DECLARE @partitioncount bigint;
SQL索引碎片整理脚本DECLARE @schemaname sysname;
SQL索引碎片整理脚本DECLARE @objectname sysname;
SQL索引碎片整理脚本DECLARE @indexname sysname;
SQL索引碎片整理脚本DECLARE @partitionnum bigint;
SQL索引碎片整理脚本DECLARE @partitions bigint;
SQL索引碎片整理脚本DECLARE @frag float;
SQL索引碎片整理脚本DECLARE @command varchar(8000);
SQL索引碎片整理脚本-- ensure the temporary table does not exist
SQL索引碎片整理脚本IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
SQL索引碎片整理脚本    DROP TABLE work_to_do;
SQL索引碎片整理脚本-- conditionally select from the function, converting object and index IDs to names.
SQL索引碎片整理脚本SELECT
SQL索引碎片整理脚本    object_id AS objectid,
SQL索引碎片整理脚本    index_id AS indexid,
SQL索引碎片整理脚本    partition_number AS partitionnum,
SQL索引碎片整理脚本    avg_fragmentation_in_percent AS frag
SQL索引碎片整理脚本INTO work_to_do
SQL索引碎片整理脚本FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED')
SQL索引碎片整理脚本WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
SQL索引碎片整理脚本-- Declare the cursor for the list of partitions to be processed.
SQL索引碎片整理脚本DECLARE partitions CURSOR FOR SELECT * FROM work_to_do;
SQL索引碎片整理脚本
SQL索引碎片整理脚本-- Open the cursor.
SQL索引碎片整理脚本OPEN partitions;
SQL索引碎片整理脚本
SQL索引碎片整理脚本-- Loop through the partitions.
SQL索引碎片整理脚本FETCH NEXT
SQL索引碎片整理脚本   FROM partitions
SQL索引碎片整理脚本   INTO @objectid, @indexid, @partitionnum, @frag;
SQL索引碎片整理脚本
SQL索引碎片整理脚本WHILE @@FETCH_STATUS = 0
SQL索引碎片整理脚本    BEGIN;
SQL索引碎片整理脚本        SELECT @objectname = o.name, @schemaname = s.name
SQL索引碎片整理脚本        FROM sys.objects AS o
SQL索引碎片整理脚本        JOIN sys.schemas as s ON s.schema_id = o.schema_id
SQL索引碎片整理脚本        WHERE o.object_id = @objectid;
SQL索引碎片整理脚本
SQL索引碎片整理脚本        SELECT @indexname = name 
SQL索引碎片整理脚本        FROM sys.indexes
SQL索引碎片整理脚本        WHERE  object_id = @objectid AND index_id = @indexid;
SQL索引碎片整理脚本
SQL索引碎片整理脚本        SELECT @partitioncount = count (*) 
SQL索引碎片整理脚本        FROM sys.partitions
SQL索引碎片整理脚本        WHERE object_id = @objectid AND index_id = @indexid;
SQL索引碎片整理脚本
SQL索引碎片整理脚本-- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding
SQL索引碎片整理脚本IF @frag>5 AND @frag <= 30.0
SQL索引碎片整理脚本    BEGIN;
SQL索引碎片整理脚本    SELECT @command = 'ALTER INDEX ' + @indexname + ' ON ' + @schemaname + '.' + @objectname + ' REORGANIZE';
SQL索引碎片整理脚本    IF @partitioncount > 1
SQL索引碎片整理脚本        SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);
SQL索引碎片整理脚本    EXEC (@command);
SQL索引碎片整理脚本    END;
SQL索引碎片整理脚本
SQL索引碎片整理脚本IF @frag > 30.0
SQL索引碎片整理脚本    BEGIN;
SQL索引碎片整理脚本    SELECT @command = 'ALTER INDEX ' + @indexname +' ON ' + @schemaname + '.' + @objectname + ' REBUILD';
SQL索引碎片整理脚本    IF @partitioncount > 1
SQL索引碎片整理脚本        SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);
SQL索引碎片整理脚本    EXEC (@command);
SQL索引碎片整理脚本    END;
SQL索引碎片整理脚本PRINT 'Executed ' + @command;
SQL索引碎片整理脚本
SQL索引碎片整理脚本FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag;
SQL索引碎片整理脚本END;
SQL索引碎片整理脚本-- Close and deallocate the cursor.
SQL索引碎片整理脚本CLOSE partitions;
SQL索引碎片整理脚本DEALLOCATE partitions;
SQL索引碎片整理脚本
SQL索引碎片整理脚本-- drop the temporary table
SQL索引碎片整理脚本IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
SQL索引碎片整理脚本    DROP TABLE work_to_do;
SQL索引碎片整理脚本GO


如果提示     '(' 附近有语法错误,则将上面的 DB_ID() 这个改成当前库的ID (当前库的ID,你可以用查询分析器 SELECTDB_ID() 查询出来)


BOL的推荐:



> 5% 且 < = 30%

ALTER INDEX REORGANIZE

> 30%

ALTER INDEX REBUILD WITH (ONLINE = ON)*

相关文章: