Skip to content

Instantly share code, notes, and snippets.

@srkirkland
Created May 20, 2015 22:02
Show Gist options
  • Save srkirkland/0797c0b2ba9d9c53a558 to your computer and use it in GitHub Desktop.
Save srkirkland/0797c0b2ba9d9c53a558 to your computer and use it in GitHub Desktop.
helpful rebuild your indexes script
SELECT name, OBJECT_NAME(a.object_id), avg_fragmentation_in_percent,
'ALTER INDEX ' + b.name + ' ON ' + OBJECT_NAME(a.object_id) +' REBUILD WITH (STATISTICS_NORECOMPUTE = ON)' as command
FROM sys.dm_db_index_physical_stats (
DB_ID(N'PrePurchasing')
, OBJECT_ID('PrePurchasing')
, NULL
, NULL
, NULL) AS a
JOIN sys.indexes AS b
ON a.object_id = b.object_id AND a.index_id = b.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