在Oracle資料庫中如何找出損壞索引呢? 下麵我們人為構造一個案例,將索引塊損壞。如下案例所示: SQL> create tablespace test_data 2 datafile '/u01/app/oracle/oradata/gsp/test_data_01.dbf' 3 size 20... ...
在Oracle資料庫中如何找出損壞索引呢? 下麵我們人為構造一個案例,將索引塊損壞。如下案例所示:
SQL> create tablespace test_data
2 datafile '/u01/app/oracle/oradata/gsp/test_data_01.dbf'
3 size 200M autoextend off
4 logging
5 segment space management auto
6 extent management local;
Tablespace created.
SQL> create tablespace test_index
2 datafile '/u01/app/oracle/oradata/gsp/test_idx_01.dbf'
3 size 200M autoextend off
4 logging
5 segment space management auto
6 extent management local;
Tablespace created.
SQL> create user kerry
2 identified by 123456
3 default tablespace test_data;
User created.
SQL> grant connect to kerry;
SQL> grant resource to kerry;
上述腳本是創建表空間,創建用戶kerry並授權,然後使用kerry賬號登錄資料庫,構造測試數據,在TEST表上創建索引IX_TEST
SQL> show user;
USER is "KERRY"
SQL>
SQL> CREATE TABLE TEST(ID NUMBER(10), NAME VARCHAR2(64));
Table created.
SQL> DECLARE I NUMBER;
2 BEGIN
3 FOR I IN 1..1000 LOOP
4 INSERT INTO TEST VALUES(I, LPAD('T', 60));
5 END LOOP;
6 COMMIT;
7 END;
8 /
PL/SQL procedure successfully completed.
SQL> CREATE INDEX IX_TEST ON KERRY.TEST(NAME) TABLESPACE TEST_INDEX;
Index created.
然後使用下麵腳本找到索引段資料庫文件ID,以及索引段的第一個塊的塊號。
SQL> show user;
USER is "SYS"
SQL> col segment_name for a32;
SQL> col header_file for 9999;
SQL> col header_block for 9999;
SQL> select segment_name
2 ,header_file
3 ,header_block
4 ,blocks
5 from dba_segments ds
6 where ds.owner='KERRY' and ds.segment_name='IX_TEST';
SEGMENT_NAME HEADER_FILE HEADER_BLOCK BLOCKS
-------------------------------- ----------- ------------ ----------
IX_TEST 8 130 16
SQL>
構造壞塊的方法有不少(例如BBED等),這裡我們使用RMAN下麵的命令clear,可以標記數據塊為corrupt,標記數據文件8中130號數據塊為壞塊。
[oracle@DB-Server ~]$ rman target /
Recovery Manager: Release 11.2.0.1.0 - Production on Thu Sep 13 17:41:05 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: GSP (DBID=644393201)
RMAN> recover datafile 8 block 130 clear;
Starting recover at 13-SEP-18
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=12 device type=DISK
Finished recover at 13-SEP-18
RMAN>
那麼我們先來看看使用那些方法驗證索引損壞了,測試驗證一下看看是否可行。
1:使用ANALYZE分析驗證索引結構
[oracle@DB-Server ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Thu Sep 13 17:42:03 2018
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> analyze index kerry.ix_test validate structure;
analyze index kerry.ix_test validate structure
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 8, block # 130)
ORA-01110: data file 8: '/u01/app/oracle/oradata/gsp/test_idx_01.dbf'