Skip to content

Instantly share code, notes, and snippets.

@bdcravens
Created April 23, 2016 20:04
Show Gist options
  • Save bdcravens/06df30db6debf9b54b99ee49f330dcbf to your computer and use it in GitHub Desktop.
Save bdcravens/06df30db6debf9b54b99ee49f330dcbf to your computer and use it in GitHub Desktop.
View table index fragmentation and rebuild index
SELECT a.index_id, name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(N'my_db'), OBJECT_ID(N'dbo.my_table'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id;
ALTER INDEX idx_myIndex ON my_table
REBUILD WITH (FILLFACTOR = 80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment