使用ORWID類比壞塊並修複過程

來源:互聯網
上載者:User

使用rowid類比資料庫壞塊修複過程:

一、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

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

select rowid,file_id,relative_fno,block_id from dba_extents where owner='CENTER' and segment_name='TEST'
---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 => 'AAASUSAAGAAAACNAAA',
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: 75026
RELATIVE_FNO: 6
BLOCK_NUMBER: 141
ROW_NUMBER: 0
1、 建立壞塊
1) 建立資料表空間,使用者center,指定資料表空間tps,建立表test,制定到資料表空間,插入資料
2) 各種方式修改檔案,建立壞塊,比如編輯資料檔案,刪除修改其中一個字元,ultraedit工具可行

2、 查詢壞塊
比如查詢壞塊的表白檔案號10,快好100----ORA-01578: ORACLE 資料區塊損壞
3、 查詢其它資訊
The "LOW_RID" is the lowest rowid INSIDE the corrupt block:
SELECT dbms_rowid.rowid_create(1,<DATA_OBJECT_ID>,<RFN>,<BL>,0) LOW_RID
from DUAL;

The "HI_RID" is the first rowid AFTER the corrupt block:
SELECT dbms_rowid.rowid_create(1,<DATA_OBJECT_ID>,<RFN>,<BL>+1,0) HI_RID
from DUAL;
1) 根據段名找到資料對象號
尋找段名
SELECT tablespace_name, segment_type, owner, segment_name
FROM dba_extents
WHERE file_id =13
AND 100 between block_id AND block_id + blocks - 1 ;
尋找資料對象號
SELECT data_object_id
FROM dba_objects
WHERE object_name = 'XXX---上一部找的set_name' and owner='上一部尋找的owner';
結果比如是12345
尋找最小快rowid和最大rowid
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

4、 找回
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';
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';

5,對比
原表對比
exec dbms_repair.skip_corrupt_blocks('CENTER','TEST');
查詢是否成功
select skip_corrupt from dba_tables where table_name='TEST';
查詢count數量,然後跟上邊的對比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.