
Introduction
Hey there! If you manage busy SQL Server databases, you’ve probably wondered at some point – is it worth tinkering with the statistics auto-update settings for those really high activity tables? Will sampling the data more frequently actually lead to better query plans and performance? As someone who’s experimented with this myself, I’m happy to share my experiences and insights. By the end of this article, you’ll have a clearer understanding of when adjusting statistics settings can pay off.
What are SQL Server statistics?
Before we dive in, let’s make sure we’re on the same page about what statistics are in SQL Server. Basically, they’re objects that store information about the distribution and density of values in a table’s columns. The query optimizer uses these stats when estimating the number of rows returned by different operations in a query plan.
SQL Server automatically creates and updates statistics either when an index is created or when the AUTO_CREATE_STATISTICS database option is on (which it is by default). It will update them again when about 20% of the rows have changed.
The case for updating statistics more frequently
So when does it make sense to update statistics more often than SQL Server’s default threshold? In my experience, it’s most helpful for tables that meet a few key criteria:
- Very large (millions/billions of rows)
- Frequent data modifications throughout the day
- Queries performing poorly due to inaccurate stats
I once worked with a table that stored clickstream web analytics data. Records were constantly being inserted round-the-clock as users interacted with the website. Some of our reporting queries started taking forever to run, even though the underlying table data hadn’t grown that much.
When I looked at the statistics, I realized they were way out of date, leading to bad cardinality estimates and inefficient query plans. As an experiment, I updated all statistics on the table with a full scan. The queries started running much faster!
How to update statistics more aggressively
If you want to try updating statistics more frequently than the 20% default, you have a couple options:
- Update statistics manually with
UPDATE STATISTICSand a higher sample rate - Adjust the
AUTO_UPDATE_STATISTICS_ASYNCsetting
For option 1, you could run something like this periodically:
UPDATE STATISTICS [my_table] WITH FULLSCAN, NORECOMPUTE;
A full scan will sample the entire table, giving you the most accurate stats. The NORECOMPUTE clause tells SQL Server to update stats even if less than 20% of rows changed.
Alternatively, if you turn on AUTO_UPDATE_STATISTICS_ASYNC, SQL Server will automatically update stats in the background after the threshold is met, reducing blocking on user queries.
You could also combine both approaches – leave the automatic updates on but supplement with manual updates at whatever frequency seems to help.
Conclusion
So in summary, if you have very large, busy tables that are exhibiting performance issues, it’s definitely worth experimenting with updating statistics more aggressively than SQL Server’s default settings. Monitor your query times and plans to gauge the impact.
There is some CPU overhead to rebuilding stats frequently, so be careful not to overdo it. And make sure you schedule manual updates during off-peak hours if possible.
Hopefully this gives you a framework to start testing on your own databases! Let me know how it goes – I’d love to hear about your experiences.