Explanation of Fragmented Indexes in a Database in SQL Server

Explanation of Fragmented Indexes in a Database in SQL Server

This report offers a SQL script that can be used to discover fragmented indexes in a SQL Server database. Index fragmentation has the potential to strongly affect query performance and this script will aid database administrators in evaluating index health, and deciding what types of maintenance to execute on indexes, namely reorganization or index rebuilding.

Script to Identify Fragmented Indexes


The next script reads details of fragmented indexes of a SQL Server database along with the schema name, table name, index name and fragmentation percentage, and the number of pages.

Example Script:

  1. SELECT   
  2.     OBJECT_SCHEMA_NAME(ips.object_id) AS SchemaName,  
  3.     OBJECT_NAME(ips.object_id) AS TableName,  
  4.     i.name AS IndexName,  
  5.     ips.index_id,  
  6.     ROUND(ips.avg_fragmentation_in_percent, 2) AS FragmentationPercent,  
  7.     ips.page_count  
  8. FROM   
  9.     sys.dm_db_index_physical_stats (DB_ID(), NULLNULLNULL'LIMITED'AS ips  
  10. JOIN   
  11.     sys.indexes AS i ON ips.object_id = i.object_id AND ips.index_id = i.index_id  
  12. WHERE   
  13.     i.index_id > 0 -- exclude heaps  
  14.     AND ips.page_count > 100 -- ignore small indexes  
  15. ORDER BY   
  16.     FragmentationPercent DESC;  



The given script returns the result set as shown in the following columns:



  • SchemaName: Scheme of the table.
  • TableName: Table name.
  • IndexName: Self-explanatory.
  • index_id: an ID of the index.
  • FragmentationPercent: The fragmentation in the index as percentage.
  • Page_Count: The number of pages that are used by the index.

Depending upon the Fragmentation percentage, you will be able to decide what to do:


  • 0-5% Fragmentation: Fair normal level; nothing to be done.
  • 5-30 % Fragmentation: You may want to reorganize the index. Reorganizing is weightless reordering of the leaf nodes of the index that is performed online and with limited disruption.
  • More than 30Percent  Fragmentation: Rebuild the index. Rebuilding develops something new copy of the index which is more effectual on highly fragmented indexes. This will usually consume more resources and most probably move into downtime as compared to operation the version of SQL server and the options employed.

 

Rebuilding Vs. Reorganizing Indexes

Reorganizing:
 
  1. Takes advantage of the available index pages.
  2. Resorts pages at leaf level in the logical order.
  3. More cost effective.
  4. In majority of cases it can be done online.
  5. Better when level of fragmentation is small.
 
Rebuilding:
 
  1. Makes a new copy of index.
  2. Needs additional resources (disk space and CPU).
  3. Online performance is possible in Enterprise Edition.
  4.  more practical with increased degrees of fragmentation.
  5. Keeps statistics afloat as part of the process.

Conclusion:

This script lists all fragmented indexes in your database with their fragmentation level. Use it to monitor index health and decide which indexes to reorganize (if 5–30% fragmented) or rebuild (if over 30%). Regular monitoring improves database performance and keeps queries running efficiently.


0 Comments

Post Comment

Your email address will not be published. Required fields are marked *