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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律