Update Statistics for All Tables and Databases in SQL Server

Problem

You are a database administrator and have hundreds of SQL Server instances. One common administration task is to update statistics for your database objects for query optimization. If you have a lot of instances with lots of tables this can be a time-consuming task.  Also, if you are handed new instances, you don’t know the status of the indexes and statistics, so you may want to update all stats for all tables and for all databases.

Solution

Why is it important to update statistics?

Statistics are used by the Query Optimizer to create execution plans based on statistical distribution of values. This distribution of data is used to build a query plan and determine what indexes to use and also whether it will be faster to seek or scan the data based on the query.

The statistical data is stored in BLOBs (binary large objects). Using value cardinality or a histogram is used to determine the number of rows to return, and execution plans are built with this information. The density is the number of duplicated values a column can have. This is used to calculate selectivity and enhance cardinality/histogram estimates. As an example, for unique values, the density is 1. Also, high-density variations on a given column can lead to parameter sniffing issues. Outdated or non-existent statistics can lead to poor performing queries or sub-optimal query plans.

Updating Statistics for a SQL Server Database

The sp_updatestats command can be used to update all statistics for a given database. This runs the UPDATE STATISTICS command against all user defined tables in the current database.

The basic usage is:

USE databaseName
GO
sp_updatestats

Use of sp_MSforeachdb

With the help of the undocumented stored procedure sp_MSforeachdb we can run a command for all databases or a selected set of databases. The sp_MSforeachdb stored procedure uses a global cursor to issue a Transact-SQL statement against each database on an instance.

Below is the basic usage.  This will iterate over each database and display the database name.  The “current” database is indicated by [?] which gets replaced dynamically with each database name including the system databases.

EXEC sp_MSforeachdb 'USE [?] SELECT ''[?]'' as DBname'

If we execute the query, we will have output like this:

Basic usage of sp_MSforeachdb

Executing Update Statistics for all SQL Server Databases

Now if we combine these 2 procedures, we can create a script that will update statistics on all tables for all databases on a SQL Server instance.

The basic command will look like this:

sp_MSforeachdb 'use [?]; exec sp_updatestats'

If we execute it, the output will be something like this in a query window:

Basic sp_updatestats usage

The script was executed against all databases, including the system databases.

Excluding SQL Server System Databases from the Script

If you want to execute the above script and exclude system databases, some additional code must be put in place. We know that system databases are identified by database ID 1 to 4, as you can see with this simple select query:

SELECT database_id, name FROM sys.databases

If we execute it, we can see the database id’s belonging to the system databases in SQL Server Management Studio:

Database list from sys.databases

All we need to do is to modify our script to filter those database IDs from the execution. To have cleaner code, I separated the T-SQL for updating the statistics into a parameter, like this:

DECLARE @TSQL nvarchar(2000)
-- Filtering system databases from execution
SET @TSQL = '
IF DB_ID(''?'') > 4
BEGIN
   USE [?]; exec sp_updatestats
END
'
-- Executing TSQL for each database
EXEC sp_MSforeachdb @TSQL

If you execute the query, this will only update databases that have a DB_ID > 4.

Exclude Specific User Databases from the sp_updatestats T-SQL Script

There can be some cases where you want to exclude specific databases from the update, and you can do it by adding just a few lines of code to the script.

We can filter the databases using an IN operator in the IF statement, as follows:

DECLARE @TSQL nvarchar(2000)
-- Filtering system databases and user databases from execution
SET @TSQL = '
IF (DB_ID(''?'') > 4
   AND ''?'' NOT IN(''distribution'',''SSISDB'',''ReportServer'',''ReportServertempdb'')
   )
BEGIN
   PRINT ''********** Rebuilding statistics on database: [?] ************''
   USE [?]; exec sp_updatestats
END
'
-- Executing TSQL for each database
EXEC sp_MSforeachdb @TSQL

As you can see, we use a NOT IN operator to indicate what databases we want to exclude. For this example, we included some of the other system databases that are used for other SQL Server components. We have also included a print statement to show what database are being updated for each iteration.

This is a sample result of the query execution:

Update statistics script execution

More about SQL Server sp_updatestats

According to Microsoft documentation for sp_updatestats, as we mentioned earlier, this function executes UPDATE STATISTICS against all user-defined and internal tables on a database.

You can also specify the RESAMPLE parameter as well. This is used to update each statistic using its most recent sample rate. Allowed values for this parameter are NO and RESAMPLE, with NO as the default. Using RESAMPLE may cause full table scans for indexes, while using NO will use the most recent sample rate.

Usage of this parameter is:

EXEC sp_updatestats @resample = 'RESAMPLE'

The permissions required to execute sp_updatestats are sysadmin or being a database owner.

The method described above is best suited when you receive the server the first time, running the script frequently can be unnecessary and will only cause high resource consumption.  Although you will notice that statistics will not be updated if not required. If you look at the image above, you can see the where it says “did not require update”.

Also, please keep in mind that updating statistics can cause related queries to recompile.

With AUTO_UPDATE_STATISTICS enabled, statistics are updated automatically by the database engine when row count changes for a column reach a threshold.

Also, Microsoft provides an Adaptive Index and statistics maintenance solution, that can be found here: https://github.com/Microsoft/tigertoolbox/tree/master/AdaptiveIndexDefrag.

Next Steps

One comment

  1. I am not certain but my guess is that the two calls to the batch procedure sp_MSforeachdb are conflicting. One needs to end and close all cursors before the next one starts.

    Maybe put BEGIN END around each one or put a USE ? between each to isolate them?

Leave a Reply

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