SQL Serverでインデックス定義を見やすく抽出する
オブジェクト カタログ ビュー
http://msdn.microsoft.com/ja-jp/library/ms189783.aspx
から。
縦軸がカラム、横軸がインデックスになります。
ポイントは、"PIVOT"で縦->横変換しクロス集計しているところです。
方法
このSQLを実行します。
WITH CTE1 AS ( SELECT DENSE_RANK() OVER( PARTITION BY T4.NAME ORDER BY T1.NAME ) RN, T1.TYPE_DESC, T4.NAME TABLE_NAME, T1.NAME INDEX_NAME, T2.KEY_ORDINAL, T3.COLUMN_ID, T3.NAME COLUMN_NAME, T1.IS_PRIMARY_KEY FROM SYS.INDEXES T1 INNER JOIN SYS.INDEX_COLUMNS T2 ON T1.OBJECT_ID = T2.OBJECT_ID AND T1.INDEX_ID = T2.INDEX_ID INNER JOIN SYS.COLUMNS T3 ON T2.OBJECT_ID = T3.OBJECT_ID AND T2.COLUMN_ID = T3.COLUMN_ID INNER JOIN SYS.TABLES T4 ON T1.OBJECT_ID = T4.OBJECT_ID WHERE T4.SCHEMA_ID = SCHEMA_ID('dbo') ), CTE2 AS ( SELECT CASE WHEN IS_PRIMARY_KEY = 1 THEN -1 ELSE RN END RN, TABLE_NAME, KEY_ORDINAL, COLUMN_ID, COLUMN_NAME FROM CTE1 ) SELECT TABLE_NAME, COLUMN_ID, COLUMN_NAME, [-1] PK, [1] IX1, [2] IX2, [3] IX3, [4] IX4, [5] IX5, [6] IX6, [7] IX7, [8] IX8, [9] IX9, [10] IX10, [11] IX11, [12] IX12, [13] IX13, [14] IX14, [15] IX15 FROM CTE2 PIVOT ( MIN(KEY_ORDINAL) FOR RN IN ([-1], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15]) ) AS PT ORDER BY TABLE_NAME, COLUMN_ID ;