[20180627]truncate table的另類恢復.txt

lfree發表於2018-06-28

[20180627]truncate table的另類恢復.txt

--//前幾天看連結
--//提到幾種方式不影響資料的恢復,突然看到reuse storage選項,感覺在truncate時使用該引數時儲存空間不會回收,
--//感覺這樣應該還是會降低高水位標誌.

--//使用reuse storage選項,儲存空間不會回收,另外在truncate時,資料段號data_object_id會發生變化,自己想想是否
--//可以透過修改相應資料庫的段號指向新的段號來實現恢復.自己動手測試看看.

1.環境:
SCOTT@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

create table empx as select * from emp;

SCOTT@book> select rowid,empx.* from empx where rownum=1;
ROWID                   EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
------------------ ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
AAAWFOAAEAAAALbAAA       7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20

SCOTT@book> @ rowid AAAWFOAAEAAAALbAAA
    OBJECT       FILE      BLOCK        ROW ROWID_DBA            DBA                  TEXT
---------- ---------- ---------- ---------- -------------------- -------------------- ----------------------------------------
     90446          4        731          0  0x10002DB           4,731                alter system dump datafile 4 block 731 ;

SCOTT@book> select object_id,data_object_id from dba_objects where owner=user and object_name='EMPX';
OBJECT_ID DATA_OBJECT_ID
---------- --------------
     90446          90446

SCOTT@book> column PARTITION_NAME noprint
SCOTT@book> select * from dba_extents where owner=user and segment_name='EMPX';
OWNER  SEGMENT_NAME         SEGMENT_TYPE       TABLESPACE_NAME                 EXTENT_ID    FILE_ID   BLOCK_ID      BYTES     BLOCKS RELATIVE_FNO
------ -------------------- ------------------ ------------------------------ ---------- ---------- ---------- ---------- ---------- ------------
SCOTT  EMPX                 TABLE              USERS                                   0          4        728      65536          8            4

SCOTT@book> truncate table empx reuse storage;
Table truncated.

SCOTT@book> select object_id,data_object_id from dba_objects where owner=user and object_name='EMPX';
OBJECT_ID DATA_OBJECT_ID
---------- --------------
     90446          90447

--//可以發現truncate後data_object_id=90447發生變化.

SCOTT@book> select * from dba_extents where owner=user and segment_name='EMPX';
OWNER  SEGMENT_NAME         SEGMENT_TYPE       TABLESPACE_NAME                 EXTENT_ID    FILE_ID   BLOCK_ID      BYTES     BLOCKS RELATIVE_FNO
------ -------------------- ------------------ ------------------------------ ---------- ---------- ---------- ---------- ---------- ------------
SCOTT  EMPX                 TABLE              USERS                                   0          4        728      65536          8            4

SCOTT@book> alter system checkpoint;
System altered.

2.看看是否透過修改相應塊的段號:

BBED> set dba 4,728
        DBA             0x010002d8 (16777944 4,728)

BBED> p /d ktbbh.ktbbhsid.
union ktbbhsid, 4 bytes                     @24
   ub4 ktbbhsg1                             @24       90446
   ub4 ktbbhod1                             @24       90446
--//可以發現truncate後資料段ID還是原來的90446,如果有資料插入段號就發生變化.我想透過修改段號實現資料恢復.

BBED> assign dba 4,731 ktbbh.ktbbhsid.ktbbhod1=90447;
Warning: contents of previous BIFILE will be lost. Proceed? (Y/N) y
ub4 ktbbhod1                                @24       0x0001614f

BBED> sum apply dba 4,731
Check value for File 4, Block 731:
current = 0x9cc2, required = 0x9cc2

3.檢查是否能正常顯示:
SCOTT@book> alter system flush buffer_cache;
System altered.

SCOTT@book> select rowid,empx.* from empx ;
no rows selected

--//不行!!看看直接使用rowid是否可以查詢.
SCOTT@book> select rowid,empx.* from empx where rowid=dbms_rowid.rowid_create (1, 90447, 4, 731, 0) ;
ROWID                   EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
------------------ ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
AAAWFPAAEAAAALbAAA       7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20

--//可以發現使用rowid可以查詢對應記錄.如果寫成如下:
SCOTT@book> select rowid,empx.* from empx where rowid between dbms_rowid.rowid_create (1, 90447, 4, 731, 0) and dbms_rowid.rowid_create (1, 90447, 4, 731, 1);
no rows selected

--//因為執行計劃是這樣:
Plan hash value: 4633905
-------------------------------------------------------------------------------------
| Id  | Operation                   | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |      |        |       |     2 (100)|          |
|*  1 |  TABLE ACCESS BY ROWID RANGE| EMPX |      1 |    99 |     2   (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / EMPX@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access(ROWID>=CHARTOROWID("DBMS_ROWID"."ROWID_CREATE"(1,90447,4,731,0)
              ) AND ROWID<=CHARTOROWID("DBMS_ROWID"."ROWID_CREATE"(1,90447,4,731,1)))
--//執行計劃TABLE ACCESS BY ROWID RANGE,注意範圍這個關鍵字.這樣要訪問段頭,因為是truncate表後降低了高水位標識,
--//這樣oracle認為要訪問的資料塊在高水位之上,這樣就沒有結果集.
--//而前面的執行計劃是TABLE ACCESS BY USER ROWID.直接透過rowid訪問塊.越過了資料段頭的訪問.
SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  3vzm2qyuft1dh, child number 0
-------------------------------------
select rowid,empx.* from empx where rowid=dbms_rowid.rowid_create (1, 90447, 4, 731, 0)
Plan hash value: 3191763077
------------------------------------------------------------------------------------
| Id  | Operation                  | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |      |        |       |     1 (100)|          |
|   1 |  TABLE ACCESS BY USER ROWID| EMPX |      1 |    99 |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / EMPX@SEL$1

3.有了以上思路,應該可以恢復全部記錄.
--//實際上就是當段頭損壞.

SCOTT@book> CREATE TABLE empy tablespace tea  AS SELECT * FROM empx where  1=0;
Table created.

--//網上找到的指令碼,參考連結:
set serveroutput on
set concat off
DECLARE
nrows number;
rid rowid;
dobj number;
ROWSPERBLOCK number;
BEGIN
ROWSPERBLOCK:=736;  --估算最大的一個塊中記錄條數,8K最多736條記錄1塊.
nrows:=0;

select data_object_id  into dobj
from dba_objects
where owner = 'SCOTT'
and object_name = 'EMPX'
-- and subobject_name = '<table partition>'  Add this condition if table is partitioned
;

for i in (select relative_fno, block_id, block_id+blocks-1 totblocks
           from dba_extents
           where owner = 'SCOTT'
             and segment_name = 'EMPX'
-- and partition_name = '<table partition>' Add this condition if table is partitioned
-- and file_id != <OFFLINED DATAFILE> This condition is only used if a datafile needs to be skipped due to ORA-376 (A)
          order by extent_id)
loop
   for br in i.block_id..i.totblocks loop
    for j in 1..ROWSPERBLOCK loop
    begin
      rid := dbms_rowid.ROWID_CREATE(1,dobj,i.relative_fno, br , j-1);
      insert into empy
      select /*+ ROWID(A) */ *
      from empx A
      where rowid = rid;
      if sql%rowcount = 1 then nrows:=nrows+1; end if;
      if (mod(nrows,10000)=0) then commit; end if;
    exception when others then null;
    end;
    end loop;
  end loop;
end loop;
COMMIT;
dbms_output.put_line('Total rows:'||to_char(nrows));
END;
/

--//執行後:
SCOTT@book> select * from empy;
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30
      7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30
      7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20
      7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30
      7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20
      7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30
      7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20
      7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10
14 rows selected.

4.你可以將這個恢復不實際,我僅僅恢復一個塊.而且是1條1條的恢復,應該效率很低.多個資料塊就比較麻煩了,我僅僅提供1個解決方法.
--//有時間寫一個大一點的表的恢復.總之透過修改對於資料塊的段號恢復是可行的.

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

相關文章