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

来源:互联网 发布:海贼王启航礼包淘宝 编辑:程序博客网 时间:2024/05/22 14:27
https://my.oschina.net/u/232879/blog/289638

"ora-01502 索引或这类索引的分区处于不可用状态"的解决方案
 收藏
 
  • 发表于 2年前 
  • 阅读 4341 
  • 收藏 1 
  • 点赞 0 
  • 评论 0
摘要: oracle ora-01502索引失效 原因查询 及问题解决

如题,在ORACLE中自动新增删除分区之后,会报出ora-01502的错误。

首先,问题的原因是在于进行表结构修改后,索引失效。问题解决有如下几个方式:

1、手动查询重建

(1)先查询失效索引,语句为:

      select index_name ,status  from user_indexes where Status = 'UNUSABLE' ;

(2)重建索引,语句为: 

  alter index  xxx rebuild;

手动操作有个快捷的方式,可以将上面的步骤合成为一个查询语句:   

 select 'alter index ' || index_name || ' rebuild;' from user_indexes where Status = 'UNUSABLE' ;

这样直接复制查询出来的语句手动执行即可。

2、将上面的手动操作做成存储过程执行的“只能自动”

(1)创建重建索引的存储过程,语句如下:

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;

(2) 创建执行重建的存储过程

CREATE OR REPLACE PROCEDURE EXEC_REBUILD_PROC AS    v_err_num  NUMBER;  --ORA错误号   v_err_msg  VARCHAR2(100); --错误描述BEGIN --10代表创建10天的分区,tablespace代表表空间名 p_rebuild_all_index(NULL,true); COMMIT;EXCEPTION WHEN OTHERS THEN  v_err_num := SQLCODE;  v_err_msg := SUBSTR(SQLERRM, 1, 100);  dbms_output.put_line('EXEC_REBUILD_PROC执行出现异常,错误码='|| v_err_num || '错误描述=' || v_err_msg);END EXEC_REBUILD_PROC;

如此即可。以上方式均已测试可用。

0 0
原创粉丝点击