sql 重组和重建索引

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 重组和重建索引相关的知识,希望对你有一定的参考价值。

SELECT dbschemas.[name] as 'Schema',
dbtables.[name] as 'Table',
dbindexes.[name] as 'Index',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count, 
case 
	when indexstats.avg_fragmentation_in_percent > 5 and indexstats.avg_fragmentation_in_percent < 30 then 'ALTER INDEX ' + dbindexes.[name] + ' ON ' + dbschemas.[name] + '.' + dbtables.[name] + ' REORGANIZE'
	when indexstats.avg_fragmentation_in_percent > 30 then 'ALTER INDEX ' + dbindexes.[name] + ' ON ' + dbschemas.[name] + '.' + dbtables.[name] + ' REBUILD'
	else 'Não há ação necessária.'
end as 'ActionNeeded'
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]
AND indexstats.index_id = dbindexes.index_id
WHERE indexstats.database_id = DB_ID() and indexstats.avg_fragmentation_in_percent > 0
ORDER BY indexstats.avg_fragmentation_in_percent desc

以上是关于sql 重组和重建索引的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 重建索引与重组索引区别

SQL Server查看索引重建重组索引进度

在 Sql Server 维护计划中重组索引与重建索引

SQL Server获取索引创建时间&重建时间&重组时间

SQL Server重建索引与重组索引会更新统计信息吗?

重组索引、重建索引和重组表