在 Oracle 中重建分区表上的索引

在 oracle中,重建普通表上的索引很简单。要重建特定索引,只需执行如下sql命令:

ALTER INDEX INDEX_NAME Rebuild;

这里,INDEX_NAME 代表索引的名字,下同。

如果重建某个表上的全部索引,执行如下 PL/SQL 代码:

begin
    for c1 in (select t.index_name, t.partitioned from user_indexes t where table_name = 'TABLE_NAME') loop
        if c1.partitioned='NO' then
            execute immediate 'ALTER INDEX ' || c1.index_name || ' REBUILD';
        end if;
    end loop;
end;
/

这里,TABLE_NAME 代表索引的名字,下同。

而重建分区表上的索引的方法和上面的有所不同。

如果这个索引不是分区的,那么重建的方法 和 重建普通表上的索引 相同。

如果这个索引是分区的,重建方法是执行如下sql代码:

begin
      for c2 in (select partition_name from user_ind_partitions where index_name='INDEX_NAME' and status = 'UNUSABLE')
      loop
        execute immediate 'alter index ' || c1.index_name || ' rebuild partition ' || c2.partition_name;
      end loop;
end;

重建一张表上的所有非分区索引的方法是执行如下sql代码:


begin
    for c1 in (select t.index_name, t.partitioned from user_indexes t where table_name = 'TABLE_NAME')
    loop
        if c1.partitioned='YES'
            -- rebuild every unusable partition for partitioned index
            for c2 in (select partition_name from user_ind_partitions where index_name=c1.index_name and status = 'UNUSABLE')
            loop
                execute immediate 'alter index ' || c1.index_name || ' rebuild partition ' || c2.partition_name;
            end loop;
        end if;
    end loop;
end;

而重建这张表上的全部索引的sql 代码如下:

begin
  for c1 in (select t.index_name, t.partitioned from user_indexes t where table_name = 'TABLE_NAME'))
  loop
    if c1.partitioned='NO' then
      -- rebuild global index directly
      execute immediate 'alter index ' || c1.index_name || ' rebuild';
    else
      -- rebuild every unusable partition for partitioned index
      for c2 in (select partition_name from user_ind_partitions where index_name=c1.index_name and status = 'UNUSABLE')
      loop
        execute immediate 'alter index ' || c1.index_name || ' rebuild partition ' || c2.partition_name;
      end loop;
    end if;
  end loop;
end;

猜你喜欢

转载自blog.csdn.net/international24/article/details/107560354