码迷,mamicode.com
首页 > 数据库 > 详细

Statistics Sql Server 统计信息

时间:2015-10-19 17:08:50      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

统计信息的作用是:查询优化器使用统计信息来创建可提高查询性能的查询计划。

 

统计信息是数据库的object,提供的统计信息是关于table或indexed view上列的统计信息。

Statistics for query optimization are objects that contain statistical information about the distribution of values in one or more columns of a table or indexed view.

 

在Table 目录下查看Statistics能够看到Statistics object,选中CIX_dt_test_idcode,查看Properties,查看该统计对象的信息

技术分享

 

Statistics columns是索引列,Update Statistics描述了Statistics最后一次更新的时间和statistics的更新方式:在创建或更新statistics时更新,或者被Sql Server自动更新,或者手动更新

对于索引,Sql Server会自动创建statistics,所以当索引创建时或修改时,会更新statistics;

Sql Server会自动更新statistics,必须满足一定的条件,修改的数据量到了一定的threshold value,并且自动更新统计信息选项 AUTO_UPDATE_STATISTICS 设置 ON。

手动更新,只需要勾选update statistics for thees,并确定即可手动更新,也可以使用Update statistics语句更新。

 

技术分享

 Statistics对象的详细信息,请参考Update statistic。

 技术分享

 

自动创建的statistics对象,以“_WA_sys”开头,需要开启数据库选项,将自动创建统计信息选项 AUTO_CREATE_STATISTICS设置为ON。

 

从sys.databasees中查看是否允许自动更新和创建statistics对象。

SELECT  name AS dbName, 
        is_auto_create_stats_on AS Auto Create Stats,
        is_auto_update_stats_on AS Auto Update Stats,
        is_auto_update_stats_async_on as Async Auto Update Stats,
        is_read_only AS Read Only
FROM sys.databases 
WHERE database_ID =db_id();

 

使用 AUTO_CREATE_STATISTICS 选项

在自动创建统计信息选项 AUTO_CREATE_STATISTICS 为 ON 时,查询优化器将根据需要在查询谓词中的单独列上创建统计信息,以便改进查询计划的基数估计。这些单列统计信息在现有统计信息对象中尚未具有直方图的列上创建。

可以使用下面的查询来确定查询优化器是否为查询谓词列创建了统计信息。它将查询目录视图 sys.stats 和 sys.stats_columns,以便为具有单列统计信息的所有列返回数据库对象名、列名和统计信息名称。查询优化器通过使用 AUTO_CREATE_STATISTICS 选项在单列上创建统计信息时,统计信息名称以 _WA 开头。

SELECT OBJECT_NAME(s.object_id) AS object_name,
    COL_NAME(sc.object_id, sc.column_id) AS column_name,
    s.name AS statistics_name
FROM sys.stats AS s 
Inner Join sys.stats_columns AS sc
    ON s.stats_id = sc.stats_id AND s.object_id = sc.object_id
WHERE s.name like _WA%
    and s.object_id=object_id(dbo.dt_test)
ORDER BY s.name;

使用 AUTO_UPDATE_STATISTICS 选项

When the automatic update statistics option, AUTO_UPDATE_STATISTICS, is on, the query optimizer determines when statistics might be out-of-date and then updates them when they are used by a query. Statistics become out-of-date after insert, update, delete, or merge operations change the data distribution in the table or indexed view. The query optimizer determines when statistics might be out-of-date by counting the number of data modifications since the last statistics update and comparing the number of modifications to a threshold. The threshold is based on the number of rows in the table or indexed view.

The query optimizer checks for out-of-date statistics before compiling a query and before executing a cached query plan. Before compiling a query, the query optimizer uses the columns, tables, and indexed views in the query predicate to determine which statistics might be out-of-date. Before executing a cached query plan, the Database Engine verifies that the query plan references up-to-date statistics.

The AUTO_UPDATE_STATISTICS option applies to statistics objects created for indexes, single-columns in query predicates, and statistics created with the CREATE STATISTICS statement. This option also applies to filtered statistics.

 

ALTER DATABASE AdventureWorks
    SET AUTO_CREATE_STATISTICS ON;

ALTER DATABASE AdventureWorks
    SET AUTO_UPDATE_STATISTICS ON;


如果数据自动更新统计信息选项设置为ON,那么可以将单独的表上的统计信息设置为不自动update,这样数据库不会自动更新这些统计信息,但是当数据自动更新统计信息选项设置为OFF时,数据库上的所有统计信息都将不能自动更新。

Disabling and Re-enabling AUTO_UPDATE_STATISTICS for Some Statistics

When AUTO_UPDATE_STATISTICS is on, you can override the database-wide statistics update behavior and set automatic statistics updates off for an individual table, index, or column, as required by your application. When AUTO_UPDATE_STATISTICS is on, you can disable and re-enable automatic statistics updates for a table, index, or column in the following ways:

  • Use the sp_autostats system stored procedure. This can disable or re-enable statistics updates for a table or index.

  • Specify the NORECOMPUTE option with the UPDATE STATISTICS statement. To re-enable statistics updates rerun UPDATE STATISTICS without the NORECOMPUTE option.

  • Specify the NORECOMPUTE option with the CREATE STATISTICS statement. To re-enable statistics updates, remove the statistics with DROP STATISTICS and then run CREATE STATISTICS without the NORECOMPUTE option.

  • Specify the STATISTICS_NORECOMPUTE option with the CREATE INDEX statement. To re-enable statistics updates, you can run ALTER INDEX with STATISTICS_NORECOMPUTE = OFF.

When AUTO_UPDATE_STATISTICS is off, you cannot set automatic updates to on for an individual table, index, or column. Re-enabling automatic statistics updates restores the behavior specified by the AUTO_UPDATE_STATISTICS option. If the AUTO_UPDATE_STATISTICS option is off, statistics updates will not occur.

 

统计信息更新有时是需要耗费很多资源的operation,当query optimizer发现表中的statistics对象过期时,需要更新统计信息,如果必须等待统计信息更新完成才生成查询计划,那么势必增加查询计划的生成时间,可以使用异步更新,查询计划仍然使用旧的统计信息来生成查询计划,同时数据库继续统计信息,即不等统计学信息更新完成,query optimizer使用已有的统计信息来生成查询计划,这样并行进行,提高性能。

When to Use Synchronous or Asynchronous Statistics Updates

Statistics updates can be either synchronous (the default) or asynchronous. With synchronous statistics updates, queries always compile and execute with up-to-date statistics; When statistics are out-of-date, the query optimizer waits for updated statistics before compiling and executing the query. With asynchronous statistics updates, queries compile with existing statistics even if the existing statistics are out-of-date; The query optimizer could choose a suboptimal query plan if statistics are out-of-date when the query compiles. Queries that compile after the asynchronous updates have completed will benefit from using the updated statistics.

The database-wide asynchronous statistics update option, AUTO_UPDATE_STATISTICS_ASYNC, determines whether the query optimizer uses synchronous or asynchronous statistics updates. By default, the asynchronous statistics update option is off, and the query optimizer updates statistics synchronously. The AUTO_UPDATE_STATISTICS_ASYNC option applies to statistics objects created for indexes, single columns in query predicates, and statistics created with the CREATE STATISTICS statement.

 

Consider using synchronous statistics for the following scenario:

  • You perform operations that change the distribution of data, such as truncating a table or performing a bulk update of a large percentage of the rows. If you do not update the statistics after completing the operaton, using synchronous statistics will ensure statistics are up-to-date before executing queries on the changed data.

Consider using asynchronous statistics to achieve more predictable query response times for the following scenarios:

  • Your application frequently executes the same query, similar queries, or similar cached query plans. Your query response times might be more predictable with asynchronous statistics updates than with synchronous statistics updates because the query optimizer can execute incoming queries without waiting for up-to-date statistics. This avoids delaying some queries and not others. For more information about finding similar queries, see Finding and Tuning Similar Queries by Using Query and Query Plan Hashes.

  • Your application has experienced client request time outs caused by one or more queries waiting for updated statistics. In some cases, waiting for synchronous statistics could cause applications with aggressive time outs to fail.

 

参考文档

https://msdn.microsoft.com/zh-cn/library/ms190397(v=sql.100).aspx

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

Statistics Sql Server 统计信息

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4887557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!