"ORA-01502: 索引''或這類索引的分區處於不可用狀態"的解決方案

這個問題的原因是索引失效,

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日
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章