表数据经过重新抽取或者大量导入的时候会破坏索引,下列方法会修复所有索引
create or replace procedure p_rebuild_all_index
(tablespace_name in varchar2,--这里是表空间名,如果不改变表空间,可以传入null
only_unusable in boolean) --是否仅对无效的索引操作
as
sqlt varchar(200);
begin
--只取非临时索引
--这个地方可根据需要设置SQL语句
for idx in (select index_name, tablespace_name, status,index_type from user_indexes where temporary = 'N' and index_type!='LOB') 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;