Basic Index Maintenance in SQL Server

Indexes are crucial in SQL Server for optimizing query performance. They work like a book’s index, enabling the SQL Server to locate data quickly without scanning the entire table. However, indexes can become fragmented or outdated over time, leading to performance degradation. This article outlines basic index maintenance tasks in SQL Server to ensure your indexes remain efficient and effective.

Understanding Index Fragmentation

Before diving into index maintenance, it’s important to understand fragmentation. Fragmentation occurs when the logical order of pages in an index does not match the physical order. This misalignment can slow down query performance because SQL Server needs to read more pages than necessary.

There are two main types of fragmentation:

  1. Internal Fragmentation: This happens when pages contain unused space due to page splits, where SQL Server divides a page into two because of data insertion.
  2. External Fragmentation: Occurs when the logical order of index pages does not match their physical order on disk.

Checking Index Fragmentation

Here’s a query to check the fragmentation:

This query returns the average fragmentation percentage of each index in the database. High fragmentation levels (generally above 30%) indicate that the index may need maintenance.

Reorganizing Indexes

Index Reorganization is a process that defragments an index by reordering the leaf-level pages to match the logical order. It’s a light, online operation that can be performed while the database is online and available to users. For more details, you can refer to Microsoft’s documentation on ALTER INDEX…REORGANIZE.

To reorganize an index, use the following command:

Or to reorganize all indexes on a table:

Reorganizing is recommended when fragmentation is between 5% and 30%.

Rebuilding Indexes

Index Rebuilding is a more intensive operation that drops and recreates the index. It removes fragmentation by rebuilding the entire index structure from scratch. This operation can be performed online or offline, but it requires more resources than reorganization. More information on this can be found in the Microsoft documentation on ALTER INDEX…REBUILD.

To rebuild an index, use the following command:

Or to rebuild all indexes on a table:

Rebuilding indexes is typically recommended when fragmentation exceeds 30%.

Automating Index Maintenance

Regular index maintenance is crucial, and automating this process can save time and ensure consistent performance. You can use SQL Server Agent to schedule regular index maintenance tasks.

Here’s a simple SQL script that can be scheduled:

This script will check the fragmentation level of each index and decide whether to reorganize or rebuild based on the fragmentation percentage.

Conclusion

Proper index maintenance is essential for optimal database performance. Regularly check your indexes for fragmentation and use the appropriate maintenance strategy—reorganize or rebuild—based on the level of fragmentation. Automating these tasks ensures your SQL Server database continues to perform efficiently without manual intervention. For more advanced techniques and options, you can refer to Microsoft’s documentation on index maintenance.

Do you need an external pair of eyes to review your SQL Server architecture?

Maybe your platform development and growth have been organic rather than going through a full enterprise architect design. You have good dev team but NO DBA input. Improvements are built by seeing the real world ‘failure’ as opposed to gaming it out at the DEV/QA stage.

I can offer a full platform evaluation, covering questions such as:

• what is your usage (potential required iops, memory, cpu, space requirements)

• Real world behaviour, usage patterns, etc.

• Routine maintenance review

• Backups / failover review

• UAT environment review

• monitoring / metrics review

Some of the questions I can investigate:

• Are you wrongly sized?

• Are you on the right platform?

• Are you making best use of what you have?

• What is the best model for your business?

• What level of DBA input do you need?

I can provide all of above via my two day “Health Check”. Click here for more info