在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'
如上截图所示,如果索引损坏(corrupt index),那么使用analyze index validate structure就会报错。要检查整个数据库所有的损坏索引(corrupt indexes)的话,就可以借助下面脚本:
spool analy_index.sql
set pagesize 50000;
select
'analyze index ' || owner || '.' || index_name|| ' validate structure;' from dba_indexes;
spool off;
@analy_index.sql
2:使用系统视图v$database_block_corruption查看损坏索引
如下所示,我们使用这个脚本来查看出现坏块的索引,发现这个脚本无法找出坏块索引。
set pagesize 50 linesize 170
col segment_name format a30
col partition_name format a30
select distinct file#,
segment_name,
segment_type,
tablespace_name,
partition_name
from dba_extents a,
v$database_block_corruption b
where a.file_id = b.file#
and a.block_id <= b.block#
and a.block_id + a.blocks >= b.block#;
原因分析如下,视图v$database_block_corruption中有坏块记录,但是我们将索引段的第一个块标记为坏块后,在dba_extents中没有该索引段的记录了。所以这种情况下的索引损坏,这个sql语句根本无法找出坏块索引。
sql> select file_id,
2 segment_name,
3 segment_type
4 from dba_extents
5 where file_id = 8 ;
no rows selected
sql> select file_id,
2 segment_name,
3 segment_type
4 from dba_extents
5 where owner = 'kerry';
file_id segment_name segment_type
---------- -------------------------------- ------------------
7 test table
7 test table
sql>
因为一个段的第一个区的第一个块是first level bitmap block,第二个块是second level bitmap block,这两个块是用来管理free block的,第三个块是pagetable segment header,这个块才是segment里的header_block,再后面的块就是用来记录数据的。关于这些知识,可以参考我博客oracle关于段的header_block的一点浅析。而我们最上面的例子,是将第一个块构造为坏块,所以导致上面sql无法查出。 我们重新构造案例,如我们将索引段的数据块构造为坏块,例如下面,将块号148人为构造坏块。那么此时这个脚本就能找出坏块索引了。所以综上述实验可以看出,这个脚本查找坏块索引是有条件的,要看索引段损坏的块是什么类型
sql> select file_id,
2 block_id,
3 blocks
from dba_extents
4 5 where owner ='&owner'
6 and segment_name = '&table_name';
enter value for owner: kerry
old 5: where owner ='&owner'
new 5: where owner ='kerry'
enter value for table_name: ix_test
old 6: and segment_name = '&table_name'
new 6: and segment_name = 'ix_test'
file_id block_id blocks
---------- ---------- ----------
8 144 8
8 152 8
sql> select header_file
2 , header_block
3 , bytes
4 , blocks
5 , extents
from dba_segments
6 7 where owner='&owner' and segment_name='&segment_name';
enter value for owner: kerry
enter value for segment_name: ix_test
old 7: where owner='&owner' and segment_name='&segment_name'
new 7: where owner='kerry' and segment_name='ix_test'
header_file header_block bytes blocks extents
----------- ------------ ---------- ---------- ----------
8 146 131072 16 2
sql>
rman> recover datafile 8 block 148 clear;