Index Fragmentation

 

--
-- look at fragmentation for each indes
--

SELECT a.index_id, name, avg_fragmentation_in_percent  
	FROM sys.dm_db_index_physical_stats (DB_ID(N'ReportBuilderTraining'), NULL, NULL, NULL, NULL) AS a  
	JOIN sys.indexes AS b 
	ON a.object_id = b.object_id AND a.index_id = b.index_id; 

-- rebuild OR regorganize to reduce fragmentation (but don't do both)
ALTER INDEX StudentMajor_PK ON dbo.StudentMajor REORGANIZE;  
ALTER INDEX StudentMajor_PK ON dbo.StudentMajor REBUILD;