Another check index fregmentation.

Last updated:

Warning: Review and test in a non-production environment before running.

Back to results

SELECT
    DB_NAME(idxst.database_id) AS [database_name],
    OBJECT_NAME(idxst.object_id, idxst.database_id) AS [object_name],
    QUOTENAME(idxif.name) [index_name],
    CASE 
        WHEN avg_fragmentation_in_percent < 10 THEN 'LOW'
        WHEN avg_fragmentation_in_percent < 30 THEN 'MEDIUM'
        WHEN avg_fragmentation_in_percent < 50 THEN 'HIGH'
        ELSE 'EXTREME'
    END as fragmentation_indicator,
    idxst.*
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL , 'SAMPLED') AS idxst -- not using DETAILED or SAMPLED
INNER JOIN sys.indexes idxif  ON idxst.object_id = idxif.object_id AND idxst.index_id = idxif.index_id
ORDER BY [object_name], [index_name]
GO