By: Tibor Nagy | Updated: 2011-02-07 | Comments (14) | Related: More > Database Administration
Problem
I have a database which has several tables that have very heavy write operations. These table are very large and some are over a hundred gigabytes. I noticed performance of this database is getting slower and after some investigation we suspect that the Auto Update Statistics function is causing a performance degradation.
Solution
Statistics are objects which contain information about the distribution of values in one or more columns of a table or indexed view. The query optimizer uses this statistical data to estimate the number of rows in the query result. In the case where the statistics are invalid or outdated then the query optimizer is likely to choose a wrong execution plan and query performance will decrease. Therefore keeping statistics up to date is an essential part of SQL Server maintenance.
If statistics updates are such a vital part of SQL Server then how can they cause performance issues? In contrast to the above mentioned benefits, updating the statistics also takes resources from the system. This can lead to performance issues when SQL Server is under heavy load especially when you are doing bulk data inserts or have continuous pressure from insert and update operations. In this case statistics are constantly updated keeping your server busy. You can use SQL Profiler to investigate whether the statistics updates are causing a slowdown or not. You can capture the "Auto Stats" event to see the overhead on your system.
If you experience problems due to statistics automatically being updated then the solution might be to disable the Auto Update Statistics option. However, we cannot live with outdated statistics on large databases, so if you turn this option off you should create a scheduled job which explicitly updates the statistics during off-peak periods. To do this, you can use the sp_updatestats procedure.
Following are the steps to disable the "Auto Update Statistics" and to put in place a scheduled job to maintain statistics.
Disable Auto Update Statistics
Open Microsoft SQL Server Management Studio and navigate to your database using Object Explorer. Right click on the database and choose Properties. Select "Options" in the new window and set "Auto Update Statistics" to False as shown on the following screen.
Alternatively you can use the following script to disable this option:
ALTER DATABASE YourDBName SET AUTO_UPDATE_STATISTICS OFF
Create New SQL Job
Open Microsoft SQL Server Management Studio and navigate to SQL Server Agent -> Jobs using Object Explorer. Right click Jobs and choose New Job.
Enter the job name, owner, category (Database maintenance) and description on the following screen:
Click on Steps in the left side pane. Fill in the job name and database. The command type is T-SQL script. You should enter the following command: EXEC sp_updatestats
On the advanced tab you can choose success/failure actions. I recommend you log the job output to a file. You should enter a run as user, but please note that only the DBO and sysadmins can execute this procedure.
The last task is to create a schedule to run the job in off-peak periods. It depends on your database usage on how often you should update the statistics: daily, twice a day, etc. It really depends on your database size, the number of changing rows, etc... I recommend you experiment with this to find the optimal solution for your database. First schedule the job to run once a day in a convenient time and also measure how long it runs. Check the performance gain and consider whether more frequent updates are required.
Additional Options
There is also an option to update the statistics for only one table or a specified index. For this purpose you can use the UPDATE STATISTICS command. Please note that this command sets the Auto Update Statistics to ON if you do not use the NORECOMPUTE option.
UPDATE STATISTICS YourTableName(YourIndex) WITH NORECOMPUTE
In addition, you can turn off Auto Update Statistics for a specific table using the sp_autostats system stored procedure.
Another feature of SQL Server to investigate is Auto Update Statistics Asynchronously.
Next Steps
- Monitor your server to check for performance issues related to Auto Update Statistics
- Check out these related resources:
- Performing maintenance tasks in SQL Server
- Check the Maintenance Tips category on MSSQLTips.com
- Read more about Maintaining Statistics
- Read more about Using Statistics to Improve Query Performance
- Read more about sp_autostats
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2011-02-07