UPDATE STATISTICS (Transact-SQL)
Updates query optimization statistics on a table or indexed view. By default, the query optimizer already updates statistics as necessary to improve the query plan; in some cases you can improve query performance by using UPDATE STATISTICS or the stored procedure sp_updatestats to update statistics more frequently than the default updates.
Updating statistics ensures that queries compile with up-to-date statistics. However, updating statistics causes queries to recompile. We recommend not updating statistics too frequently because there is a performance tradeoff between improving query plans and the time it takes to recompile queries. The specific tradeoffs depend on your application. UPDATE STATISTICS can use tempdb to sort the sample of rows for building statistics.
UPDATE STATISTICS table_or_indexed_view_name
[
{
{ index_or_statistics__name }
| ( { index_or_statistics_name } [ ,...n ] )
}
]
[ WITH
[
FULLSCAN
| SAMPLE number { PERCENT | ROWS }
| RESAMPLE
| <update_stats_stream_option> [ ,...n ]
]
[ [ , ] [ ALL | COLUMNS | INDEX ]
[ [ , ] NORECOMPUTE ]
] ;
<update_stats_stream_option> ::=
[ STATS_STREAM = stats_stream ]
[ ROWCOUNT = numeric_constant ]
[ PAGECOUNT = numeric_contant ]
When to Use UPDATE STATISTICS
For more information about when to use UPDATE STATISTICS, see Statistics.
Updating All Statistics with sp_updatestats
For information about how to update statistics for all user-defined and internal tables in the database, see the stored procedure sp_updatestats (Transact-SQL). For example, the following command calls sp_updatestats to update all statistics for the database.
EXEC sp_updatestats;
Determining the Last Statistics Update
To determine when statistics were last updated, use the STATS_DATE function.
A. Update all statistics on a table
The following example updates the statistics for all indexes on the SalesOrderDetail table.
USE AdventureWorks2012; GO UPDATE STATISTICS Sales.SalesOrderDetail; GO
B. Update the statistics for an index
The following example updates the statistics for the AK_SalesOrderDetail_rowguid index of the SalesOrderDetail table.
USE AdventureWorks2012; GO UPDATE STATISTICS Sales.SalesOrderDetail AK_SalesOrderDetail_rowguid; GO
C. Update statistics by using 50 percent sampling
The following example creates and then updates the statistics for the Name and ProductNumber columns in the Product table.
USE AdventureWorks2012;
GO
CREATE STATISTICS Products
ON Production.Product ([Name], ProductNumber)
WITH SAMPLE 50 PERCENT
-- Time passes. The UPDATE STATISTICS statement is then executed.
UPDATE STATISTICS Production.Product(Products)
WITH SAMPLE 50 PERCENT;
D. Update statistics by using FULLSCAN and NORECOMPUTE
The following example updates the Products statistics in the Product table, forces a full scan of all rows in the Product table, and turns off automatic statistics for the Products statistics.
USE AdventureWorks2012;
GO
UPDATE STATISTICS Production.Product(Products)
WITH FULLSCAN, NORECOMPUTE;
GO
Caution