top of page
Learn T-SQL

Implementing ola hallengren indexing

Updated: Mar 23, 2023


It is essential for any DBA to understand and know how to implement the proper indexing and backups for their database. Ola Hallengren has created a comprehensive set of scripts designed to assist DBAs in setting up and managing maintenance plans, ensuring optimal performance of their SQL Server databases. This post will provide detailed instructions on how to utilize ola hallengren's IndexOptimizer and Backup scripts, drawing upon workflow background information, so that you can easily integrate these features into your daily operations.

Finding index fragmentation In SQL Server

To find index fragmentation in SQL Server, you can use the following T-SQL query:

SELECT 
    DB_NAME() AS 'Database Name',
    t.name AS 'Table Name',
    i.name AS 'Index Name',
    ips.index_type_desc AS 'Index Type',
    ips.avg_fragmentation_in_percent AS 'Avg Fragmentation %'
FROM 
    sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) ips
JOIN 
    sys.tables t ON ips.object_id = t.object_id
JOIN 
    sys.indexes i ON ips.object_id = i.object_id AND ips.index_id = i.index_id
WHERE 
    ips.index_type_desc NOT IN ('HEAP','CLUSTERED COLUMNSTORE','NONCLUSTERED COLUMNSTORE')
    AND ips.avg_fragmentation_in_percent > 30 -- or any other fragmentation threshold
ORDER BY 
    ips.avg_fragmentation_in_percent DESC

This query uses the sys.dm_db_index_physical_stats dynamic management function to return information about index fragmentation for the current database. The DB_ID() function returns the ID of the current database, and the NULL parameters are used to indicate that fragmentation information should be returned for all objects in the database.

The JOIN clauses are used to join the sys.dm_db_index_physical_stats results to the sys.tables and sys.indexes system tables, in order to retrieve additional information about the tables and indexes in question.

The WHERE clause is used to filter the results, excluding heaps and columnstore indexes, and only showing indexes with fragmentation greater than 30%. You can modify the fragmentation threshold as needed.

The results are ordered by average fragmentation percentage in descending order. This query can help identify which indexes are in need of maintenance, such as rebuilding or reorganizing.

ALTER INDEX

To re-index in T-SQL, you can use the ALTER INDEX statement with the REBUILD option. Here is an example of how to re-index a specific index in a specific table:

USE [database_name]
GO
ALTER INDEX [index_name] ON [schema_name].[table_name] REBUILD;
GO

To loop through each database and re-index all indexes, you can use a cursor to iterate over the databases and use dynamic SQL to execute the ALTER INDEX statement for each index in each table.


Here is an example of how to do this:

DECLARE @dbname VARCHAR(255)
DECLARE @sql NVARCHAR(MAX)

DECLARE db_cursor CURSOR FOR
SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'USE ' + QUOTENAME(@dbname) + N';
    DECLARE @tablename NVARCHAR(MAX);
    DECLARE @indexname NVARCHAR(MAX);

    DECLARE table_cursor CURSOR FOR
    SELECT QUOTENAME(s.name) + N'.' + QUOTENAME(t.name)
    FROM sys.tables t
    INNER JOIN sys.schemas s ON t.schema_id = s.schema_id

    OPEN table_cursor
    FETCH NEXT FROM table_cursor INTO @tablename

    WHILE @@FETCH_STATUS = 0
    BEGIN
        DECLARE index_cursor CURSOR FOR
        SELECT QUOTENAME(i.name)
        FROM sys.indexes i
        WHERE i.object_id = OBJECT_ID(@tablename)

        OPEN index_cursor
        FETCH NEXT FROM index_cursor INTO @indexname

        WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @sql = @sql + N'
            ALTER INDEX ' + @indexname + ' ON ' + @tablename + ' REBUILD;'
            FETCH NEXT FROM index_cursor INTO @indexname
        END

        CLOSE index_cursor
        DEALLOCATE index_cursor

        FETCH NEXT FROM table_cursor INTO @tablename
    END

    CLOSE table_cursor
    DEALLOCATE table_cursor

    EXEC sp_executesql @sql

    FETCH NEXT FROM db_cursor INTO @dbname
END

CLOSE db_cursor
DEALLOCATE db_cursor

Note that this script will re-index all indexes in all tables in all user databases except for system databases. You can customize it to exclude certain databases or tables if needed.


Rebuild vs re-index in SQL Server

In SQL Server, "rebuild" and "re-index" both refer to maintenance operations that are performed on database indexes. However, there is a subtle difference between the two:

  • Re-indexing involves dropping and recreating the index, whereas rebuilding an index involves defragmenting the existing index.

  • Rebuilding an index is typically faster than re-indexing, but it requires more free space in the database.

  • Rebuilding an index can also cause a significant increase in disk I/O activity, whereas re-indexing may cause less I/O activity.

In general, rebuilding an index is recommended when the index fragmentation level is high (i.e., above 30%), while re-indexing is recommended when the fragmentation level is moderate (i.e., between 10% and 30%). However, the specific choice between rebuilding and re-indexing will depend on various factors, such as the size of the index, the amount of free space in the database, and the availability of resources (e.g., CPU, memory, disk).

It's worth noting that index maintenance should be performed regularly to ensure optimal database performance, but it should be done during off-peak hours to minimize the impact on users.

The script above does ONLY rebuilds SQL indexes. If you want to dynamically determine if an index needs a rebuild or a re index Ola Hallengren's indexing solution.

Ola Hallengren's indexing solution is a popular and widely-used script that provides a comprehensive set of index maintenance operations for SQL Server databases. The solution is available as a free download from Ola Hallengren's website, and it can be customized to fit the specific needs of different database environments.


The indexing solution consists of a set of SQL Server Agent jobs that run on a regular schedule to perform various maintenance tasks, such as rebuilding or reorganizing indexes, updating statistics, and cleaning up old backup files. The solution also includes support for user-defined filegroups and partitions, as well as options for specifying which indexes to include or exclude from maintenance.

One of the key benefits of using Ola Hallengren's indexing solution is its flexibility and ease of use. The solution is highly configurable, with many different parameters that can be adjusted to meet specific database requirements. It also provides detailed logging and error handling, which can help administrators diagnose and troubleshoot issues that may arise during index maintenance.


Overall, Ola Hallengren's indexing solution is a reliable and efficient tool for managing index maintenance in SQL Server databases. It has become a popular choice among database administrators and is often recommended as a best practice for ensuring optimal database performance.


Some of the pros of using Ola Hallengren's indexing solution are:

  • Easy to use: Ola Hallengren's solution is straightforward to implement and use, even for those who are new to database maintenance.

  • Customizable: The solution is highly customizable, allowing you to specify parameters like the databases to be indexed, the types of indexes to be optimized, the schedule, etc.

  • Reliable: Ola Hallengren is a well-known and respected database expert who has been developing and refining his solution for many years. The solution is widely used and has a reputation for being reliable.

  • Comprehensive: The solution includes all the necessary features for managing database indexes, such as rebuilding, reorganizing, updating statistics, and more.

However, there are also some potential cons to consider when using Ola Hallengren's indexing solution:

  • Limited support: While the solution is widely used and has a strong user community, support is primarily provided through forums and user groups, rather than through official channels.

  • Complex configurations: While the solution is customizable, some of the configurations can be complex and require a thorough understanding of database indexing and maintenance.

  • No GUI: The solution is command-line based, which may be less convenient for users who prefer a graphical user interface.

Overall, Ola Hallengren's indexing solution is a popular and effective choice for automating and managing database indexes, but it may not be the best fit for all users, particularly those who require extensive support or a GUI interface.



How to To implement Ola Hallengren's Index solution

  • Download the latest version of the solution from Ola Hallengren's website.

  • Open the downloaded .sql file in SQL Server Management Studio (SSMS).

  • Modify the parameters in the script to suit your environment. Some of the key parameters you may want to customize include:

  • @Databases: A comma-separated list of the databases to be indexed.

  • @Indexes: A comma-separated list of specific indexes to be indexed.

  • @FragmentationLow: The minimum fragmentation level at which indexes will be reorganized.

  • @FragmentationMedium: The minimum fragmentation level at which indexes will be rebuilt.

  • @FragmentationHigh: The minimum fragmentation level at which indexes will be rebuilt with the 'sort in tempdb' option.

  • Execute the modified script in SSMS to create the necessary objects in your database.

  • Schedule a job to run the solution on a regular basis, using SQL Server Agent or another scheduling tool.

  • Monitor the job's progress and adjust the parameters as necessary.

By implementing Ola Hallengren's Index solution, you can automate the process of managing database indexes, which can help improve query performance and reduce maintenance overhead.


Here's an example of how to implement Ola Hallengren's Index solution in T-SQL:

  • Download the latest version of the solution from Ola Hallengren's website.

  • Open the downloaded .sql file in SQL Server Management Studio (SSMS).

  • Modify the parameters in the script to suit your environment. Here's an example of how to modify some of the key parameters:

USE [master]
GO

EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO

EXEC sp_configure 'max degree of parallelism', 1
RECONFIGURE WITH OVERRIDE
GO

USE [YourDatabaseName]
GO

EXECUTE dbo.IndexOptimize 
    @Databases = 'USER_DATABASES',
    @FragmentationLow = NULL,
    @FragmentationMedium = 'INDEX_REORGANIZE,INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
    @FragmentationHigh = 'INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
    @FragmentationLevel1 = 5,
    @FragmentationLevel2 = 30,
    @LogToTable = 'Y'
GO
  • Execute the modified script in SSMS to create the necessary objects in your database.

  • Schedule a job to run the solution on a regular basis, using SQL Server Agent or another scheduling tool.

  • Monitor the job's progress and adjust the parameters as necessary.

This example sets the parameters to optimize all user databases, rebuild indexes with high fragmentation levels online or offline, and log the results to a table. You can modify the parameters to suit your specific needs.


How long will indexing take

The time it takes to perform indexing in SQL Server will depend on several factors, such as the size of the database, the number and size of the indexes being created or rebuilt, the fragmentation level of the existing indexes, the available hardware resources (e.g., CPU, memory, disk I/O), and the workload on the server.


In general, indexing operations can take anywhere from a few minutes to several hours, or even days in some cases, depending on the size and complexity of the database. Rebuilding a large index or a large number of indexes, for example, can take longer than rebuilding smaller indexes. Similarly, the time it takes to create a new index can depend on the size and complexity of the table being indexed.

To get a better estimate of how long indexing will take in a specific database environment, it's a good idea to perform a test run on a non-production system. This can help identify any potential issues or bottlenecks that may affect indexing performance, and it can also provide a baseline for estimating how long indexing will take in a production environment.

It's also important to note that indexing operations can impact database performance, particularly during the actual indexing process. As such, it's often recommended to schedule index maintenance during off-peak hours or times of low database activity to minimize the impact on users.


Can I re-index during production hours?

Re-indexing during production hours can impact database performance and cause increased CPU, memory, and disk I/O activity. Therefore, it's generally recommended to perform index maintenance during off-peak hours or times of low database activity to minimize the impact on users.


However, in some cases, it may be necessary to perform index maintenance during production hours due to business requirements or other constraints. In such cases, it's important to carefully plan and schedule the index maintenance operations to minimize their impact on users.


Some strategies to consider when performing index maintenance during production hours include:

  • Use a phased approach: Rather than rebuilding or re-indexing all indexes at once, consider breaking the process into smaller, more manageable batches. This can help minimize the impact on database performance and user activity.

  • Monitor performance: Keep a close eye on database performance metrics, such as CPU utilization, memory usage, and disk I/O activity, during the index maintenance process. If performance issues arise, consider adjusting the index maintenance settings or throttling the process to reduce its impact.

  • Communicate with users: Make sure to communicate the planned index maintenance to users in advance, and provide updates as the process progresses. This can help manage expectations and minimize the impact of the index maintenance on users.

Overall, while it's generally recommended to perform index maintenance during off-peak hours, it may be possible to perform maintenance during production hours with careful planning and monitoring.

In conclusion, indexing in SQL Server can be an important part of maintaining database performance and reliability. Depending on the size and complexity of the database, indexing operations can take anywhere from a few minutes to several hours or even days. To get a better estimate of how long indexing will take in a specific environment, it’s a good idea to perform a test run on a non-production system. Additionally, it’s important to note that indexing operations can impact database performance, so it’s generally recommended to schedule index maintenance during off-peak hours or times of low database activity. However, in some cases, it may be necessary to perform index maintenance

325 views0 comments
bottom of page