Determine Index Fragmentation in a SQL Server Database

Index fragmentation can adversely affect query response time. When accessing data through an index, SQL Server must read each page in the specified range to retrieve the indexed values. If the index is highly fragmented, SQL Server may have to search many more pages, and possibly levels, to get this information. This results in poor performance and causes your application to respond slowly.

We can use system function sys.dm_db_index_physical_stats to detect fragmentation in specific index, all indexes in a table or indexed view, or all indexes in databases, or all indexes in all databases. The column avg_fragmentation_in_percent returns the percentage of fragmented data.

Although there is no hard and fast rule, a common recommendation is to keep the index fragmentation below 10 percent if possible. Following query can be used to identify indexes in the current database that have more than 5% fragmentation:

-- Ensure a USE statement has been executed first.

SELECT dm.[object_id]
	,DB_NAME(DB_ID()) + '.' + s.[name] + '.' + o.[name]
	,dm.[index_id]
	,i.[name]
	,dm.[partition_number]
	,dm.[index_type_desc]
	,[pad_index] = CASE i.[is_padded]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,i.[fill_factor]
	,[statistics_norecompute] = CASE st.[no_recompute]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,[ignore_dup_key] = CASE i.[ignore_dup_key]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,[allow_row_locks] = CASE i.[allow_row_locks]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,[allow_page_locks] = CASE i.[allow_page_locks]
		WHEN 0
			THEN 'OFF'
		WHEN 1
			THEN 'ON'
		END
	,dm.[avg_fragmentation_in_percent]
FROM sys.objects o
INNER JOIN sys.indexes i ON o.[object_id] = i.[object_id]
	AND i.[name] = 'NULL'
INNER JOIN sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') dm ON i.[object_id] = dm.[object_id]
	AND i.[index_id] = dm.[index_id]
	AND dm.[avg_fragmentation_in_percent] >= 5
INNER JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
INNER JOIN sys.stats st ON i.[name] = st.[name]
	AND o.[object_id] = st.[object_id]
	AND o.[type] = 'U'

Microsoft recommends reorganizing index if an index fragmentation is between 5-30% and rebuilding index if the index fragmentation is over 30%. For more details about reorganize and rebuild Indexes refer to Microsoft Books Online at http://msdn.microsoft.com/en-us/library/ms189858.aspx.