任何人都可以重建,当“avg_fragmentation_in_percent”超过一定的限度(更好,如果不使用光标)重新索引碎片化指数提供脚本?
Answer 1:
要重建使用:
ALTER INDEX __NAME_OF_INDEX__ ON __NAME_OF_TABLE__ REBUILD
或重组用途:
ALTER INDEX __NAME_OF_INDEX__ ON __NAME_OF_TABLE__ REORGANIZE
重组应在较低的(<30%)分片使用,但仅重建(这是较重到数据库)切割碎片降至0%。
欲了解更多信息,请参阅https://msdn.microsoft.com/en-us/library/ms189858.aspx
Answer 2:
有两种解决方案:一种简单的,一个更加先进。
介绍
有可供您根据您的问题的严重性,以两种解决方案
用自己的价值观取代,具体如下:
- 更换
XXXMYINDEXXXX
与索引的名字。 - 更换
XXXMYTABLEXXX
用表的名称。 - 更换
XXXDATABASENAMEXXX
与数据库的名称。
解决方案1.索引
重建所有索引在离线模式下的表
ALTER INDEX ALL ON XXXMYTABLEXXX REBUILD
重建一个指定的指数在离线模式下的表
ALTER INDEX XXXMYINDEXXXX ON XXXMYTABLEXXX REBUILD
解决方案2.碎片
碎片是在经常有同时添加和删除的条目表的问题。
检查碎片百分比
SELECT
ips.[index_id] ,
idx.[name] ,
ips.[avg_fragmentation_in_percent]
FROM
sys.dm_db_index_physical_stats(DB_ID(N'XXXMYDATABASEXXX'), OBJECT_ID(N'XXXMYTABLEXXX'), NULL, NULL, NULL) AS [ips]
INNER JOIN sys.indexes AS [idx] ON [ips].[object_id] = [idx].[object_id] AND [ips].[index_id] = [idx].[index_id]
碎片5..30%
如果碎片值大于5%但低于30%,那么就值得重新组织索引。
重组的所有索引的表
ALTER INDEX ALL ON XXXMYTABLEXXX REORGANIZE
重组一个指定的索引为表
ALTER INDEX XXXMYINDEXXXX ON XXXMYTABLEXXX REORGANIZE
碎片30%+
如果碎片值为30%以上那么就值得在联机模式下,然后重建索引。
重建在联机模式下的所有索引的表
ALTER INDEX ALL ON XXXMYTABLEXXX REBUILD WITH (ONLINE = ON)
重建在联机模式下一个指定索引的表
ALTER INDEX XXXMYINDEXXXX ON XXXMYTABLEXXX REBUILD WITH (ONLINE = ON)
Answer 3:
下面是我从把修改后的脚本http://www.foliotek.com/devblog/sql-server-optimization-with-index-rebuilding我发现,张贴在这里有用的。 虽然它使用游标,我知道什么是使用游标可以很容易地转换成光标少版本的主要问题。
这是证据充分的,你可以很容易地通过它读取和修改您的需求。
IF OBJECT_ID('tempdb..#work_to_do') IS NOT NULL
DROP TABLE tempdb..#work_to_do
BEGIN TRY
--BEGIN TRAN
use yourdbname
-- Ensure a USE statement has been executed first.
SET NOCOUNT ON;
DECLARE @objectid INT;
DECLARE @indexid INT;
DECLARE @partitioncount BIGINT;
DECLARE @schemaname NVARCHAR(130);
DECLARE @objectname NVARCHAR(130);
DECLARE @indexname NVARCHAR(130);
DECLARE @partitionnum BIGINT;
DECLARE @partitions BIGINT;
DECLARE @frag FLOAT;
DECLARE @pagecount INT;
DECLARE @command NVARCHAR(4000);
DECLARE @page_count_minimum SMALLINT
SET @page_count_minimum = 50
DECLARE @fragmentation_minimum FLOAT
SET @fragmentation_minimum = 30.0
-- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
-- and convert 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 ,
page_count AS page_count
INTO #work_to_do
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL,
'LIMITED')
WHERE avg_fragmentation_in_percent > @fragmentation_minimum
AND index_id > 0
AND page_count > @page_count_minimum;
IF CURSOR_STATUS('global', 'partitions') >= -1
BEGIN
PRINT 'partitions CURSOR DELETED' ;
CLOSE partitions
DEALLOCATE partitions
END
-- Declare the cursor for the list of partitions to be processed.
DECLARE partitions CURSOR LOCAL
FOR
SELECT *
FROM #work_to_do;
-- Open the cursor.
OPEN partitions;
-- Loop through the partitions.
WHILE ( 1 = 1 )
BEGIN;
FETCH NEXT
FROM partitions
INTO @objectid, @indexid, @partitionnum, @frag, @pagecount;
IF @@FETCH_STATUS < 0
BREAK;
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;
SET @command = N'ALTER INDEX ' + @indexname + N' ON '
+ @schemaname + N'.' + @objectname + N' REBUILD';
IF @partitioncount > 1
SET @command = @command + N' PARTITION='
+ CAST(@partitionnum AS NVARCHAR(10));
EXEC (@command);
--print (@command); //uncomment for testing
PRINT N'Rebuilding index ' + @indexname + ' on table '
+ @objectname;
PRINT N' Fragmentation: ' + CAST(@frag AS VARCHAR(15));
PRINT N' Page Count: ' + CAST(@pagecount AS VARCHAR(15));
PRINT N' ';
END;
-- Close and deallocate the cursor.
CLOSE partitions;
DEALLOCATE partitions;
-- Drop the temporary table.
DROP TABLE #work_to_do;
--COMMIT TRAN
END TRY
BEGIN CATCH
--ROLLBACK TRAN
PRINT 'ERROR ENCOUNTERED:' + ERROR_MESSAGE()
END CATCH
Answer 4:
我发现下面的脚本是在维护索引非常好,你可以有这样的安排在夜间运行,或者你希望的任何其他时间段。
http://sqlfool.com/2011/06/index-defrag-script-v4-1/
Answer 5:
真正的答案,在2016年和2017年,是:使用奥拉Hallengren的脚本:
https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html
这是所有我们任何人都需要知道或怕麻烦,在我们共同的进化了这一点。
Answer 6:
查询REBUILD / REORGANIZE指数
- 30%<=重建
- 5%<=重新组织
- 5%>无能为力
查询:
SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName,
ind.name AS IndexName, indexstats.index_type_desc AS IndexType,
indexstats.avg_fragmentation_in_percent,
'ALTER INDEX ' + QUOTENAME(ind.name) + ' ON ' +QUOTENAME(object_name(ind.object_id)) +
CASE WHEN indexstats.avg_fragmentation_in_percent>30 THEN ' REBUILD '
WHEN indexstats.avg_fragmentation_in_percent>=5 THEN 'REORGANIZE'
ELSE NULL END as [SQLQuery] -- if <5 not required, so no query needed
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats
INNER JOIN sys.indexes ind ON ind.object_id = indexstats.object_id
AND ind.index_id = indexstats.index_id
WHERE
--indexstats.avg_fragmentation_in_percent , e.g. >10, you can specify any number in percent
ind.Name is not null
ORDER BY indexstats.avg_fragmentation_in_percent DESC
产量
TableName IndexName IndexType avg_fragmentation_in_percent SQLQuery
--------------------------------------------------------------------------------------- ------------------------------------------------------
Table1 PK_Table1 CLUSTERED INDEX 75 ALTER INDEX [PK_Table1] ON [Table1] REBUILD
Table1 IX_Table1_col1_col2 NONCLUSTERED INDEX 66,6666666666667 ALTER INDEX [IX_Table1_col1_col2] ON [Table1] REBUILD
Table2 IX_Table2_ NONCLUSTERED INDEX 10 ALTER INDEX [IX_Table2_] ON [Table2] REORGANIZE
Table2 IX_Table2_ NONCLUSTERED INDEX 3 NULL