UPDATE STATISTICS

该命令在一张表或者索引了的视图上更新查询优化统计数字信息. 默认情况下, 查询优化器已经更新了必要的用来提高查询计划的统计信息; 在某些情况下, 你可以通过使用UPDATE STATISTICS 命令或者存储过程sp_updatestats 来比默认更频繁地更新统计信息来提高查询效率.

 

更新统计信息能确保查询能以最新的统计信息来编译. 然而, 更新统计信息会引起查询的重新编译. 我们建议不要过于频繁地更新统计信息, 因为这里有一个在提高查询计划和用来重新编译查询的权衡. 具体的权衡要看你的应用程序而定.

 

关于统计信息的更多信息, 还有何时使用UPDATE STATISTICS, 请参考Using Statistics to Improve Query Performance.

 

注意: 

何时使用UPDATE STATISTICS

参考Using Statistics to Improve Query Performance.

 
使用 sp_updatestats更新所有的统计信息

更多信息关于如何更新所有数据库中的用户定义的表和系统内部表, 请参考存储过程sp_updatestats (Transact-SQL)

举例, 下面的命令调用sp_updatestats 存储过程来更新数据库里所有的统计信息.

EXEC sp_updatestats

 
确定统计信息最后更新的时间
要确定统计信息最后更新的时间, 请使用STATS_DATE 函数.

 

权限

如要运行这个命令, 你需要在表或视图上有ALTER权限.

 

例子

A. Update all statistics on a table

The following example updates the statistics for all indexes on the SalesOrderDetail table.

USE AdventureWorks;
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 AdventureWorks;
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 AdventureWorks;
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 AdventureWorks;
GO
UPDATE STATISTICS Production.Product(Products)
    WITH FULLSCAN, NORECOMPUTE;
GO

 

译自:

UPDATE STATISTICS (Transact-SQL)

http://msdn.microsoft.com/en-us/library/ms187348.aspx

posted on 2010-02-14 01:31  中道学友  阅读(6665)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上