Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Basit's SQL Server Tips

Basit Farooq is a Lead Database Administrator, Trainer and Technical Author. He has more than a decade of IT experience in development, technical training and database administration on Microsoft SQL Server platforms. Basit has authored numerous SQL Server technical articles, and developed and implemented many successful database infrastructure, data warehouse and business intelligence projects. He holds a master's degree in computer science from London Metropolitan University, and industry standard certifications from Microsoft, Sun, Cisco, Brainbench, Prosoft and APM, including MCITP Database Administrator 2008, MCITP Database Administrator 2005, MCDBA SQL Server 2000 and MCTS .NET Framework 2.0 Web Applications.

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:

USE [<Database Name>]
GO
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.


Comments

Leave a comment on the original post [basitaalishan.wordpress.com, opens in a new window]

Loading comments...