Oracle Performance Analysis 10: Re-indexing-common SQL

Source: Internet
Author: User

Oracle Performance Analysis 10: Re-indexing-common SQL

The previous section describes the causes and methods for re-indexing. This section describes several common SQL statements and stored procedures.

Query all invalid global indexes
select index_name, status  from user_indexes where table_name = upper(table_name)   and status = 'UNUSABLE'
Query all invalid partition Indexes
select index_name, status  from user_ind_partitions where index_name in (select index_name                        from user_indexes                       where table_name = upper(table_name)                         and partitioned = 'YES')   and status = 'UNUSABLE'
Rebuilding all global indexes online

The following stored procedure recreates all global indexes of a specified table online.

declare  v_table_name varchar2(100) := upper(table_name);begin  for i_index_name in (select index_name                         from user_indexes                        where table_name = upper(v_table_name)                          and partitioned = 'NO') loop      dbms_output.put_line(i_index_name.index_name);      execute immediate 'ALTER INDEX ' || i_index_name.index_name ||                        ' REBUILD ONLINE';  end loop;end;
Rebuilding all invalid global indexes online

The following stored procedures view all global indexes of a specified table and recreate all invalid global indexes online.

declare  v_table_name varchar2(100) := upper(table_name);  v_status     varchar2(8);begin  for i_index_name in (select index_name                         from user_indexes                        where table_name = upper(v_table_name)                          and partitioned = 'NO') loop      select status      into v_status      from user_indexes     where index_name = i_index_name.index_name;    if v_status = 'UNUSABLE' then      dbms_output.put_line(i_index_name.index_name);      execute immediate 'ALTER INDEX ' || i_index_name.index_name ||                        ' REBUILD ONLINE';    end if;    end loop;end;
Rebuilding all partition indexes online

The following stored procedure re-creates all the partition indexes online.

declare  v_table_name varchar2(100) := upper(table_name);begin  FOR i_index_name IN (SELECT index_name                         FROM user_indexes                        WHERE table_name = upper(v_table_name)                          and partitioned = 'YES') LOOP    FOR i_partition_name IN (SELECT partition_name                               FROM user_tab_partitions                              WHERE table_name = upper(v_table_name)) LOOP      dbms_output.put_line(i_partition_name.partition_name || ' : ' ||                           i_index_name.Index_Name);      execute immediate 'ALTER INDEX ' || i_index_name.Index_Name ||                        ' REBUILD PARTITION ' ||                        i_partition_name.partition_name || ' ONLINE';    END LOOP;  END LOOP;end;
Rebuilding all invalid partition indexes online

In the following stored procedure, you can view all the partition indexes of a specified table, traverse each partition in sequence, and recreate all invalid partition indexes online.

declare  v_table_name varchar2(100) := upper(table_name);  v_status     varchar2(8);begin  FOR i_index_name IN (SELECT index_name                         FROM user_indexes                        WHERE table_name = upper(v_table_name)                          and partitioned = 'YES') LOOP    FOR i_partition_name IN (SELECT partition_name                               FROM user_tab_partitions                              WHERE table_name = upper(v_table_name)) LOOP      SELECT status        into v_status        FROM user_ind_partitions       WHERE index_name = i_index_name.index_name         and partition_name = i_partition_name.partition_name;      if v_status = 'UNUSABLE' THEN        dbms_output.put_line(i_partition_name.partition_name || ' : ' ||                             i_index_name.Index_Name);        execute immediate 'ALTER INDEX ' || i_index_name.Index_Name ||                          ' REBUILD PARTITION ' ||                          i_partition_name.partition_name || ' ONLINE';      END IF;    END LOOP;  END LOOP;end;

You can also use the nologging and compress parameters for the preceding re-indexing (see re-indexing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.