Oracle 重建所有表的索引的存储过程 (重建索引)

create or replace procedure p_rebuild_all_index
   (tablespace_name in varchar2,--这里是表空间名,如果不改变表空间,可以传入null
   only_unusable in boolean)    --是否仅对无效的索引操作
as
   sqlt varchar(200);
begin
    --只取非临时索引
    for idx in (select index_name, tablespace_name, status from user_indexes where temporary = 'N') loop
        --如果是如重建无效的索引,且当索引不是无效时,则跳过
        if only_unusable = true and idx.status <> 'UNUSABLE' then
           goto continue;
        end if;

        if (tablespace_name is null) or idx.status = 'UNUSABLE' then
           --如果没有指定表空间,或索引无效,则在原表空间重建
           sqlt := 'alter index ' || idx.index_name || ' rebuild ';
        elsif upper(tablespace_name) <> idx.tablespace_name then
           --如果指定的不同的表空间,则在指定表空间待建索引
           sqlt := 'alter index ' || idx.index_name || ' rebuild tablespace ' || tablespace_name;
        else
           --如果表空间相同,则跳过
           goto continue;
        end if;

        dbms_output.put_line(idx.index_name);
        EXECUTE IMMEDIATE sqlt;
        <<continue>>
        null;
     end loop;
end;
/*
功能:重建索引。
说明:如果表空间参数传入null,则在原表空间内重建索引,否则在目标表空间重建索引。
      如果表空间相同,则跳过。
      only_unusable表示是否只对无效的索引进行重建
作者:81,   2007年6月26日
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值