查询数据库中所有表的索引密度和碎片信息,以便为索引的重建和整理提供依据,也可以参考DBCC SHOWCONTIG,通常FRAGMENTATIOIN在30%以上建议重建,否则建议整理
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
LIMITED | SAMPLED | DETAILED | NULL | DEFAULT
这些模式影响了如何收集碎片数据。LIMITED模式扫描堆所有的页,但对于索引,则只扫描叶级上面的父级别页。SAMPLED收集在堆或索引中1%采样
率的数据。DETAILED模式扫描所有页(堆或索引)。DETAILED是执行最慢的,但也是最精确的选项。指定NULL或DEFAULT的效果与
LIMITED模式的相同。