Tip

Identify redundant indexes

Indexes are created to enhance performance, but often they are created with the same attributes, resulting in redundant indexes. Over a period of time the number of such indexes can grow, increasing the size of the DB2 catalog. The following SQL can help you to indentify all such indexes. It has been tested on DB2 UDB OS/390 version 7.0.

CREATE VIEW <CREATOR>.VDUP_INDEX
(CREATOR,TBNAME,IXNAME,DUP_INDEX)
 AS                                    SELECT
 VW1.CREATOR,VW1.TBNAME,VW1.IXNAME,VW2.IXNAME
 FROM
 (SELECT
 IX.CREATOR
 AS CREATOR                                                 , IX.TBNAME
 AS TBNAME                                                  , IX.NAME
 AS IXNAME                                                  ,SUBSTR(
 MAX(CASE WHEN KY.COLSEQ = 1
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 2
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 3
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 4
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 5
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 6
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 7
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 8
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 9
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 10
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 11
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 12
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 13
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 14
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 15
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 16
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 17
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 18
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 19
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 20
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 21
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 22
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 23
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 24
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 25
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 26
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 27
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 28
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 29
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 30
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 31
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 32
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 33
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 34
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 35
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 36
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 37
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 38
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 39
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 40
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 41
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 42
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 43
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 44
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 45
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 46
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 47
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 48
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 49
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 50
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 51
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 52
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 53
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 54
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 55
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 56
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 57
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 58
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 59
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 60
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 61
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 62
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 63
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 64
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)    ,1,192)
 AS COLS
 FROM SYSIBM.SYSINDEXES  IX
 , SYSIBM.SYSKEYS     KY
 WHERE IX.CREATOR  = KY.IXCREATOR
 AND IX.NAME     = KY.IXNAME
 GROUP BY IX.CREATOR
 , IX.TBNAME
 , IX.NAME  )
 AS VW1,
 (SELECT
 IX.CREATOR
 AS CREATOR                                                 , IX.TBNAME
 AS TBNAME                                                  , IX.NAME
 AS IXNAME                                                  , SUBSTR(
 MAX(CASE WHEN KY.COLSEQ = 1
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 2
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 3
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 4
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 5
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 6
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 7
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 8
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 9
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 10
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 11
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 12
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 13
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 14
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 15
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 16
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 17
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 18
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 19
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 20
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 21
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 22
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 23
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 24
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 25
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 26
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 27
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 28
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 29
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 30
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 31
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 32
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 33
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 34
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 35
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 36
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 37
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 38
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 39
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 40
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 41
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 42
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 43
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 44
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 45
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 46
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 47
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 48
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 49
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 50
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 51
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 52
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 53
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 54
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 55
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 56
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 57
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 58
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 59
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 60
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 61
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 62
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 63
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)  ||MAX(CASE WHEN KY.COLSEQ = 64
 THEN STRIP(CHAR(KY.COLNO))||KY.ORDERING ELSE ' ' END)    ,1,192)
 AS COLS
 FROM SYSIBM.SYSINDEXES  IX
 , SYSIBM.SYSKEYS     KY
 WHERE IX.CREATOR  = KY.IXCREATOR
 AND IX.NAME     = KY.IXNAME
 GROUP BY IX.CREATOR
 , IX.TBNAME
 , IX.NAME      )
 AS VW2
 WHERE
 VW1.CREATOR=VW2.CREATOR                                             AND
 VW1.TBNAME=VW2.TBNAME                                               AND
 VW1.COLS  =VW2.COLS                                                 AND
 VW1.IXNAME<>VW2.IXNAME
Example: To get all redundant indexes on the tables with creator like %ABC%%:
Select * from <CREATOR>.VDUP_INDEX 
      where creator like '%ABC%'

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free DB2 tips and scripts.
  • Tip contest: Have a DB2 tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical DB2 questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: DB2 tips, tutorials, and scripts from around the Web.

This was first published in November 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.