SQL exec sp_updatestats 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23440770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:47:53  来源:igfitidea点击:

What does exec sp_updatestats do?

sqlsql-serverdatabase-performance

提问by Vignesh

What is the use of sp_updatestats? Can I run that in the production environment for performance improvement?

有什么用sp_updatestats?我可以在生产环境中运行它以提高性能吗?

回答by dean

sp_updatestatsupdates all statistics for all tables in the database, where even a single row has changed. It does it using the default sample, meaning it doesn't scan all rows in the table so it will likely produce less accurate statistics than the alternatives.

sp_updatestats更新数据库中所有表的所有统计信息,即使其中一行发生了变化。它使用默认样本执行此操作,这意味着它不会扫描表中的所有行,因此它产生的统计数据可能不如替代方案准确。

If you have a maintenance plan with 'rebuild indexes' included, it will also refresh statistics, but more accurate because it scans all rows. No need to rebuild stats after rebuilding indexes.

如果您有一个包含“重建索引”的维护计划,它也会刷新统计信息,但更准确,因为它会扫描所有行。重建索引后无需重建统计信息。

Manually updating particular statistics object or a table with update statisticscommand gives you much better control over the process. For automating it, take a look here.

使用update statistics命令手动更新特定的统计对象或表可以让您更好地控制该过程。要使其自动化,请查看此处

Auto-update fires only when optimizer decides it has to. There was a change in math for 2012: in <2012, auto update was fired for every 500 + 20% change in table rows; in 2012+ it is SQRT(1000 * Table rows). It means it is more frequent on large tables. Temporary tables behave differently, of course.

自动更新仅在优化器决定必须时触发。2012 年的数学发生了变化:在 <2012 年,表格行每变化 500 + 20% 就会触发自动更新;在 2012+ 中,它是 SQRT(1000 * 表行)。这意味着它在大表上更频繁。当然,临时表的行为有所不同。

To conclude, sp_updatestatscould actually do more damage than good, and is the least recommendable option.

总而言之,sp_updatestats实际上可能弊大于利,是最不值得推荐的选择。