Oracle中重建表的全局的索引、分区索引、及同时建全局和分区索引----脚本

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

ALTER INDEX INDEX_NAME Rebuild;

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

1.重建某个表上的索引,执如下PL/SQL代码:

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

begin

 for c1 in (select t.index_name, t.partitioned from user_indexes t where table_name = 'T2')

loop

  if c1.partitioned='NO' then

   execute immediate 'ALTER INDEX ' || c1.index_name || ' REBUILD';

  end if;

 end loop;

end;

/

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

 

2.而重建分区表上的索引 

2.1如果这个索引是分区的,重建所有分区方法:

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;

/

2.2如果这个索引是非分区的,重建所有分区索引法: (和普通表上的相同)

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;

3.重建这张表上的全部索引(全局和分区)

begin

for c1 in (select t.index_name, t.partitioned from user_indexes t where table_name = 'T2'))

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;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Running Sun丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值