This article gives a walk-through of SQL Server Statistics and different methods to perform SQL Server Update Statistics.

本文介绍了SQL Server统计信息以及执行SQL Server更新统计信息的不同方法。

介绍 (Introduction)

SQL Server statistics are essential for the query optimizer to prepare an optimized and cost-effective execution plan. These statistics provide distribution of column values to the query optimizer, and it helps SQL Server to estimate the number of rows (also known as cardinality). The query optimizer should be updated regularly. Improper statistics might mislead query optimizer to choose costly operators such as index scan over index seek and it might cause high CPU, memory and IO issues in SQL Server. We might also face blocking, deadlocks that eventually causes trouble to the underlying queries, resources.

SQL Server统计信息对于查询优化器准备优化的,具有成本效益的执行计划至关重要。 这些统计信息将列值分配给查询优化器,并帮助SQL Server估计行数(也称为基数)。 查询优化器应定期更新。 统计信息不正确可能会误导查询优化器选择昂贵的运算符,例如对索引查找进行索引扫描,并且可能导致SQL Server中的CPU,内存和IO问题过多。 我们还可能会遇到阻塞和死锁,这些死锁最终会给基础查询和资源带来麻烦。

查看SQL Server统计信息的选项 (Options to view SQL Server statistics)

We can view SQL Server statistics for an existing object using both SSMS and the T-SQL method.

我们可以使用SSMS和T-SQL方法查看现有对象SQL Server统计信息。

SSMS查看SQL Server统计信息 (SSMS to view SQL Server Statistics)

Connect to a SQL Server instance in SSMS and expand the particular database. Expand the object ( for example, HumanResources.Employee), and we can view all available statistics under the STATISTICS tab.

连接到SSMS中SQL Server实例并扩展特定的数据库。 展开对象(例如,HumanResources.Employee),我们可以在STATISTICS选项卡下查看所有可用的统计信息。

We can get details about any particular statistics as well. Right-click on the statistics and go to properties.

我们还可以获取有关任何特定统计信息的详细信息。 右键单击统计信息,然后转到属性。

It opens the statistics properties and shows the statistics columns and last update date for the particular statistics.

它打开统计信息属性,并显示统计信息列和特定统计信息的最后更新日期。

Click on the Details, and it shows the distribution of values and the frequency of each distinct value occurrence (histogram) for the specified object.

单击详细信息,它将显示指定对象的值分布以及每个不同值出现的频率(直方图)。

T-SQL查看SQL Server统计信息 (T-SQL to view SQL Server Statistics)

We can use DMV sys.dm_db_stats_properties to view the properties of statistics for a specified object in the current database.

我们可以使用DMV sys.dm_db_stats_properties来查看当前数据库中指定对象的统计信息属性。

Execute the following query to check the statistics for HumanResources.Employee table.

执行以下查询以检查HumanResources.Employee表的统计信息。

SELECT sp.stats_id, name, filter_definition, last_updated, rows, rows_sampled, steps, unfiltered_rows, modification_counter
FROM sys.stats AS statCROSS APPLY sys.dm_db_stats_properties(stat.object_id, stat.stats_id) AS sp
WHERE stat.object_id = OBJECT_ID('HumanResources.Employee');
  • Stats_ID: It is the unique ID of the statistics object Stats_ID:这是统计对象的唯一ID
  • Name: It is the statistics name 名称 :这是统计名称
  • Last_updated: It is the date and time of the last statistics update Last_updated:这是上次统计信息更新的日期和时间
  • Rows: It shows the total number of rows at the time of the last statistics update 行数:显示上次统计信息更新时的总行数
  • Rows_sampled: It gives the total number of sample rows for the statistics Rows_sampled:给出统计数据的样本行总数
  • Unfiltered_rows: In the screenshot, you can see both rows_sampled and unfiltered_rows value the same because we did not use any filter in the statistics Unfiltered_rows:在屏幕截图中,您可以看到rows_sampled和unfiltered_rows值相同,因为我们没有在统计信息中使用任何过滤器
  • Modification_counter: It is a vital column to look. We get the total number of modifications since the last statistics update Modification_counter:这是一本至关重要的专栏。 自上次统计信息更新以来,我们获得的修改总数

执行SQL Server更新统计信息的不同方法 (The different methods to perform SQL Server update Statistics)

SQL Server provides different methods at the database level to update SQL Server Statistics.

SQL Server在数据库级别提供了不同的方法来更新SQL Server统计信息。

Right-click on the database and go to properties. In the database properties, we can view statistics options under the Automatic tab.

右键单击数据库,然后转到属性。 在数据库属性中,我们可以在“自动”选项卡下查看统计信息选项。

自动创建统计 (Auto Create Statistics)

SQL Server automatically creates statistics on the individual columns for the query predicate to improve the cardinality estimate and prepare a cost-effective execution plan.

SQL Server在查询谓词的各个列上自动创建统计信息,以改善基数估计并准备经济高效的执行计划。

  • Auto Create Statistics name starts with _WA

    自动创建统计信息名称以_WA开头

    Use the following query to identify statistics auto-created by SQL Server.

    使用以下查询标识由SQL Server自动创建的统计信息。

    SELECT sp.stats_id, name, filter_definition, last_updated, rows, rows_sampled, steps, unfiltered_rows, modification_counter
    FROM sys.stats AS statCROSS APPLY sys.dm_db_stats_properties(stat.object_id, stat.stats_id) AS sp
    WHERE stat.object_id = OBJECT_ID('HumanResources.Employee')
    and name like '_WA%';
    
  • Auto-created SQL Server statistics are single column statistics

    自动创建SQL Server统计信息是单列统计信息

  • SQL Server creates auto statistics for the columns that do not have a histogram for the existing statistics object SQL Server为没有现有统计对象的直方图的列创建自动统计信息

自动创建增量统计 (Auto Create Incremental Statistics)

Starting from SQL Server 2014, we have a new option Auto-Create Incremental Statistics. SQL Server requires scanning the entire table for SQL Server update statistics, and it causes issues for the large tables. It is also valid for a table with partitioning as well. We can use this feature to update only the partition that we require to update. By default, Auto Create Incremental is disabled for individual databases. You can go through Introducing SQL Server Incremental Statistics for Partitioned Tables to get more knowledge on incremental statistics.

从SQL Server 2014开始,我们有一个新选项“ 自动创建增量统计信息”。 SQL Server要求扫描整个表以获取SQL Server更新统计信息,这会导致大型表出现问题。 对于具有分区的表也有效。 我们可以使用此功能仅更新我们需要更新的分区。 默认情况下,单个数据库禁用“自动创建增量”。 您可以阅读《 SQL Server分区表增量统计》,以获取有关增量统计的更多知识。

自动更新统计
(Auto Update Statistics
)

We regularly perform DML operations such as insert, update, delete and such operations change the data distribution or histogram value. Due to these operations, statistics might be out of date, and it might cause issues with the query optimizer efficiency. By default, the SQL Server database has an option Auto Update Statistics true.

我们会定期执行DML操作,例如插入,更新,删除,此类操作会更改数据分布或直方图值。 由于这些操作,统计信息可能已过时,并且可能导致查询优化器效率出现问题。 默认情况下,SQL Server数据库的选项“自动更新统计信息为true”。

With this Auto Update Statistics option, query optimizer updates the SQL Server update statistics when the statistics are out of date. SQL Server uses the following method to update statistics automatically.

使用此“自动更新统计信息”选项,当统计信息过时时,查询优化器将更新SQL Server更新统计信息。 SQL Server使用以下方法自动更新统计信息。

SQL Server 2014 or before

Number of rows at the time of statistics creation

Auto Update Statistics

<=500

Update statistics for every 500 modifications

>500

Update statistics for every 500 + 20 percent modifications

SQL Server 2014或更早版本

创建统计信息时的行数

自动更新统计

<= 500

每500项修改更新统计信息

> 500

每500 + 20%的修改更新统计信息

For the large tables, we require to update 20% of a row to auto-update the statistics. For example, a table with 1 million rows requires 20,000 rows updates. It might not be suitable for the query optimizer to generate an efficient execution plan. SQL Server 2016 onwards, it uses dynamic statistics update threshold, and it adjusts automatically according to the number of rows in the table.

对于大表,我们需要更新一行的20%以自动更新统计信息。 例如,具有100万行的表需要20,000行更新。 查询优化器可能不适合生成有效的执行计划。 从SQL Server 2016起,它使用动态统计信息更新阈值,并根据表中的行数自动进行调整。

Threshold = √((1000)*Current table cardinality)

阈值=√((1000)*当前表基数)

For example, a table with one million rows we can use the formula to calculate the number of updates after which SQL Server will automatically update statistics.

例如,对于具有一百万行的表,我们可以使用公式来计算更新数量,之后SQL Server将自动更新统计信息。

Threshold = √(1000*1000000) = 31622

阈值=√(1000 * 1000000)= 31622

SQL Server updates the statistics after the approx. 31622 modifications in the object.

SQL Server在大约之后更新统计信息。 对象的31622修改。

  • Note: the database compatibility level should be 130 or above to use this dynamic threshold statistics calculations.注意:数据库兼容性级别应为130或更高,才能使用此动态阈值统计信息计算。

异步自动更新统计信息 (Auto Update Statistics Asynchronously)

SQL Server uses synchronous mode to update the statistics. If query optimizer finds out of date statistics, it updates the SQL Server Statistics first and then prepares the execution plan according to the recently updated statistics.

SQL Server使用同步模式来更新统计信息。 如果查询优化器发现过时的统计信息,它将首先更新SQL Server统计信息,然后根据最近更新的统计信息准备执行计划。

If we enable the Auto Update Statistics Asynchronously, SQL Server does not wait to update the statistics. Instead, it executes the query with existing statistics and initiates update statistics requests’ in parallel. The next executed query takes the benefit of the updated statistics. Since SQL Server does not wait for the updated statistics, we also call it Asynchronous mode statistics.

如果我们异步启用自动更新统计信息,则SQL Server不会等待更新统计信息。 而是使用现有统计信息执行查询,并并行发起更新统计信息请求。 下一个执行的查询将利用更新的统计信息。 由于SQL Server不等待更新的统计信息,因此我们也将其称为异步模式统计信息。

手动更新统计信息 (Manually Update Statistics)

In the previous section, we learned that SQL Server automatically updates the out-of-date statistics. We can also manually update the statistics for improving the query execution plan and performance on a requirement basis. We can use the UPDATE STATISTICS or Sp_Update stored procedure to update SQL Server statistics.

在上一节中,我们了解到SQL Server自动更新了过时的统计信息。 我们还可以手动更新统计信息,以根据需要改进查询执行计划和性能。 我们可以使用UPDATE STATISTICS或Sp_Update存储过程来更新SQL Server统计信息。

Let’s use the UPDATE STATISTICS command and its various options to update SQL Server statistics.

让我们使用UPDATE STATISTICS命令及其各种选项来更新SQL Server统计信息。

示例1:对象中所有统计信息SQL Server UPDATE STATISTICS (Example 1: SQL Server UPDATE STATISTICS for all statistics in an object)

Execute the following query to update SQL Server Statistics on HumanResources.Employee table.

执行以下查询以更新HumanResources.Employee表上SQL Server统计信息。

Update STATISTICS HumanResources.Employee

In the following screenshot, we can verify that all the stats get an update at the same time.

在以下屏幕截图中,我们可以验证所有统计信息是否同时获得更新。

示例2:用于特定统计信息SQL Server UPDATE STATISTICS (Example 2: SQL Server UPDATE STATISTICS for specific statistics )

Let’s say we want to update SQL Server statistics for statistics IX_Employee_OrganizationNode. Execute the following code.

假设我们要更新统计信息IX_Employee_OrganizationNodeSQL Server统计信息。 执行以下代码。

Update STATISTICS HumanResources.Employee IX_Employee_OrganizationNode

It updates only specific statistics. In the following screenshot, we can verify this.

它仅更新特定的统计信息。 在以下屏幕截图中,我们可以对此进行验证。

示例3:具有完整扫描SQL Server UPDATE STATISTICS (Example 3: SQL Server UPDATE STATISTICS with FULL Scan)

We use FULL SCAN in the UPDATE STATISTICS to scan all rows of a table. In the previous examples, we did not specify the FULL SCAN parameter. Therefore, SQL Server automatically decides whether it requires FULL SCAN or not.

我们在UPDATE STATISTICS中使用FULL SCAN来扫描表的所有行。 在前面的示例中,我们未指定FULL SCAN参数。 因此,SQL Server会自动决定是否需要全扫描。

The following query does a full scan and updates the statistics for specific statistics in the specified object.

以下查询进行完整扫描,并更新指定对象中特定统计信息的统计信息。

Update STATISTICS HumanResources.Employee IX_Employee_OrganizationNode WITH FULLSCAN

We can also use the WITH SAMPLE 100 PERCENT clause instead of WITH FULLSCAN and both returns the same result.

我们也可以使用WITH SAMPLE 100 PERCENT子句来代替WITH FULLSCAN,两者都返回相同的结果。

Update STATISTICS HumanResources.Employee IX_Employee_OrganizationNode WITH SAMPLE 100 PERCENT

例4:带有样本的UPDATE STATISTICS (Example 4: UPDATE STATISTICS with SAMPLE)

We can use WITH SAMPLE CLAUSE to specify the percentage or number of rows for the query optimizer to update statistics.

我们可以使用WITH SAMPLE CLAUSE为查询优化器指定行的百分比或行数以更新统计信息。

The following query specifies a 10 percent sample to update the statistics.

以下查询指定10%的样本来更新统计信息。

Update STATISTICS HumanResources.Employee IX_Employee_OrganizationNode WITH SAMPLE 10 PERCENT

The following query specifies 1000 rows sample to update the statistics.

以下查询指定1000行样本以更新统计信息。

Update STATISTICS HumanResources.Employee IX_Employee_OrganizationNode WITH SAMPLE 1000 ROWS
  • Note:

    注意:

    • We should not specify 0 PERCENT or 0 Rows to update the statistics because it just updates the statistics object, but it does not contain statistics data 我们不应该指定0 PERCENT或0行来更新统计信息,因为它只是更新统计信息对象,但其中不包含统计信息数据
    • We cannot use FULL SCAN and SAMPLE together 我们不能同时使用FULL SCAN和SAMPLE
    • We should use SAMPLE under specific requirements only. We might take less sample size, and it might not be suitable for the query optimizer to choose the appropriate plan 我们仅应在特定要求下使用SAMPLE。 我们可能需要较少的样本量,并且可能不适合查询优化器选择适当的计划
    • We should not disable the auto-update statistics even we are regularly updating the SQL Server Statistics. Auto Update Statistics allows SQL Server to automatically update stats according to the predefined threshold 即使我们定期更新SQL Server统计信息,也不应禁用自动更新统计信息。 自动更新统计信息允许SQL Server根据预定义的阈值自动更新统计信息
    • Updating statistics with FULL SCAN might take longer for an extensive data set object. We should plan it and do it off business hours 对于完整的数据集对象,使用FULL SCAN更新统计信息可能需要更长的时间。 我们应该计划并在下班时间进行

We usually perform database maintenance such as index rebuild or index reorganize. SQL Server automatically updates the statistics after the index rebuild. It is equivalent to update statistics with FULL SCAN however; it does not update the column statistics. We should update column statistics after index rebuild as well. We can use the following queries to do the task for all statistics on a specified object.

我们通常执行数据库维护,例如索引重建或索引重组。 重建索引后,SQL Server会自动更新统计信息。 但是,它等效于使用FULL SCAN更新统计信息。 它不会更新列统计信息。 重建索引后,我们也应该更新列统计信息。 我们可以使用以下查询为指定对象上的所有统计信息执行任务。

Update STATISTICS HumanResources.Employee  WITH FULLSCAN, COLUMNS

SQL Server does not update statistics with the index reorganize activity. We should manually update the statistics, if required or need to rely on the automatically updated statistics.

SQL Server不会使用索引重组活动来更新统计信息。 如果需要或需要依靠自动更新的统计信息,我们应该手动更新统计信息。

使用sp_updatestats更新所有统计信息 (Updating All Statistics with sp_updatestats)

We can update use sp_updatestats to update all statistics in the database. It does through each object statistics and performs the required update. For the large databases, it might take unnecessary longer time and system resources as well because it performs a check on each statistic on the object.

我们可以更新使用sp_updatestats来更新数据库中的所有统计信息。 它会检查每个对象的统计信息并执行所需的更新。 对于大型数据库,因为它会检查对象的每个统计信息,所以可能会花费不必要的较长时间和系统资源。

If the update is not required, we get the following message.

如果不需要更新,我们将收到以下消息。

0 index(es)/statistic(s) have been updated, 1 did not require update.

0个索引/统计信息已更新,1个不需要更新。

If it updates the statistics, we get the following message.

如果它更新了统计信息,我们将收到以下消息。

1 index(es)/statistic(s) have been updated.

1项索引/统计信息已更新。

exec sp_updatestats

使用SQL Server维护计划更新统计信息 (Update STATISTICS using SQL Server Maintenance Plan)

We can configure a SQL Server maintenance plan to update the statistics regularly. Connect to SQL Server instance in SSMS. Right-click on the Maintenance Plans and go to Maintenance Plan Wizard.

我们可以配置SQL Server维护计划以定期更新统计信息。 连接到SSMS中SQL Server实例。 右键单击维护计划,然后转到维护计划向导。

Select the Update Statistics maintenance task from the list of tasks.

从任务列表中选择“ 更新统计信息”维护任务。

Click Next, and you can define the Update Statistics task.

单击“下一步”,您可以定义“更新统计信息”任务。

In this page, we can select the database (specific database or all databases), objects (specific or all objects). We can also specify to update all, column or index statistics only.

在此页面中,我们可以选择数据库(特定数据库或所有数据库),对象(特定或所有对象)。 我们还可以指定仅更新所有统计信息,列统计信息或索引统计信息。

We can further choose the scan type as a Full Scan or sample by. In the Sample by, we need to specify the sample percentage or sample rows as well.

我们可以进一步选择扫描类型为“完全扫描”或“采样依据”。 在“样本依据”中,我们还需要指定样本百分比或样本行。

结论 (Conclusion)

In this article, we explored the concept of SQL Server Statistics and various options to update these statistics with both automated and manual methods. We should regularly monitor the statistics and update them as per the requirement. You can also go through SQL Server Statistics in Always On Availability Groups to get SQL Server statistics behavior in AG group databases.

在本文中,我们探讨了SQL Server统计信息的概念以及使用自动和手动方法更新这些统计信息的各种选项。 我们应定期监视统计信息,并根据要求进行更新。 您也可以通过“ 始终在线”可用性组中的“ SQL Server统计信息”来获取AG组数据库中SQL Server统计信息行为。

翻译自: https://www.sqlshack.com/sql-server-statistics-and-how-to-perform-update-statistics-in-sql/

SQL Server统计信息以及如何在SQL中执行更新统计信息相关推荐

  1. java gridfs_如何在GridFS中执行更新操作(使用Java)?

    我使用的Mongo-Java-Driver 2.13是在 GridFS中 存储了一个PDF文件( 大小 为 30mb) .我能够轻松地执行插入,删除和查找操作. MongoClient mongo = ...

  2. sql server表分区_介绍分区表SQL Server增量统计信息

    sql server表分区 If you are maintaining a very large database, you might be well aware of the pain to p ...

  3. sql查询禁用缓存_如何在SQL Server 2017中启用和禁用身份缓存

    sql查询禁用缓存 Every data warehouse developer is likely to appreciate the significance of having surrogat ...

  4. SQL Server 2008处理隐式数据类型转换在执行计划中的增强

    关注我们获得更多内容 作者 | 邹建,资深数据库专家,精通各项 SQL Server 技术,具有丰富的管理.维护.优化能力以及业务应用经验.他一直热心于技术知识的分享.传播,持续活跃在 CSDN 和 ...

  5. SQL Server On Linux(3)——SQL Server 2019 For Linux 下载并部署示例数据库

    本人新书上市,请多多关照:<SQL Server On Linux运维实战 2017版从入门到精通>   接上文SQL Server On Linux(2)--SQL Server 201 ...

  6. [Spark周边]--SQL Server 2019预览结合了SQL Server和Apache Spark来创建统一的数据平台

    感谢原文作者:https://cloudblogs.microsoft.com/sqlserver/2018/09/24/sql-server-2019-preview-combines-sql-se ...

  7. SQL Server温故系列(1):SQL 数据操作 CRUD 之增删改合

    1.插入语句 INSERT INTO 1.1.用 INSERT 插入单行数据 1.2.用 INSERT 插入多行数据 1.3.用 INSERT 插入子查询结果行 1.4.INSERT 小结及特殊字段插 ...

  8. SQL Server代理(8/12):使用SQL Server代理外部程序

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在这个系列的上篇文章里,你学习如何使用SQ ...

  9. 使用SQL Server 2017 Docker容器在.NET Core中进行本地Web API开发

    目录 介绍 先决条件 最好事先知道 假设 动机 跨平台 快速安装 经济有效 不同版本/多个实例 速度 持久性 找到SQL Server 2017镜像并在本地下载它 在没有卷挂载的情况下在本地执行SQ​ ...

最新文章

  1. 智能手机收邮件之NOKIA
  2. python中协程实现的本质以及两个封装协程模块greenle、gevent
  3. Microsoft.XMLHTTP 使用方式
  4. 天池实验室|读取数据集的两种方式
  5. 过滤html标签的代码
  6. 《ASP.NET Core 微服务实战》-- 读书笔记(第1章 、第2章)
  7. python判断天数_python判断输入日期是该年的第几天
  8. SQL里变量的声明以及常用函数举例
  9. Embedding技术在商业搜索与推荐场景的实践
  10. 显示器驱动程序已停止响应 并且已成功恢复
  11. java 单元测试 网络请求_Spring Boot 系列(二)单元测试网络请求
  12. CSS:行内/内部样式/外部样式的使用方式示例
  13. VS2015/VS2017C++报错C++LNK1104 无法打开文件“ucrtd.lib”或无法打开包括文件“corecrt.h“
  14. Java工程师是做什么的 岗位职责都有哪些
  15. iOS 编译过程的原理和应用
  16. html页面打印成a4的尺寸,我如何将我的html div调整为A4打印页面中的整页?
  17. 星辰小队针对于软件“星遇”的第二次10天冲刺——第3天
  18. UEFI入门必读的12本书
  19. 成都奔驰加装原厂无钥匙舒适进入 蔚一名车汇
  20. 2018大学计算机答案,2018年大学计算机程序试题及答案

热门文章

  1. mod_fcgid FcgidMaxRequestLen 131072 问题
  2. bzoj 3709: [PA2014]Bohater
  3. myEclipse怎样将程序部署到tomcat(附录MyEclipse调试快捷键)
  4. php安装redis扩展'checking for igbinary includes... configure: error: Cannot find igbinary.h'解决方法...
  5. 不用更改注册表就可以更改桌面所在的位置
  6. android 动态壁纸开发
  7. Java回顾之JDBC
  8. It's my life
  9. Xcode启动RN报错“`fsevents` unavailable“
  10. RIPv1和RIPv2的区别和相同点