index損壞恢復

y81277241發表於2014-03-06


生產庫中索引和表是IO分離儲存的.索引不需要備份,而且表的更改 索引也會聯動更改,即使備份也毫無意義
即使索引表空間損壞,索引的定義也儲存在資料字典中,取出後設資料重建即可

如何取後設資料
 dbms_metadata.get_ddl('型別','物件','模式')

SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') from dual;

DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT')
--------------------------------------------------------------------------------

  CREATE TABLE "SCOTT"."EMP"
   ( "EMPNO" NUMBER(4,0),
 "ENAME" VARCHAR2(10),
 "JOB" VARCHAR2(9),
 "MGR" NUMBER(4,0),
 "HIREDATE" DATE,
 "SAL" NUMBER(7,2),
 "COMM" NUMBER(7,2),
 "DEPTNO" NUMBER(2,0),
  CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"  ENABLE,
  CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
   REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"

 

SQL>

取scott使用者下所有表的後設資料
SQL> select (select dbms_metadata.get_ddl('TABLE',table_name,owner) from dual)
 from dba_tables where owner='SCOTT';

取scott使用者下所有索引的後設資料
SQL> select (select dbms_metadata.get_ddl('INDEX',index_name,owner) from dual)
 from dba_indexes where owner='SCOTT';


SQL> create tablespace ind_tbs datafile '/db254/ind_tbs01.dbf' size 10M;

SQL> create index i1 on scott.emp(ename) tablespace ind_tbs;
SQL> create index i2 on scott.emp(sal) tablespace ind_tbs;
SQL> create index i3 on scott.emp(job) tablespace ind_tbs;


SQL> select segment_type,segment_name,owner from dba_segments where tablespace_name='IND_TBS';

SEGMENT_TYPE    SEGMENT_NAME       OWNER
------------------ ------------------------- ------------------------------
INDEX     I1        SYS
INDEX     I2        SYS
INDEX     I3        SYS


SQL> select * from scott.emp where sal < 1000;

     EMPNO ENAME      JOB        MGR HIREDATE     SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK       7902 17-DEC-80     809      20
      7900 JAMES      CLERK       7698 03-DEC-81     959      30

SQL>
SQL> ! cp 1.txt /db254/ind_tbs01.dbf
 

SQL> alter system switch logfile;

System altered.

SQL> select * from scott.emp where sal <1000;

     EMPNO ENAME      JOB        MGR HIREDATE     SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK       7902 17-DEC-80     809      20
      7900 JAMES      CLERK       7698 03-DEC-81     959      30

SQL> alter system checkpoint;

System altered.

SQL> select * from scott.emp where sal <1000;
select * from scott.emp where sal <1000
                    *
ERROR at line 1:
ORA-00376: file 6 cannot be read at this time
ORA-01110: data file 6: '/db254/ind_tbs01.dbf'

6號檔案有問題 6號檔案在哪個表空間

SQL> select tablespace_name ,file_id from dba_data_files where file_id=6;

TABLESPACE_NAME    FILE_ID
--------------- ----------
IND_TBS    6

存的是什麼內容
SQL> select segment_type,segment_name,owner from dba_segments where tablespace_name='IND_TBS';

SEGMENT_TYPE    SEGMENT_NAME       OWNER
------------------ ------------------------- ------------------------------
INDEX     I1        SYS
INDEX     I2        SYS
INDEX     I3        SYS

SQL>
SQL> 獲取後設資料
SQL> SELECT DBMS_METADATA.GET_DDL(segment_type,segment_name,owner)||';' FROM dba_segments where tablespace_name='IND_TBS';

DBMS_METADATA.GET_DDL(SEGMENT_TYPE,SEGMENT_NAME,OWNER)||';'
--------------------------------------------------------------------------------

  CREATE INDEX "SYS"."I1" ON "SCOTT"."EMP" ("ENAME")
  PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "IND_TBS"
 ;


  CREATE INDEX "SYS"."I2" ON "SCOTT"."EMP" ("SAL")
  PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "IND_TBS"
 ;


  CREATE INDEX "SYS"."I3" ON "SCOTT"."EMP" ("JOB")
  PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "IND_TBS"
 ;


刪除原表空間
SQL> DROP TABLESPACE IND_TBS;
DROP TABLESPACE IND_TBS
*
ERROR at line 1:
ORA-01549: tablespace not empty, use INCLUDING CONTENTS option


SQL> DROP TABLESPACE IND_TBS INCLUDING CONTENTS;

Tablespace dropped.


SQL> CREATE TABLESPACE IND_TBS DATAFILE '/db254/ind_tbs01.dbf' size 10M reuse;

Tablespace created.

執行後設資料

SQL> select segment_type,segment_name,owner from dba_segments where tablespace_name='IND_TBS';

SEGMENT_TYPE    SEGMENT_NAME       OWNER
------------------ ------------------------- ------------------------------
INDEX     I1        SYS
INDEX     I2        SYS
INDEX     I3        SYS

SQL> select * from scott.emp where sal < 1000;

     EMPNO ENAME      JOB        MGR HIREDATE     SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK       7902 17-DEC-80     809      20
      7900 JAMES      CLERK       7698 03-DEC-81     959      30

SQL>
  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29523859/viewspace-1102666/,如需轉載,請註明出處,否則將追究法律責任。

相關文章