首页 > 解决方案 > 查找所有需要重建的索引

问题描述

我一直在寻找一种方法来找到所有需要重建的索引。但是在网上找不到语法。

有没有找到相同的语法?

标签: oracleindexingoracle12c

解决方案


无法使用。看看下面的例子:

SQL> create table test (id number);

Table created.

SQL> create index i1t on test (id);

Index created.

使索引不可用:

SQL> alter index i1t unusable;

Index altered.

SQL> select index_name from user_indexes where status = 'UNUSABLE';

INDEX_NAME
------------------------------
I1T

重建它并再次检查其状态:

SQL> alter index i1t rebuild;

Index altered.

SQL> select index_name from user_indexes where status = 'UNUSABLE';

no rows selected

SQL>

当然,您不会手动重建它们 - 编写一个脚本来为您完成。例如:

SQL> set serveroutput on;
SQL> begin
  2    for cur_r in (select index_name from user_indexes
  3                  where status = 'UNUSABLE'
  4                 )
  5    loop
  6      dbms_output.put_line('Rebuilding index ' || cur_r.index_name);
  7      execute immediate 'alter index ' || cur_r.index_name || ' rebuild';
  8    end loop;
  9  end;
 10  /
Rebuilding index I2T
Rebuilding index I1T

PL/SQL procedure successfully completed.

SQL>

推荐阅读