Skip to content

Instantly share code, notes, and snippets.

@eduardosilva
Created January 14, 2014 11:10
Show Gist options
  • Save eduardosilva/8416713 to your computer and use it in GitHub Desktop.
Save eduardosilva/8416713 to your computer and use it in GitHub Desktop.
Returns fragmentation in percentage average for the indexes of a SQL Server database.
SELECT
OBJECT_NAME(F.object_id) AS [table],
index_type_desc,
avg_fragmentation_in_percent,
ISNULL(name,'heap') AS [index]
FROM
sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS F
INNER JOIN
sys.indexes AS I ON F.object_id = I.object_id AND F.index_id = I.index_id
ORDER BY
avg_fragmentation_in_percent DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment