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   中道学友  阅读(6760)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

导航

< 2010年2月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 1 2 3 4 5 6
7 8 9 10 11 12 13

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

点击右上角即可分享
微信分享提示