Home : Recommendations for Index Maintenance in Microsoft SQL Server
Q14367 - INFO: Recommendations for Index Maintenance in Microsoft SQL Server

Indexes can become fragmented over time in Microsoft SQL Server.  In order to maintain best performance in WIMS these indexes should be checked on a regular basis (monthly).  Microsoft recommends the following based on avg_fragmentation_in_percent:

avg_fragmentation_in_percent value Corrective statement
> 5% and < = 30% ALTER INDEX REORGANIZE
> 30% ALTER INDEX REBUILD

To determine avg_fragmentation_in_percent value:

SELECT object_name(IPS.object_id) AS [TableName], SI.name AS [IndexName], IPS.Index_type_desc,
IPS.avg_fragmentation_in_percent,IPS.avg_fragment_size_in_pages,IPS.avg_page_space_used_in_percent,
IPS.record_count,IPS.ghost_record_count,IPS.fragment_count,IPS.avg_fragment_size_in_pages
FROM sys.dm_db_index_physical_stats(db_id(DB_NAME()), NULL, NULL, NULL , 'DETAILED') IPS
JOIN sys.tables ST WITH (nolock) ON IPS.object_id = ST.object_id
JOIN sys.indexes SI WITH (nolock) ON IPS.object_id = SI.object_id AND IPS.index_id = SI.index_id
WHERE ST.is_ms_shipped = 0
order by IPS.avg_fragmentation_in_percent desc

To Rebuild/reorganize a Heap type Index.  (Heap indexes can only be rebuilt)

ALTER TABLE MDEVARS rebuild

To rebuild a non Heap Index (i.e. NONCLUSTERED INDEX):

ALTER INDEX PK_DATADD4_AT ON DATADD4_AT REBUILD

To reorganize a non Heap Index (i.e. NONCLUSTERED INDEX):

ALTER INDEX PK_DATADDH ON DATADDH REORGANIZE

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 3/29/2016 11:50 AM.
Last Modified on 3/29/2016 11:51 AM.
Last Modified by Scott Dorner.
Article has been viewed 3879 times.
Rated 3 out of 10 based on 2 votes.
Print Article
Email Article