查询数据库中所有表的索引密度和碎片信息,以便为索引的重建和整理提供依据,也可以参考DBCC SHOWCONTIG,通常FRAGMENTATIOIN在30%以上建议重建,否则建议整理
declare @db_id int select @db_id=db_id() SELECT i.name AS indexname, o.name AS tablename, s.name AS schemaname, f.index_type_desc AS indextype, f.avg_page_space_used_in_percent AS indexdensity, f.avg_fragmentation_in_percent AS indexfragmentation, f.page_count AS pages FROM sys.dm_db_index_physical_stats(@db_id, NULL, NULL, NULL, 'SAMPLED') f INNER JOIN sys.objects o ON o.object_id = f.object_id INNER JOIN sys.schemas s ON o.schema_id = s.schema_id INNER JOIN sys.indexes i ON f.object_id = i.object_id AND f.index_id = i.index_id WHERE page_count > 50 AND f.index_id > 0 ORDER BY o.name, i.index_id
文章出至: