"ORA-01502: 索引''或这类索引的分区处于不可用状态"的解决方案

来源:互联网 发布:java计算圆的面积 编辑:程序博客网 时间:2024/05/02 06:11
    

        "ORA-01502: 索引''或这类索引的分区处于不可用状态"的解决方案            

        分类:            oracle                  5496人阅读     评论(0)    收藏    举报    
nulluser存储sql

这个问题的原因是索引失效,

select index_name ,status  from user_indexes where Status = 'UNUSABLE' 可以查询出失效的索引,

通过 select 'alter index ' || index_name || ' rebuild;' from user_indexes where Status = 'UNUSABLE' 可以构建重建索引的sql语句。

将sql结果语句copy出来执行即可。


方案二:写一个存储过程 ,存储过程如下:

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 0
原创粉丝点击