欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

自动重建索引

程序员文章站 2022-09-11 08:14:23
1.shell脚本的方式 index_re.sh sqlplus / as sysdba <

1.shell脚本的方式

index_re.sh

sqlplus / as sysdba <<EOF
spool /tmp/i.sql rep
select 'alter index '||owner||'."'||index_name||'" rebuild;' from dba_indexes where owner='SCOTT' and status='UNUSABLE';
spool off
ho sed '/^alter index /p' -n /tmp/i.sql > /tmp/i1.sql
start /tmp/i1.sql
EOF

2.匿名PLSQL

begin
for i in (select index_name from user_indexes where status = 'UNUSABLE') loop
execute immediate 'alter index "'||i.index_name||'" rebuild';
end loop;
end;
/

3.调用过程

create or replace procedure index_rebuild
is
begin
for i in (select index_name from user_indexes where status = 'UNUSABLE') loop
execute immediate 'alter index "'||i.index_name||'" rebuild';
end loop;
end;
/

exec index_rebuild;