Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Ridheen.

Asked: February 16, 2018 - 11:33 am UTC

Last updated: November 04, 2020 - 12:10 am UTC

Version: 12c

Viewed 10K+ times! This question is

You Asked

Hi Tom

I really need to identify all the failed or unusable indexes in all schema in database and also to rebuild it. Kindly provide a query for both. im using SQL DEVELOPER TOOL in ORACLE 12c.

and Connor said...

  procedure check_all_indexes is
  begin
    for i in ( 
        select index_owner, index_name, partition_name, 'partition' ddl_type
        from all_ind_partitions
        where (index_owner,index_name) in 
           ( select owner, index_name
             from   all_indexes
             where table_owner = upper(p_owner)
             and   table_name  = upper(p_table_name)
           )
        and status = 'UNUSABLE'
        union all
        select index_owner, index_name, subpartition_name, 'subpartition' ddl_type
        from all_ind_subpartitions
        where (index_owner,index_name) in 
           ( select owner, index_name
             from   all_indexes
             where table_owner = upper(p_owner)
             and   table_name  = upper(p_table_name)
           )
        and status = 'UNUSABLE'
        union all
        select owner, index_name, null, null
        from all_indexes
        where table_owner = upper(p_owner)
        and   table_name  = upper(p_table_name)
        and status = 'UNUSABLE'
    )
    loop
      if i.ddl_type is null then
        ddl('alter index '||i.index_owner||'.'||i.index_name||' rebuild');
      else
        ddl('alter index '||i.index_owner||'.'||i.index_name||' rebuild '||i.ddl_type||' '||i.partition_name);
      end if;
    end loop;
  end;



Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thank you

Ridheen Sudevan, February 19, 2018 - 9:27 am UTC


can be simplified further

CK, November 03, 2020 - 1:58 pm UTC

if statement not needed, 2nd form of the string will be enough since all trailing bits will be null or blank
procedure needs to accept arguments p_table and p_owner but i'm guessing they are due to brevity like ddl()
Connor McDonald
November 04, 2020 - 12:10 am UTC

nice input

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database