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 > Show all indexes on the table as report

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-17-09, 17:27
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Show all indexes on the table as report

select
case
when (mn.minix = si.name and sk.colseq = 1) then si.tbname
else ' '
end table_nme,
case
when (sk.colseq = 1) then si.name
else ' '
end index_nme,
sk.colname,
case
when sc.coltype = 'CHAR'
then strip(sc.coltype) || '(' || varchar(sc.length) || ')'
when sc.coltype = 'FLOAT' and sc.length = 2
then 'DOUBLE'
when sc.coltype = 'VARCHAR'
then strip(sc.coltype) || '(' || varchar(sc.length) || ')'
when sc.coltype = 'DECIMAL'
then strip(sc.coltype) || '(' || varchar(sc.length) || ', ' || varchar(scale) || ')'
else sc.coltype
end as coltype,
sk.colseq,
case
when (sk.colseq = 1) then ' ' || si.UNIQUERULE
else ' '
end UNIQUERULE
from sysibm.sysindexes si
join
sysibm.syskeys sk
on si.name = sk.ixname
join
sysibm.syscolumns sc
on sc.tbname = si.tbname
and
sc.name = sk.colname
and
sc.tbcreator = si.tbcreator
join table
(select min(name) minix
from sysibm.sysindexes mn
where mn.tbcreator = sc.tbcreator
and mn.tbname = si.tbname
) mn
on 1 = 1
where si.tbname in (ucase('???????'))
and
si.tbcreator = '???????'
order by si.tbname, si.name, sk.colseq;
with ur;


You have to change '???????' by existing table name and by table creator, and you'll have the looking good report with all indexes on the table together with columns names and types of columns.

Lenny K. (Citigroup, NY-NJ)
Reply With Quote
  #2 (permalink)  
Old 07-17-09, 17:32
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
This query is working in all system of DB2 and good for all DB2 versions.

Lenny
Reply With Quote
  #3 (permalink)  
Old 07-18-09, 21:02
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
I just had registred on dbforums to say "thanks" to the author of this query.

I used this with small modification:
instead of
"si.tbname in (ucase('???????'))"
I used
"si.tbname like "%part table name%"
and got all my tables together with indexes....

That's a very good job !

Maybe it could not be bad to show the number of column not just in index but in the table, also.

Could author, or somebody join this query to the result of EXPLAIN ?

Thank you.

Kara Sw. NY
Reply With Quote
  #4 (permalink)  
Old 07-20-09, 09:36
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thank you Kara.

The first part is easy:

Quote:
select
case
when (mn.minix = si.name and sk.colseq = 1) then si.tbname
else ' '
end table_nme,
case
when (sk.colseq = 1) then si.name
else ' '
end index_nme,
sk.colname,
case
when sc.coltype = 'CHAR'
then strip(sc.coltype) || '(' || varchar(sc.length) || ')'
when sc.coltype = 'FLOAT' and sc.length = 2
then 'DOUBLE'
when sc.coltype = 'VARCHAR'
then strip(sc.coltype) || '(' || varchar(sc.length) || ')'
when sc.coltype = 'DECIMAL'
then strip(sc.coltype) || '(' || varchar(sc.length) || ', ' || varchar(scale) || ')'
else sc.coltype
end as coltype,
sk.colseq,
sk.colno as tbl_col_no,
case
when (sk.colseq = 1) then ' ' || si.UNIQUERULE
else ' '
end UNIQUERULE
from sysibm.sysindexes si
join
sysibm.syskeys sk
on si.name = sk.ixname
join
sysibm.syscolumns sc
on sc.tbname = si.tbname
and
sc.name = sk.colname
and
sc.tbcreator = si.tbcreator
join table
(select min(name) minix
from sysibm.sysindexes mn
where mn.tbcreator = sc.tbcreator
and mn.tbname = si.tbname
) mn
on 1 = 1
where si.tbname in (ucase('???????'))
and
si.tbcreator = '???????'
order by si.tbname, si.name, sk.colseq;
with ur;
Lenny K. (@Citigroup)
Reply With Quote
  #5 (permalink)  
Old 05-03-11, 17:28
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Talking Refreshing the Memory

I see sometimes some people interesting HOW....

Code:
Select 
       case 
         when (mn.minix = si.name and sk.colseq = 1) then si.tbname
         else ' '
        end  table_nme,
       case 
         when (sk.colseq = 1) then si.name 
         else ' '
       end index_nme,
       sk.colname, 
       case
          when sc.coltype = 'CHAR' 
         then  strip(sc.coltype) || '(' || varchar(sc.length) || ')' 
          when sc.coltype = 'FLOAT' and sc.length = 2
         then  'DOUBLE'
          when sc.coltype = 'VARCHAR' 
         then  strip(sc.coltype) || '(' || varchar(sc.length) || ')'
          when sc.coltype = 'DECIMAL' 
         then  strip(sc.coltype) || '(' || varchar(sc.length) || ', ' 
                || varchar(scale) || ')'
         else  sc.coltype 
       end    as coltype,      
       sk.colseq,
       case 
         when (sk.colseq = 1) 
         then '      ' || si.UNIQUERULE
         else ' '
       end UNIQUERULE
 from sysibm.sysindexes  si
   join
      sysibm.syskeys     sk 
   on si.name       = sk.ixname
   join  
      sysibm.syscolumns  sc 
   on sc.tbname   = si.tbname    
      and
      sc.name       = sk.colname
	and
	sc.tbcreator = si.tbcreator
   join table
      (select min(name) minix, tbname
         from sysibm.sysindexes mn
         where   mn.tbcreator = sc.tbcreator 
         group by tbname) mn
   on
       mn.tbname = si.tbname    
           
where     si.tbname in (ucase('Yours_TBL_NAME'))                         
	    and
            si.tbcreator = 'Yours_TBL_CREATOR'
order by si.tbname, si.name, sk.colseq;
with ur;
Reply With Quote
  #6 (permalink)  
Old 05-03-11, 18:13
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Try it

if you'll use
Code:
si.tbname in 
(ucase('Yours_TBL_1'), ucase('Yours_TBL_2'), ...)
you'll see report on all tables together.

Try it and you'll like it !

Lenny
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