Articles SQL

Index Maintenance in SQL Server: Determining REORGANIZE and REBUILD Operations

The concept of using SQL queries to determine and rectify the fragmentation rates of indexes is quite important. This query serves as a useful tool to understand how indexes require maintenance for improving the performance of databases. I will explain the process of using the provided SQL query to check index fragmentation rates and perform necessary corrections when needed.

SQL Query:

SELECT as 'Schema', as 'Table', as 'Index',
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS DDIPS
INNER JOIN sys.tables T on T.object_id = DDIPS.object_id
INNER JOIN sys.schemas S on T.schema_id = S.schema_id
INNER JOIN sys.indexes I ON I.object_id = DDIPS.object_id
AND DDIPS.index_id = I.index_id
WHERE DDIPS.database_id = DB_ID()
and is not null
AND DDIPS.avg_fragmentation_in_percent > 0
ORDER BY DDIPS.avg_fragmentation_in_percent desc

This SQL query will list the fragmentation rates and page counts of indexes in the current database. Understanding the extent of index fragmentation is crucial because it helps us determine how to rectify them.

Subsequently, the results of this query can be copied into an Excel table, and the following Excel formula can be used to automatically generate index correction queries:


This Excel formula automatically generates ALTER INDEX queries based on the index fragmentation percentage. If the index fragmentation percentage is greater than 10 and less than 30, the REORGANIZE operation is applied; otherwise, the REBUILD operation is applied.

This concept in the article will assist database administrators in automating index maintenance in SQL Server or similar database systems, thereby enhancing database performance. Regular index maintenance is a critical step in optimizing database performance.

How REORGANIZE and REBUILD Operations are Determined in SQL:

In SQL Server, the “REORGANIZE” and “REBUILD” operations for index maintenance are determined based on the degree of index fragmentation and the needs of the database. Here are the factors that influence this decision:

1. Index Fragmentation Rate (avg_fragmentation_in_percent): The fragmentation rate of indexes is a critical metric that indicates how fragmented an index is. The fragmentation rate is automatically calculated by SQL Server and can be obtained using the `sys.dm_db_index_physical_stats` function. Typically, it is determined using the following rules:

  • If the fragmentation rate is between 0–10%, the index is considered normal.
  • If the fragmentation rate is between 10–30%, a “REORGANIZE” operation may be applied.
  • If the fragmentation rate is above 30%, a “REBUILD” operation may be required.
  1. These operations are designed to optimize index performance and, ultimately, the performance of the database. The choice between “REORGANIZE” and “REBUILD” depends on the specific fragmentation level and the database’s performance requirements.
  2. “Database Load and Usage Patterns: The load and usage patterns of a database also influence index maintenance. If a database experiences heavy write operations or a high volume of transactions, caution should be exercised during maintenance. In such cases, the ‘REORGANIZE’ operation may be preferred because it is a lighter process. The ‘REBUILD’ operation is more aggressive and resource-intensive, necessitating careful planning.
  3. Database Size and Resources: The size of the database and the available resources impact maintenance decisions. The ‘REBUILD’ operation requires more resources, potentially leading to longer execution times, which can have a greater impact on your resources in the case of large databases. For smaller databases, the ‘REBUILD’ operation may pose fewer concerns.
  4. Database Criticality: The criticality of the database determines the allocation of time and resources for maintenance tasks. Maintenance operations for critical databases should be planned more meticulously.
  5. Operating System and Hardware: The allocation of operating system and hardware resources to the database is also crucial. If you have more powerful hardware, you may opt for the ‘REBUILD’ operation more frequently.

These are fundamental factors to consider when determining the ‘REORGANIZE’ and ‘REBUILD’ operations for index maintenance. Every database is unique and may have specific requirements, so maintenance operations should be tailored to the needs of your database. Ideally, index fragmentation rates should be monitored at regular intervals, and maintenance operations should be planned in alignment with your database management policies.”

Here’s the explanation of the SQL code:

1.SELECT Statement: This statement specifies which columns will be included in the result set. Below are the names of the columns to be used, along with some of them having alias names:

  • as 'Schema': A column representing the schema name, with the alias name ‘Schema’.
  • as 'Table': A column representing the table name, with the alias name ‘Table’.
  • as 'Index': A column representing the index name, with the alias name ‘Index’.
  • DDIPS.avg_fragmentation_in_percent: A column representing the average fragmentation percentage.
  • DDIPS.page_count: A column representing the page count.

2.FROM Clause: This clause specifies the source from which data will be retrieved. In this query, the sys.dm_db_index_physical_stats function is used to obtain the physical statistics of the indexes in the database.

  • sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS DDIPS: This function returns the physical statistics of the indexes in the database. DB_ID() retrieves the identity of the current database. AS DDIPS is an alias given to this result set.

3.INNER JOIN Statements: These statements are used to join the data. In the query, the results of sys.tablessys.schemas, and sys.indexes tables are combined with the results of sys.dm_db_index_physical_stats. This allows the relevant table, schema, and index information to be brought together.

4.WHERE Clause: This clause is used to filter the query results. In this query, only indexes that meet specific conditions are included. For example, it selects indexes where the fragmentation rate is greater than 0 and where the index is not null.-

5.ORDER BY Clause: This clause determines how the results are sorted. The query results are ordered from highest to lowest based on the fragmentation percentage, meaning the most fragmented indexes are listed first.

If you have any questions or details you would like to add, feel free to write me.

Leave a Reply

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