How to Update Statistics
There are two ways to update statistics. The first way is the easy way. It is one line of code that will update all the statistics in the database using the default sample size of 20,000 rows per table.
The other way, is to use the UPDATE STATISTICS command. This command gives much better granularity of control:
UPDATE STATISTICS Sales.SalesOrderDetail |
UPDATE STATISTICS Sales.SalesOrderDetail IX_SalesOrderDetail |
UPDATE STATISTICS Production.Product(Products) WITH SAMPLE 50 PERCENT |
Using update statistics can give you the granularity of control to only update the out of date statistics, thus having less impact on your production system.
The following script updates all out of date statistics. Set the @MaxDaysOld variable to the number of days you will allow the statistics to be out of date by. Setting the @SamplePercent variable to null will use the SQL Server default value of 20,000 rows. You can also change the sample type to specify rows or percent.
DECLARE @SamplePercent int |
DECLARE @SampleType nvarchar(50) |
SET @SamplePercent = NULL |
SET @SampleType = 'PERCENT' |
BEGIN CATCH SELECT 1 END CATCH |
RowNum = ROW_NUMBER() OVER ( ORDER BY ISNULL (STATS_DATE(object_id, st.stats_id),1)) |
,TableName = OBJECT_SCHEMA_NAME(st.object_id) + '.' + OBJECT_NAME(st.object_id) |
,StatDate = ISNULL (STATS_DATE(object_id, st.stats_id),1) |
FROM sys.stats st WITH (nolock) |
WHERE DATEDIFF( day , ISNULL (STATS_DATE(object_id, st.stats_id),1), GETDATE ()) > @MaxDaysOld |
ORDER BY ROW_NUMBER() OVER ( ORDER BY ISNULL (STATS_DATE(object_id, st.stats_id),1)) |
DECLARE @CurrentRecord int |
DECLARE @TableName nvarchar(255) |
DECLARE @StatName nvarchar(255) |
DECLARE @SQL nvarchar( max ) |
DECLARE @SampleSize nvarchar(100) |
SET @MaxRecord = ( SELECT MAX (RowNum) FROM #OldStats) |
SET @SampleSize = ISNULL ( ' WITH SAMPLE ' + CAST (@SamplePercent AS nvarchar(20)) + ' ' + @SampleType,N '' ) |
WHILE @CurrentRecord <= @MaxRecord |
@TableName = os.TableName |
WHERE RowNum = @CurrentRecord |
SET @SQL = N 'UPDATE STATISTICS ' + @TableName + ' ' + @StatName + @SampleSize |
SET @CurrentRecord = @CurrentRecord + 1 |
After updating the statistics, the execution plans that use these statistics may become invalid. Ideally SQL Server should then create a new execution plan. Personally, I prefer to help SQL Server out by flushing the cache. I would recommend you do the same. Note, this clears the entire procedure cache for the server, not just the database.
You should then also update the usage stats. Usage stats are the row counts stored for each index:
If you are not already doing so, it is highly recommended to leave the default settings of “Auto Update Statistics” and “Auto Create Statistics” ON.
No comments:
Post a Comment