创建使用program对无效索引进行重建



--创建使用program对无效索引进行重建
  • shell脚本
  • 匿名pl/sql
  • 创建procedure

--模拟将索引不可用
SYS@PROD2> alter table scott.emp move;

Table altered.

SYS@PROD2> alter table scott.dept move;

Table altered.

SYS@PROD2> select 'alter index '||owner||'."'||index_name||'" rebuild;' from dba_indexes where OWNER='SCOTT' and status='UNUSABLE';

'ALTERINDEX'||OWNER||'."'||INDEX_NAME||'"REBUILD;'
------------------------------------------------------------------------------------
alter index SCOTT."PK_DEPT" rebuild;
alter index SCOTT."PK_EMP" rebuild;

1.通过shell脚本实现

[oracle@ocm1 ~]$ vi index_re.sh  --编写脚本
[oracle@ocm1 ~]$ cat index_re.sh 
#!/bin/bash

export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_SID=PROD2
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
[oracle@ocm1 ~]$ chmod +x index_re.sh --给予执行权限
[oracle@ocm1 ~]$ ./index_re.sh 

SQL*Plus: Release 11.2.0.3.0 Production on Fri Jan 6 15:58:20 2017

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SYS@PROD2> SYS@PROD2> 
'ALTERINDEX'||OWNER||'."'||INDEX_NAME||'"REBUILD;'
------------------------------------------------------------------------------------
alter index SCOTT."PK_DEPT" rebuild;
alter index SCOTT."PK_EMP" rebuild;

SYS@PROD2> SYS@PROD2> 
SYS@PROD2> 
Index altered.


Index altered.

SYS@PROD2> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@ocm1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Fri Jan 6 15:58:30 2017

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

--验证索引确实被重建
SYS@PROD2> select 'alter index '||owner||'."'||index_name||'" rebuild;' from dba_indexes where OWNER='SCOTT' and status='UNUSABLE';

no rows selected

2.编写匿名sql

SYS@PROD2> alter table scott.dept move;
al
Table altered.

SYS@PROD2> alter table scott.emp move;

Table altered.

SYS@PROD2> begin
  2  for i in (select index_name from user_indexes where status='UNUSABLE') loop
  3  execute immediate 'alter index "'||i.index_name||'" rebuild';
  4  end loop;
  5  end;
  6  /

PL/SQL procedure successfully completed.

SYS@PROD2> save index_plsql.sql
Created file index_plsql.sql
SYS@PROD2> conn scott/tiger
Connected.

SCOTT@PROD2> start index_plsql.sql

PL/SQL procedure successfully completed.

SCOTT@PROD2> select index_name,status from user_indexes;

INDEX_NAME		       STATUS
------------------------------ --------
PK_EMP			       VALID
PK_DEPT 		       VALID

3.创建procedure

SCOTT@PROD2> create or replace procedure index_rebuild
  2  is
  3  begin
  4  for i in (select index_name from user_indexes where status='UNUSABLE') loop
  5  execute immediate 'alter index "'||i.index_name||'" rebuild';
  6  end loop;
  7  end;
  8  /

Procedure created.

SCOTT@PROD2> save index_procudure.sql
Created file index_procudure.sql
SCOTT@PROD2> alter table emp move;

Table altered.

SCOTT@PROD2> alter table dept move;

Table altered.

SCOTT@PROD2> select index_name,status from user_indexes;

INDEX_NAME		       STATUS
------------------------------ --------
PK_EMP			       UNUSABLE
PK_DEPT 		       UNUSABLE

SCOTT@PROD2> exec index_rebuild;

PL/SQL procedure successfully completed.

SCOTT@PROD2> select index_name,status from user_indexes;

INDEX_NAME		       STATUS
------------------------------ --------
PK_EMP			       VALID
PK_DEPT 		       VALID


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值