Skip to content

Instantly share code, notes, and snippets.

@codingbadger
Created September 4, 2013 10:10
Show Gist options
  • Save codingbadger/6435131 to your computer and use it in GitHub Desktop.
Save codingbadger/6435131 to your computer and use it in GitHub Desktop.
Lists all Indexes and their fragmentation
SELECT ps.database_id, ps.OBJECT_ID,
ps.index_id, b.name,
ps.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS ps
INNER JOIN sys.indexes AS b ON ps.OBJECT_ID = b.OBJECT_ID
AND ps.index_id = b.index_id
WHERE ps.database_id = DB_ID()
ORDER BY ps.avg_fragmentation_in_percent desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment