0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Db2:インデックスの差異を見つけたい

Last updated at Posted at 2021-07-20

背景と状況

本番用データベースとテスト用データベースとでテーブルのインデックスが異なっていることはありませんか。インデックスが定義されていない、列の並びが違う、列が不足している等です。テーブルが300くらいあると調べるのが大変です。

やりたいこと

異なるデータベースのインデックスを比較して違いを見つけたい。
IBM Db2 V11.5 Windwosで確認しています。

やり方

Db2のシステムテーブルからインデックス情報を取得してテキストファイルに出力、テキスト比較ツールで比較します。

  1. SQLを作るSQL
  2. インデックス情報をファイルに出力
  3. テキスト比較ツールで比較

SQLを作るSQL

まずはテーブルの一覧を取得してテーブルごとにインデックス情報をファイルに出力するSQLを作成します。比較対象から外すテーブルをwhere条件で指定しています。

Index_SYSCAT.sql
SELECT 'EXPORT TO ' 
       || Rtrim(indname) 
       || '.txt of del ' 
       || 
' select INDNAME, COLNAME, COLORDER from  SYSCAT.INDEXCOLUSE where INDNAME = ''' 
|| Rtrim(indname) 
|| ''' order by COLSEQ ;' 
FROM   syscat.indexes 
WHERE  indschema = 'DB2ADMIN' 
       AND tabname not LIKE 'ADVISE_%' 
       AND tabname not LIKE 'EXPLAIN_%' 
       AND tabname not in  ('OBJECT_METRICS')
ORDER  BY tabname, 
          indname ;

以下のコマンドでSQLを作成します。Index_out.sqlというファイルが作成されます。
db2 -txf Index_SYSCAT.sql -z Index_out.sql

Index_out.sql
EXPORT TO MST_KEY.txt of del  select INDNAME, COLNAME, COLORDER from  SYSCAT.INDEXCOLUSE where INDNAME = 'MST_KEY' order by COLSEQ ;                                                                                                                                                                                                                                                  
EXPORT TO SND_KEY.txt of del  select INDNAME, COLNAME, COLORDER from  SYSCAT.INDEXCOLUSE where INDNAME = 'SND_KEY' order by COLSEQ ;

テーブルごとにインデックス情報をファイルにエクスポートするSQLです。

インデックス情報をファイルに出力

以下のコマンドでファイルにインデックス情報をエクスポートします。
db2 -tvf Index_out.sql
image.png

テキスト比較ツールで比較

フォルダーごとテキスト比較ツールで比較します。例では、WinMergeを使って比較しています。
image.png
ファイルをクリックして中身を表示します。
image.png
インデックスごとに列名、昇順降順を比較することになります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?