If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Identify unused indexes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-18-09, 03:39
prashant44 prashant44 is offline
Registered User
 
Join Date: Feb 2008
Posts: 59
Identify unused indexes

Hello,

We are using db2 V8.1 LUW on aix 5.3

After lot of searching i derived a query to find unused indexes in a database. Can you please confirm if this is right query (right way)?

db2 "select NAME, CREATOR from SYSIBM.sysindexes where NAME not in (select distinct sysibm.sysplandep.bname from sysibm.sysplandep, syscat.PACKAGEDEP, syscat.indexes where sysibm.sysplandep.bname=syscat.PACKAGEDEP.bname and syscat.PACKAGEDEP.bname=syscat.indexes.INDNAME)"


subquery in blue will find indexes whose entry is in tables syscat.PACKAGEDEP and sysibm.sysplandep. Finally it will exclude this result set from sysibm.sysindexes to get unused indexces.

regards,
Prashant
Reply With Quote
  #2 (permalink)  
Old 05-18-09, 03:46
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
That query may be correct (I have not looked at it in detail) for indexes used in static SQL (inlcuding SQL stored procedures) but will not include any dynamic SQL that is typically submitted from a java program.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390

Last edited by Marcus_A; 05-19-09 at 10:35.
Reply With Quote
  #3 (permalink)  
Old 05-18-09, 04:33
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Also, be aware that (1) index statistics might give the optimizer lots of information, even if that index won't be used, and (2) cluster indexes play an important performance-related role, even when not used.
(But if a cluster index turns out to be never used in access paths, it's maybe time to choose an other index as cluster index...)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #4 (permalink)  
Old 05-18-09, 07:40
dr_te_z dr_te_z is offline
Registered User
 
Join Date: Jan 2009
Location: Zoetermeer, Holland
Posts: 555
db2pd is the best tool to hunt for unused indexes:
Code:
db2pd -db sample -tcbstats index
Look for the "TCB Index Stats:" header and the "Scans" column.
This will tell you how many times DB2 used that index. If the counter is ZERO, DB2 did not need that index at all so.......
Be ware:
1 - when a database stops and starts again all the counters are reset to ZERO
2 - beware of FK contraints. The index might be required as FK.
Reply With Quote
  #5 (permalink)  
Old 05-18-09, 11:45
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by dr_te_z
beware of FK contraints. The index might be required as FK.
AFAIK, an index is never required to support FKs.
An index on a FK column can of course be helpful for join queries where the join condition happens to be "PK = FK" and where there is either a strong condition on the PK column, or the FK table is the inner table of a nested loop join.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #6 (permalink)  
Old 05-18-09, 12:26
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Peter is right. In an FK-PK relationship, the only requirement is that a unique constraint exists on the PK columns, and in DB2 such a unique constraint is enforced with the help of a (unique) index.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 05-19-09, 01:52
dr_te_z dr_te_z is offline
Registered User
 
Join Date: Jan 2009
Location: Zoetermeer, Holland
Posts: 555
Quote:
Originally Posted by Peter.Vanroose
AFAIK, an index is never required to support FKs.
I know, I know. What I mean is:
Suppose there is a function in the application to DELETE a code from a table. This code is used as a FK in many other tables and because it's an online/realtime function in your application, the developer made sure that all FK's have an index.
Now we, as "know-it-all" DBA run some analysis, identify unsed indexes and throw them away.... We all know that disaster strikes when the code-deletion application is executed.
So I mean that you'll have to check with the developers.
Reply With Quote
  #8 (permalink)  
Old 05-19-09, 03:29
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
... and they probably don't know either. ;-)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 05-19-09, 10:14
prashant44 prashant44 is offline
Registered User
 
Join Date: Feb 2008
Posts: 59
Thank you Markus, stolze & peter for your valuable comments.

And Thanks detez, for your suggestion. I was not aware of such a use of "db2pd".

Thank you ALL , I really got more than what i expected.

That's why this is my favorite forum
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On