Skip to content

Instantly share code, notes, and snippets.

@james-s-tayler
Created October 27, 2019 11:34
Show Gist options
  • Save james-s-tayler/ef4c54b7b8ae12f395624400b5e4eade to your computer and use it in GitHub Desktop.
Save james-s-tayler/ef4c54b7b8ae12f395624400b5e4eade to your computer and use it in GitHub Desktop.
SQL Server Index Fragmentation
select
ps.object_id,
idx.name,
idx.type_desc,
ps.alloc_unit_type_desc,
ps.avg_fragmentation_in_percent,
ps.fragment_count,
ps.avg_fragment_size_in_pages,
ps.page_count
from
sys.dm_db_index_physical_stats(db_id(), NULL, NULL, NULL, NULL) ps
join
sys.indexes idx
on idx.object_id = ps.object_id
and idx.index_id = ps.index_id
order by
avg_fragmentation_in_percent desc,
page_count desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment