Advertisement
tmmdv

Quick & dirty way to find redundant indexes

Dec 23rd, 2014
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.88 KB | None | 0 0
  1. col table_owner format a15 trunc
  2. col ind1 format a50 word_wrapped
  3. col ind2 format a50 word_wrapped
  4. col MB format 999,999,999
  5.  
  6. break ON report skip 1
  7. compute SUM OF MB ON report
  8.  
  9. WITH t AS
  10. (
  11.   SELECT
  12.     table_owner,
  13.     TABLE_NAME,
  14.     index_name,
  15.     index_owner,
  16.     listagg(column_name, ',') WITHIN GROUP (ORDER BY column_position) || ',' cols
  17.   FROM
  18.     dba_ind_columns
  19.   GROUP BY
  20.     table_owner,
  21.     TABLE_NAME,
  22.     index_name,
  23.     index_owner
  24. )
  25. SELECT
  26.   t2.table_owner,
  27.   t2.TABLE_NAME,
  28.   t2.index_name,
  29.   (SELECT SUM(bytes)/1024/1024 FROM dba_segments s WHERE s.segment_name = t2.index_name AND s.owner = t2.index_owner) MB,
  30.   rtrim(t2.cols, ',') ind1,
  31.   rtrim(t1.cols, ',') ind2
  32. FROM
  33.   t t1,
  34.   t t2
  35. WHERE
  36.   t1.table_owner = t2.table_owner AND
  37.   t1.TABLE_NAME = t2.TABLE_NAME AND
  38.   t1.index_name <> t2.index_name AND
  39.   t1.cols LIKE t2.cols || '%'
  40. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement