Show index fragmentation
SQL ServerMaintenanceCommand
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;What it does
Lists the fragmentation percentage and page count of indexes; the basis for the rebuild/reorganize decision.
When to use it
Before maintenance, to decide which indexes to address.
Caution
Fragmentation is irrelevant on small indexes (< 1000 pages); they are filtered out. 'DETAILED' mode is more accurate but very expensive; 'LIMITED' is usually sufficient. Query only; no risk.