您的位置:首页 > 其它

使用ORWID模拟坏块并修复过程

2014-01-15 12:04 274 查看
一、Rowid定义(官网)

A globally unique address for a row in a database.

Oracle Database uses a rowid to uniquely identify a row. Internally, the rowid is a structure that holds information that the database needs to access a row. A rowid is not physically stored in the database, but is inferred from the file and block on which
the data is stored.

An extended rowid includes a data object number. This rowid type uses a base 64 encoding of the physical address for each row. The encoding characters are A-Z, a-z, 0-9, +, and /.

Example 12-1 queries the ROWID pseudocolumn to show the extended rowid of the row in the employees table for employee 100.

Example 12-1 ROWID Pseudocolumn

SQL> SELECT ROWID FROM employees WHERE employee_id = 100;

ROWID

------------------

AAAPecAAFAAAABSAAA

Figure 12-8 illustrates the format of an extended rowid.

Figure 12-8 ROWID Format

Description of "Figure 12-8 ROWID Format"

An extended rowid is displayed in a four-piece format, OOOOOOFFFBBBBBBRRR, with the format divided into the following components:

• OOOOOO

The data object number identifies the segment (data object AAAPec in Example 12-1). A data object number is assigned to every database segment. Schema objects in the same segment, such as a table cluster, have the same data object number.

• FFF

The tablespace-relative data file number identifies the data file that contains the row (file AAF in Example 12-1).

• BBBBBB

The data block number identifies the block that contains the row (block AAAABS in Example 12-1). Block numbers are relative to their data file, not their tablespace. Thus, two rows with identical block numbers could reside in different data files of the same
tablespace.

• RRR

The row number identifies the row in the block (row AAA in Example 12-1).

After a rowid is assigned to a row piece, the rowid can change in special circumstances. For example, if row movement is enabled, then the rowid can change because of partition key updates, Flashback Table operations, shrink table operations, and so on. If
row movement is disabled, then a rowid can change if the row is exported and imported using Oracle Database utilities.

Note:

Internally, the database performs row movement as if the row were physically deleted and reinserted. However, row movement is considered an update, which has implications for triggers.

二、一些说明

rowid是伪列(pseudocolumn),伪劣的意思是实际上这一列本身在数据字典中并不存在,在查询结果输出时它被构造出来的。

rowid并不会真正存在于表的data block中,但是他会存在于index当中,用来通过rowid来寻找表中的行数据。

ROWID 格式:

扩展的ROWID 在磁盘上需要10 个字节的存储空间,并使用18 个字符来显示。

它包含下列组成元素:

1. 数据对象编号:每个数据对象(如表或索引)在创建时都分配有此编号,并且此编号在数据库中是唯一的

2. 相关文件编号:此编号对于表空间中的每个数据文件是唯一的

3. 块编号:表示包含此行的块在数据文件中的位置

4. 行编号:标识块头中行目录位置的位置

在内部,存储的10个字节(bytes),即80位(bit)又按如下规则进行划分:

(1)数据对象编号需要32 bit

(2)相关文件编号需要10 bit

(3)块编号需要22 bit

(4)行编号需要16 bit

查看制定rowid内容

SQL> select file_id,relative_fno,block_id from dba_extents where owner='CENTER' and segment_name='TEST';

FILE_ID RELATIVE_FNO BLOCK_ID

---------- ------------ ----------

6 6 136

---rowed不保存在数据字典中

ORA-01446: 无法使用 DISTINCT, GROUP BY 等子句从视图中选择 ROWID 或采样

SQL> set serveroutput on

SQL>

SQL> DECLARE

  2   v_rowid_type NUMBER;

  3   v_OBJECT_NUMBER NUMBER;

  4   v_RELATIVE_FNO NUMBER;

  5   v_BLOCK_NUMBERE_FNO NUMBER;

  6   v_ROW_NUMBER NUMBER;

  7   BEGIN

  8   DBMS_ROWID.rowid_info (

  9   rowid_in => 'AAAR7bAAFAAAACFAAA',

 10   rowid_type => v_rowid_type,

 11   object_number => v_OBJECT_NUMBER,

 12   relative_fno => v_RELATIVE_FNO,

 13   block_number => v_BLOCK_NUMBERE_FNO,

 14   ROW_NUMBER => v_ROW_NUMBER);

 15   DBMS_OUTPUT.put_line ('ROWID_TYPE: ' || TO_CHAR (v_rowid_type));

 16   DBMS_OUTPUT.put_line ('OBJECT_NUMBER: ' || TO_CHAR (v_OBJECT_NUMBER));

 17   DBMS_OUTPUT.put_line ('RELATIVE_FNO: ' || TO_CHAR (v_RELATIVE_FNO));

 18   DBMS_OUTPUT.put_line ('BLOCK_NUMBER: ' || TO_CHAR (v_BLOCK_NUMBERE_FNO));

 19   DBMS_OUTPUT.put_line ('ROW_NUMBER: ' || TO_CHAR (v_ROW_NUMBER));

 20   END;

 21  /

 

ROWID_TYPE: 1

OBJECT_NUMBER: 73435

RELATIVE_FNO: 5

BLOCK_NUMBER: 133

ROW_NUMBER: 0

也可以使用如下方法查看:

--根据rowid抽块对像编号

select dbms_rowid.rowid_object('AAASUSAAGAAAACNAAA') obj#  from dual;

--根据rowid抽取表空间相对文件号

select dbms_rowid.rowid_relative_fno('AAASUSAAGAAAACNAAA') rfile# from dual;

--根据rowid抽取块号

select dbms_rowid.rowid_block_number('AAASUSAAGAAAACNAAA') block# from dual;

--根据rowid抽取行号

select dbms_rowid.rowid_row_number('AAASUSAAGAAAACNAAA') row#  from dual;

查找最小快rowid和最大rowid,这里可以模拟生成查找rowid,但是不一定很准确,因为可能存在删除的row,大致范围可以找出来

select dbms_rowid.rowid_create(1, 12345,10,100,0) from dual;

--结果是AAAS+/AANAAAAF1AAA

select dbms_rowid.rowid_create(1, 12345,10,101,0) from dual;

---结果是AAAS+/AANAAAAF2AAA

三,模拟创建坏块

1) 创建表空间,用户center,指定表空间tbs,创建表test,制定到表空间,插入数据

2) 各种方式修改文件,创建坏块,比如编辑数据文件,删除修改其中一个字符,ultraedit工具可行

四,处理方式

使用dbms_repair:

1、 找到坏块数据量&保存插入临时表

SELECT /*+ ROWID(A) */ COUNT(*) FROM test

WHERE ROWID>='AAAS+/AANAAAAF2AAA';

CREATE TABLE test_TMP TABLESPACE XXX

AS

SELECT /*+ ROWID(A) */ * FROM test

WHERE ROWID<'AAAS+/AANAAAAF2AAA'; -- 这里可能多个区间

2,使用dbms_repair跳过坏块,否则查询表出现问题

exec dbms_repair.skip_corrupt_blocks('CENTER','TEST');

查询是否成功

select skip_corrupt from dba_tables where table_name='TEST';

查询count数量,然后跟上边的对比

使用rman修复:

1,rman检查

整个库检查

run {

allocate channel d1 type disk;

allocate channel d2 type disk;

backup validate check logical database;

}

单个文件检查:

backup validate datafile 4;

然后查看结果:select * from V$DATABASE_BLOCK_CORRUPTION ;

当然也可以使用dbv检查:
比如:dbv file='/ora01/app/oracle/oradata/prodc/tbs1_datafile.dbf' blocksize=8192 logfile='/ora01/app/oracle/oradata/prodc/dbv.log'

rman小知识:

rman在备份数据时,会将数据读入读缓存,然会将数据从读缓存写入写缓存并最终写入数据库。

在从读缓存到写缓存的过程中,rman会进行物理一致性的检测(db_block_checksum未true)

和逻辑一致性检测(使用了check logical选项)

SQL> show parameter db_block

 

NAME                     TYPE    VALUE

------------------------------------ ----------- ------------------------------

db_block_buffers             integer     0

db_block_checking            string  FALSE

db_block_checksum            string  TRUE

db_block_size                integer     8192

我们仅仅希望验证一下数据文件是否可以备份,而不想进行真正的备份,这是我们就可以使用validate命令了

可以使用backup validate database,检查物理文件的可读性和位置等

使用backup validate check logical database;backup validate datafile xxx 检查坏块,没试过

而使用restore database validate; 用来检查备份集的完整性,使用restore命令的check logical参数能够更完全的检查最新备份集

当然也可以检查 restore validate controlfile/spfile

2,确定坏块的信息

SELECT e.owner,

       e.segment_type,

       e.segment_name,

       e.partition_name,

       c.file#,

       greatest(e.block_id, c.block#) corr_start_block#,

       least(e.block_id + e.blocks - 1, c.block# + c.blocks - 1) corr_end_block#,

       least(e.block_id + e.blocks - 1, c.block# + c.blocks - 1) -

       greatest(e.block_id, c.block#) + 1 blocks_corrupted,

       null description

  FROM dba_extents e, v$database_block_corruption c

 WHERE e.file_id = c.file#

   AND e.block_id <= c.block# + c.blocks - 1

   AND e.block_id + e.blocks - 1 >= c.block#

UNION

SELECT s.owner,

       s.segment_type,

       s.segment_name,

       s.partition_name,

       c.file#,

       header_block corr_start_block#,

       header_block corr_end_block#,

       1 blocks_corrupted,

       'Segment Header' description

  FROM dba_segments s, v$database_block_corruption c

 WHERE s.header_file = c.file#

   AND s.header_block between c.block# and c.block# + c.blocks - 1

UNION

SELECT null owner,

       null segment_type,

       null segment_name,

       null partition_name,

       c.file#,

       greatest(f.block_id, c.block#) corr_start_block#,

       least(f.block_id + f.blocks - 1, c.block# + c.blocks - 1) corr_end_block#,

       least(f.block_id + f.blocks - 1, c.block# + c.blocks - 1) -

       greatest(f.block_id, c.block#) + 1 blocks_corrupted,

       'Free Block' description

  FROM dba_free_space f, v$database_block_corruption c

 WHERE f.file_id = c.file#

   AND f.block_id <= c.block# + c.blocks - 1

   AND f.block_id + f.blocks - 1 >= c.block#

 order by file#, corr_start_block#;

--根据段名找到数据对象号

SELECT tablespace_name, segment_type, owner, segment_name

FROM dba_extents

WHERE file_id = &fileid

and &blockid between block_id AND block_id + blocks - 1;

3,恢复

blockrecover datafile 6 block 23;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: