3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL Server 統計情報更新の動作とSQLまとめ

Posted at

概要

SQL

統計情報の自動作成

設定状況確認

select
  name 
  ,is_auto_create_stats_on
  ,is_auto_create_stats_incremental_on
  ,is_auto_update_stats_on
  ,is_auto_update_stats_async_on
from sys.databases order by name
列名 データ型 説明
is_auto_create_stats_on bit 1 = AUTO_CREATE_STATISTICS は ON です。
is_auto_create_stats_incremental_on bit 自動統計の増分オプションの既定の設定を示します。
0 = 自動作成の統計は非増分です。
1 = 可能な場合は、自動作成の統計情報は増分されます。
適用対象: SQL Server 2014 (12.x) 以降。
is_auto_update_stats_on bit 1 = AUTO_UPDATE_STATISTICS は ON です。
is_auto_update_stats_async_on bit 1 = AUTO_UPDATE_STATISTICS_ASYNC は ON です。

設定変更

ALTER DATABASE データベース名 SET AUTO_CREATE_STATISTICS ON|OFF
ALTER DATABASE データベース名 SET AUTO_UPDATE_STATISTICS_ASYNC ON|OFF

トレースフラグ2371

  • 確認
    DBCC TRACESTATUS(2371)

  • 有効化
    DBCC TRACEON(2371)

  • 無効化
    DBCC TRACEOFF(2371)

統計情報の操作

更新の実行

EXEC sp_updatestats

オブジェクトの統計情報更新状況確認

SELECT
    so.name
    , ss.name
    , ss.auto_created
    , ss.user_created
    , ss.no_recompute
    , STATS_DATE(ss.object_id, ss.stats_id)
FROM
    sys.objects AS so
    LEFT JOIN
        sys.stats AS ss
    ON
        ss.object_id = so.object_id
WHERE
    type = ‘U’
ORDER BY
    so.name

最終更新日時・更新回数の確認

SELECT
    OBJECT_NAME(dsp.object_id) 
    , dsp.object_id 
    , ss.name
    , dsp.last_updated 
    , dsp.rows
    , dsp.rows_sampled 
    , dsp.steps 
    , dsp.unfiltered_rows 
    , dsp.modification_counter 
FROM
    sys.stats AS ss 
    CROSS APPLY 
    sys.dm_db_stats_properties(ss.object_id, ss.stats_id) AS dsp 
WHERE
    OBJECT_SCHEMA_NAME(dsp.object_id, DB_ID()) <> ‘sys’
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?