SQL Server Maintenance Plan Reorganize Index and Update Statistics Tasks

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...

(https://www.mssqltips.com/) MENU

SQL Server Maintenance Plan Reorganize Index and Update Statistics


Tasks

By: Sergey Gigoyan (/sqlserverauthor/175/sergey-gigoyan/) | Updated: 2019-12-20 | Comments | Related: More (/sql-server-dba-resources/) >
Indexing (/sql-server-tip-category/38/indexing/)

Problem
In a previous article, we configured the Rebuild Index task using the Maintenance Plan Wizard (/sqlservertip/6252/sql-server-maintenance-plans-rebuild-
index-task/). As mentioned in that article, rebuilding indexes is a resource-consuming operation as it drops and recreates indexes. Thus, in terms of
performance, sometimes an alternate option for fixing index fragmentation can be more effective. This option is index reorganization, which is a less
resource-consuming process, but has limited options for index defragmentation. Unlike the index rebuild operation, the Index Reorganize task does not
update statistics, for example, and it is impossible to set the fill factor for the index in this task. In this article, we will explore how to configure the
Reorganize Index and Update Statistic tasks.

Solution
Usually, when indexes are not too fragmented (as suggested by Microsoft, when fragmentation level is between 5% and 30%), it is recommended to
reorganize indexes instead of rebuilding them. Additionally, after reorganizing indexes it is reasonable to update the statistics as this operation does not
update the statistics like the index rebuild operation. To learn more about index rebuild and reorganize operations, review this tip (/sqlservertip/3533/sql-
server-query-performance-after-index-maintenance-for-reorganization-vs-rebuild-operations/).

It is also recommended to read the previous article of this series - Rebuild Index Task (/sqlservertip/6252/sql-server-maintenance-plans-rebuild-index-task/)
before reading this one. Let’s move on to creating the Reorganize Index task via the Maintenance Plans and then Update Statistics task.

Designing the SQL Server Reorganize Index Task


The first steps of configuring the Reorganize Index task is quite similar to the Rebuild Index task. First, we will start with the "Maintenance Plan Wizard":

Obviously, we will give the corresponding name to the task and will choose the daily schedule as it is less resource-consuming operation as compared to
rebuilding indexes and we prefer to run it more often:

1 of 9 10/21/2022, 3:15 PM
SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...

Actually, we have chosen a weekly schedule, but have chosen all days despite Sunday as on Sundays we have a Rebuild Index task configured. In the next
step, we choose the "Reorganize Index" task and move forward:

We have chosen "All Databases" to reorganize all indexes in all databases of our instance. In the configuration screen, we can see that like in the case of
rebuilding indexes, we have several options:

2 of 9 10/21/2022, 3:15 PM
SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...

Here we can also choose to reorganize indexes depending on their fragmentation level, used page counts and last used time.

Choosing the scan type is also available. We can see that "Compact large objects" is selected which means that space will be deallocated for tables and
views if possible. According to Microsoft’s best practices, it is recommended to reorganize indexes if their fragmentation level is >15% and <=30% (if >30%,
a rebuild should be done).

We leave default values here as well. The possibility to configure these options are missing in older versions of SQL Server. The picture below illustrates the
differences between the same screen in SQL Server 2017 (on the left) and SQL Server 2014 (on the right):

We will move forward by clicking "Next" and reach the final step where we will click "Finish" to complete:

3 of 9 10/21/2022, 3:15 PM
SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...

Now we can see that the ReorganizeIndexes plan has been added under "Maintenance Plans" and the corresponding job has been added under SQL
Server Agent "Jobs":

Designing the SQL Server Update Statistics Task


In order to execute a query, SQL Server Query Optimizer tries to choose the best execution plan. To do so, it evaluates the relative costs of the potential
execution plans using the column and index statistics. The statistics maintained by the SQL Server Engine, as their names suggest, contains statistical
information about the distribution of values in columns of a table or indexed view. Based on this information, the Query Optimizer chooses which plan to
use. For example, using the statistics, the Query Optimizer can decide whether to use an Index Scan or Index Seek operation in a particular query.

If the database’s AUTO_CREATE_STATISTICS and AUTO_UPDATE_STATISTICS options are turned on, the SQL Server Engine creates and updates
statistics automatically. However, after some database maintenance operations, it is important to update statistics manually. Particularly, after a Reorganize
Index as the update statistics operation will improve the index use by providing the optimizer with better data when creating an execution plan.

Therefore, in our maintenance plan, we will add an Update Statistics task just after the Reorganize Index task. Let’s do it via the Maintenance Plan
Designer. We can do it by right-clicking on the plan and choosing "Modify" or by double-clicking on it:

4 of 9 10/21/2022, 3:15 PM
SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...

Then, drag and drop "Update Statistics Task" from the Toolbox to the design surface area:

To configure this task, we can double-click on it:

5 of 9 10/21/2022, 3:15 PM
SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...

We choose "All databases, leave the default values of two other options and click "OK". Then we join the Reorganize Index Task with the Update
Statistics Task by the precedence constraint (the green arrow). If we double click on that constraint, we can see that the Update Statistics Task will start
after the previous, Reorganize Index Task, is successfully completed:

Then we should click on the "Save" button to save our modifications.

This task can be executed like the previous - Rebuild Index task – by right-clicking on the task and choosing the "Execute" or by right-clicking on the
corresponding job and the on "Start Job at Step…":

6 of 9 10/21/2022, 3:15 PM
SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...

After this runs, all fragmented indexes within the ranges we setup above in all databases have been reorganized and statistics has been updated.

Conclusion
All in all, we have configured both the Rebuild Index and Reorganize Index tasks via the Maintenance Plans Wizard. As we can see, the Maintenance Plans
have become more flexible in the newer versions of SQL Server allowing us to configure more options via the UI. Nevertheless, if it is needed to have more
complicated logic in the maintenance tasks, T-SQL code can be more helpful. This is because the T-SQL code allows not only to configure everything which
can be configured via the UI, but also to develop more complex tasks and logic.

Next Steps
To read more about the discussed topic, please follow the links below:

Getting Started with SQL Server Maintenance Plans - Part 1 (/sqlservertip/6099/getting-started-with-sql-server-maintenance-plans--part-1/)


SQL Server Maintenance Plans Rebuild Index Task (/sqlservertip/6252/sql-server-maintenance-plans-rebuild-index-task/)
https://docs.microsoft.com/en-us/sql/relational-databases/maintenance-plans/maintenance-plans?view=sql-server-2017 (https://docs.microsoft.com/en-
us/sql/relational-databases/maintenance-plans/maintenance-plans?view=sql-server-2017)
https://docs.microsoft.com/en-us/sql/relational-databases/maintenance-plans/rebuild-index-task-maintenance-plan?view=sql-server-ver15
(https://docs.microsoft.com/en-us/sql/relational-databases/maintenance-plans/rebuild-index-task-maintenance-plan?view=sql-server-ver15)
https://docs.microsoft.com/en-us/sql/relational-databases/maintenance-plans/reorganize-index-task-maintenance-plan?view=sql-server-ver15
(https://docs.microsoft.com/en-us/sql/relational-databases/maintenance-plans/reorganize-index-task-maintenance-plan?view=sql-server-ver15)
https://docs.microsoft.com/en-us/sql/relational-databases/indexes/reorganize-and-rebuild-indexes?view=sql-server-ver15 (https://docs.microsoft.com/en-
us/sql/relational-databases/indexes/reorganize-and-rebuild-indexes?view=sql-server-ver15)
https://docs.microsoft.com/en-us/sql/relational-databases/indexes/specify-fill-factor-for-an-index?view=sql-server-ver15 (https://docs.microsoft.com/en-
us/sql/relational-databases/indexes/specify-fill-factor-for-an-index?view=sql-server-ver15)
https://docs.microsoft.com/en-us/sql/relational-databases/statistics/statistics?view=sql-server-ver15 (https://docs.microsoft.com/en-us/sql/relational-
databases/statistics/statistics?view=sql-server-ver15)
https://docs.microsoft.com/en-us/sql/t-sql/statements/update-statistics-transact-sql?view=sql-server-ver15 (https://docs.microsoft.com/en-us/sql/t-
sql/statements/update-statistics-transact-sql?view=sql-server-ver15)

7 of 9 10/21/2022, 3:15 PM
SQL Server Maintenance Plan Reorganize Index and Update Statistic... https://www.mssqltips.com/sqlservertip/6256/sql-server-maintenance-...
SQL Server Row Count for all Tables in a Database (/sqlservertip/2537/sql-server-row-count-for-all-tables-in-a-database/)

How to Get Current Date in SQL Server (/sqlservertip/6817/sql-current-date/)

Display Line Numbers in a SQL Server Management Studio Query Window (/sqlservertip/2542/display-line-numbers-in-a-sql-server-management-studio-query-window/)

Concatenate SQL Server Columns into a String with CONCAT() (/sqlservertip/2985/concatenate-sql-server-columns-into-a-string-with-concat/)

Ways to compare and find differences for SQL Server tables and data (/sqlservertip/2779/ways-to-compare-and-find-differences-for-sql-server-tables-and-data/)

Searching and finding a string value in all columns in a SQL Server table (/sqlservertip/1522/searching-and-finding-a-string-value-in-all-columns-in-a-sql-server-table/)

Format numbers in SQL Server (/sqlservertip/7021/sql-format-number/)

About the author


(/sqlserverauthor/175/sergey-gigoyan/) Sergey Gigoyan is a database professional with more than 10 years of experience, with a focus on database design, development,
performance tuning, optimization, high availability, BI and DW design.

View all my tips (/sqlserverauthor/175/sergey-gigoyan/)

Article Last Updated: 2019-12-20

9 of 9 10/21/2022, 3:15 PM

You might also like