7
8

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 5 years have passed since last update.

SQLServer: 指定したテーブルのインデックス使用状況を取得する

Posted at

インデックスを張ったのは良いけど、本当にちゃんと意図した使われ方をしているのか知りたいときありますよね。

下記のSQLで、特定のテーブルのインデックス利用状況を取得することができます。
・seekで使われるつもりで作ったインデックスでscanが多用されていないか?
・scanで使われるつもりで作ったインデックスでseekが多用されていないか?(こっちは稀ですね)
など確認できます。

※実行にはVIEW SERVER STATE 権限が必要です。

SELECT
    o.name AS table_name
  , i.name AS index_name
  , s.user_seeks    -- Index Seek 数
  , s.user_scans    -- Index Scan 数
  , s.user_updates  -- UPDATE 数
  , s.user_lookups  -- lookup 数
  , last_user_seek
  , last_user_scan
  , last_user_lookup
  , last_user_update
FROM
sys.objects o
 INNER JOIN sys.indexes i
   ON o.object_id = i.object_id
 INNER JOIN sys.dm_db_index_usage_stats s 
   ON i.index_id = s.index_id
WHERE o.type = 'U'
 AND database_id = DB_ID()
 AND s.object_id = OBJECT_ID('テーブル名')
 AND o.name = 'テーブル名'
ORDER BY
 s.user_seeks+s.user_scans desc --seek+scan回数降順
--s.user_seeks desc -- seek回数降順
--s.user_scans desc -- scan回数降順
7
8
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
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?