查看索引碎片
SQL Server维护命令
SELECT OBJECT_SCHEMA_NAME(ips.object_id) AS schema_name,
OBJECT_NAME(ips.object_id) AS table_name,
i.name AS index_name,
ips.index_type_desc,
ROUND(ips.avg_fragmentation_in_percent,1) AS frag_pct,
ips.page_count
FROM sys.dm_db_index_physical_stats(DB_ID(N'Customer'), NULL, NULL, NULL, 'LIMITED') ips
JOIN sys.indexes i
ON i.object_id = ips.object_id AND i.index_id = ips.index_id
WHERE ips.avg_fragmentation_in_percent > 10
AND ips.page_count > 1000
ORDER BY ips.avg_fragmentation_in_percent DESC;作用
列出索引的碎片百分比和页数;是做出重建/重组决定的依据。
适用场景
维护前,用于决定处理哪些索引。
注意
小索引(< 1000 页)的碎片无关紧要;已被过滤掉。'DETAILED' 模式更准确但开销很大;'LIMITED' 通常就足够。仅查询;无风险。