Skip to content

Instantly share code, notes, and snippets.

@kshimi
Created March 14, 2019 09:59
Show Gist options
  • Save kshimi/de54e78cc70114856dad095ded085588 to your computer and use it in GitHub Desktop.
Save kshimi/de54e78cc70114856dad095ded085588 to your computer and use it in GitHub Desktop.
SQLServer check fragmentation
-- Find the average fragmentation percentage of all indexes
-- in the HumanResources.Employee table.
SELECT
 i.index_id, name
 , s.avg_fragmentation_in_percent
 , s.avg_fragment_size_in_pages
 , s.fragment_count, s.page_count
FROM sys.dm_db_index_physical_stats (NULL, NULL, NULL, NULL, NULL) AS s
JOIN sys.indexes AS i ON a.object_id = b.object_id AND a.index_id = b.index_id
where s.avg_fragmentation_in_percent > 30
order by s.avg_fragmentation_in_percent desc
;
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment