SQL Server Update Statistics Before or After Index Rebuild

Stephen West | June 7th, 2017 | SQL |

Nowadays, SQL server is used to maintain the database activities for professional purpose. In general, two operations like rebuilding indexes and updating statistics should be executed within the database. So, in this blog, we are going to discuss about update statistics and rebuild the index and also describing the need to update statistics before and after rebuilding indexes in SQL server.

More About Update Statistics and Rebuilding Index

  • Moreover, UPDATE STATISTICS statement updates two kinds of statistics such as index and column by default. Also, this operation saves the sample of records of the table.
  • On the Other hand, REBUILDING INDEX will update only index statistics rather than column statistics.

In case, if users want to rebuild all the indexes and update all the statistics in SQL server. Then, all the index statistics will be updated through scanning all the rows on the table. After that, users need to update their column statistics by running an UPDATE STATISTICS operation WITH FULLSCAN, COLUMNS statement. First of all, need to update index statistics and secondly, update column statistics even it does not matter which one execute first.

Next, users have to rebuild indexes based on their fragmentation level. Also, REBUILD INDEX allows to update index statistics only instead of column statistics.

Facts of Update Statistics

There are certain facts related to Update statistics discussed below:

  • Update Statistics can update query optimization on a table or indexed view
  • It also ensures the compiling and executing queries with updated statistics
  • Performance trade-off between improving query plans and it takes time to recompile the queries
  • Keep in mind a query cannot recompile even users have updated statistics
  • SQL Server query takes a long time for the execution
  • Insert operation begin to append new values on ascending or descending key columns
  • Update Statistics able to use tempdb to sort the rows of a sample for building statistics
  • After performing the maintenance operations it can change the data distribution like table truncate and insert a large number rows in bulk

Running Update Statistics in SQL Server

  • SQL Server query takes too much time for the execute
  • Insert operation helps to add new values on ascending or descending key columns
  • After using maintenance operation it can modify the data distribution such as truncating a table or insert a lot of data in bulk.

Not Running Update Statistics in SQL Server

In general, using Update statistics to improve the performance of SQL query. Let us discuss below points:

  • Statistics cannot be up-to-date after such operations such as rebuilding or reorganizing an index. Because the distributed data is not changed after these operations.
  • Additionally, when an index on a table is rebuild using ALTER INDEX REBUILD or DBCC DBREINDEX then, the query optimizer updates the statistics.
  • The query optimizer unable to update statistics after the defragmentation operation because such operations cannot drop and recreate the index.

Why Index Fragmentation Occurs in SQL Server?

Whenever there is a modification in the database, a cause i.e. index fragmentation occurs. This problem raised when indexes have multiple pages in which the logical ordering depends on the key value, and physical ordering does not match with the data file. Highly fragmented indexes can easily degrade the SQL query performance and begin to slow down the performance.

Rebuilding Indexes in SQL Server

  • To Rebuild an index statistics and can also re-create the index
  • It can easily remove the fragmentation problem
  • It can compact all the pages and retrieve disk space
  • Specify the Fill factor during the operation performed
  • It can re-order the index rows in contiguous pages
  • Rebuilding or reorganising the small indexes in SQL. Frequently, it cannot reduce the fragmentation as small index pages are stored on mixed extents.

Conclusion

Update Statistics and Rebuild Index are two major SQL Server operations that we have discussed in above section. Moreover, to understand the various aspects of both the statements, we have presented the update statistics before and after rebuilding indexes in SQL server and its specification in detail.

Transaction Log Restore