データ バッファ キャッシュ内の使用量の内訳を取得(dm_os_buffer_descriptors)

TechEd 2009 での私のシアターセッション「SQL Server 動的管理ビュー再入門」で紹介した「データ バッファ キャッシュ内の使用量の内訳」を取得できるスクリプトです。DMV(動的管理ビュー)の dm_os_buffer_descriptors とシステム カタログ ビューの allocation_units、partitions を JOIN しています。

USE データベース名
go
SELECT 
  DB_NAME(b.database_id) ,OBJECT_NAME(p.object_id) ,p.index_id
 ,COUNT(*) As 使用ページ数
FROM
 sys.allocation_units a
  INNER JOIN sys.partitions p ON a.container_id = p.hobt_id
  INNER JOIN sys.dm_os_buffer_descriptors b
      ON a.allocation_unit_id = b.allocation_unit_id
WHERE b.database_id = DB_ID()   --現在接続中のDB のみ
GROUP BY b.database_id, p.object_id, p.index_id
ORDER BY 使用ページ数 DESC