在 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;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值