Search This Blog

Thursday, 29 January 2015

determining index fragmentation and business rules.

Index Fragmentation: It plays an important role in improving the performance of the sql server. The indexes get fragments as more and more records are inserted and updated to the respective table. so in order to check the fragmentation, use the following query to get all information: 

PS: Business Rules defined by Microsoft to determine the fragmentation of the indexes. 
1. If the index fragmentation average is greater than and equal to 30 percent, then rebuild/ recreate the index to reduce fragmentation.
2. If the index fragmentation average is between 5 to 30 percent then reorganize a respective index.
3. If the index fragmentation average is below 5 percent, then no action is required.

Also, in general for page_count, we use to practice page_count=>2000.

So, for example, if certain index at avg_Fragmentation_percent>30 but page_count= 100 (assuming), then we can ignore that index.

Query to determine fragmentation is defined below: 

SELECT ps.database_id
 ,ps.OBJECT_ID
 ,ps.index_id
 ,b.NAME
 ,ps.avg_fragmentation_in_percent
 ,ps.avg_fragment_size_in_pages
 ,ps.page_count
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) AS ps
INNER JOIN sys.indexes AS b ON ps.OBJECT_ID = b.OBJECT_ID
 AND ps.index_id = b.index_id
WHERE ps.database_id = DB_ID()
 AND ps.avg_fragmentation_in_percent >= 5
 AND page_count >= 2000
 AND b.NAME IS NOT NULL
ORDER BY ps.avg_fragmentation_in_percent DESC
GO