[20150409]只讀表空間與延遲塊清除.txt

lfree發表於2018-06-27

[20150409]只讀表空間與延遲塊清除.txt

--//很久以前寫,發現居然自己沒有貼出來現在補上.

--昨天測試只讀表空間的資料庫恢復問題,突然想到一種情況,如果只讀表空間存在延遲塊清除情況,這樣在下次訪問是會更新塊的資訊嗎?
--自己還是做1個測試:

1.首先在測試前,說明1點,設定表空間只讀,僅僅阻止dml操作,並不能阻止ddl操作,ddl操作的是資料字典.

SCOTT@test> @ &r/ver1

PORT_STRING                    VERSION        BANNER
------------------------------ -------------- ----------------------------------------------------------------
x86_64/Linux 2.4.xx            10.2.0.4.0     Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

CREATE TABLESPACE MSSM DATAFILE
  '/mnt/ramdisk/test/mssm01.dbf' SIZE 16M AUTOEXTEND OFF
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT MANUAL
FLASHBACK ON;

create table scott.deptx tablespace mssm as select * from scott.dept ;
create table scott.empy  tablespace mssm as select * from scott.emp  ;

SCOTT@test> update deptx set loc=loc ;
update deptx set loc=loc
       *
ERROR at line 1:
ORA-00372: file 6 cannot be modified at this time
ORA-01110: data file 6: '/mnt/ramdisk/test/mssm01.dbf'
--無法修改表中資料.

SCOTT@test> delete from empy ;
delete from empy
            *
ERROR at line 1:
ORA-00372: file 6 cannot be modified at this time
ORA-01110: data file 6: '/mnt/ramdisk/test/mssm01.dbf'
--無法刪除表中資料.

SCOTT@test> drop table empy ;
Table dropped.

SCOTT@test> select * from empy ;
select * from empy
              *
ERROR at line 1:
ORA-00942: table or view does not exist

--可以發現表依舊可以drop掉.
SCOTT@test> column SEGMENT_NAME format a30
SCOTT@test> select segment_name,header_file,header_block from dba_segments where tablespace_name='MSSM';
SEGMENT_NAME                    HEADER_FILE HEADER_BLOCK
------------------------------ ------------ ------------
BIN$E0GFElhsudLgUKjAWWRv7w==$0            6           17
DEPTX                                     6            9

SCOTT@test> flashback table empy to before drop;
Flashback complete.

SCOTT@test> drop table empy purge ;
Table dropped.

SCOTT@test> select segment_name,header_file,header_block from dba_segments where tablespace_name='MSSM';
SEGMENT_NAME                    HEADER_FILE HEADER_BLOCK
------------------------------ ------------ ------------
DEPTX                                     6            9
6.17                                      6           17

--SEGMENT_NAME以HEADER_FILE.HEADER_BLOCK表示,如果設定讀寫後.才會清除.

SCOTT@test> ALTER TABLESPACE MSSM READ write;
Tablespace altered.

SCOTT@test> select segment_name,header_file,header_block from dba_segments where tablespace_name='MSSM';
SEGMENT_NAME                    HEADER_FILE HEADER_BLOCK
------------------------------ ------------ ------------
DEPTX                                     6            9


2.延遲塊清除測試:

SCOTT@test> select rowid,ora_rowscn,deptx.* from deptx ;
ROWID                ORA_ROWSCN       DEPTNO DNAME          LOC
------------------ ------------ ------------ -------------- -------------
AAAO1FAAGAAAAAKAAA  12688068469           10 ACCOUNTING     new york
AAAO1FAAGAAAAAKAAB  12688068469           20 RESEARCH       dallas
AAAO1FAAGAAAAAKAAC  12688068469           30 SALES          chicago
AAAO1FAAGAAAAAKAAD  12688068469           40 OPERATIONS     boston

SCOTT@test> @ &r/lookup_rowid AAAO1FAAGAAAAAKAAA
      OBJECT         FILE        BLOCK          ROW DBA                  TEXT
------------ ------------ ------------ ------------ -------------------- ----------------------------------------
       60741            6           10            0 6,10                 alter system dump datafile 6 block 10 ;

SCOTT@test> update deptx set loc=lower(loc) ;
4 rows updated.

--開啟另外會話執行.
SCOTT@test> alter system checkpoint ;
System altered.
SCOTT@test> alter system flush buffer_cache ;
System altered.
SYS@test> @ &r/bh 6 10
old  23:   dbarfil = &1 and
new  23:   dbarfil = 6 and
old  24:   dbablk = &2
new  24:   dbablk = 10
HLADDR           DBARFIL      DBABLK CLASS CLASS_TYPE         STATE               TCH   CR_SCN_BAS   CR_SCN_WRP   CR_UBA_FIL   CR_UBA_BLK   CR_UBA_SEQ BA               OBJECT_NAME
---------------- ------- ----------- ----- ------------------ ---------- ------------ ------------ ------------ ------------ ------------ ------------ ---------------- --------------------
000000007B6FDFD8       6          10     1 data block         free                  0            0            0            0            0            0 000000006DC1A000 DEPTX
000000007B6FDFD8       6          10     1 data block         free                  0            0            0            0            0            0 000000006E12E000 DEPTX
000000007B6FDFD8       6          10     1 data block         free                  0            0            0            0            0            0 000000006B074000 DEPTX
000000007B6FDFD8       6          10     1 data block         free                  0            0            0            0            0            0 000000006ACA8000 DEPTX
000000007B6FDFD8       6          10     1 data block         free                  0            0            0            0            0            0 000000006ACF4000 DEPTX


SCOTT@test> select current_scn from v$database ;
CURRENT_SCN
------------
12688068873

SCOTT@test> commit ;

Commit complete.

SCOTT@test> select current_scn from v$database ;
CURRENT_SCN
------------
12688068877

SCOTT@test> alter tablespace mssm read only ;
Tablespace altered.

SCOTT@test> alter system dump datafile 6 block 10 ;
System altered.

Block header dump:  0x0180000a
Object id on Block? Y
seg/obj: 0xed45  csc: 0x02.f44490bc  itc: 2  flg: O  typ: 1 - DATA
     fsl: 0  fnx: 0x0 ver: 0x01

Itl           Xid                  Uba         Flag  Lck        Scn/Fsc
0x01   0x000a.016.00001ff2  0x00800038.0ed0.0f  ----    4  fsc 0x0000.00000000 <=lck=4表示操作4條記錄,由於塊不在記憶體,scn/fsc沒有資訊.
0x02   0x000a.022.00001ff2  0x00800037.0ed0.0b  C---    0  scn 0x0002.f4448f75 <= 這個是前面的scn=12688068469.

data_block_dump,data header at 0x8420c5c
===============
tsiz: 0x1fa0
hsiz: 0x1a
pbl: 0x08420c5c
bdba: 0x0180000a
     76543210
flag=--------
ntab=1
nrow=4
frre=-1
fsbo=0x1a
fseo=0x1f44
avsp=0x1f2a
tosp=0x1f2a
0xe:pti[0]  nrow=4  offs=0
0x12:pri[0] offs=0x1f44
0x14:pri[1] offs=0x1f5e
0x16:pri[2] offs=0x1f74
0x18:pri[3] offs=0x1f88
block_row_dump:
tab 0, row 0, @0x1f44
tl: 26 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 0b
col  1: [10]  41 43 43 4f 55 4e 54 49 4e 47
col  2: [ 8]  6e 65 77 20 79 6f 72 6b
tab 0, row 1, @0x1f5e
tl: 22 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 15
col  1: [ 8]  52 45 53 45 41 52 43 48
col  2: [ 6]  64 61 6c 6c 61 73
tab 0, row 2, @0x1f74
tl: 20 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 1f
col  1: [ 5]  53 41 4c 45 53
col  2: [ 7]  63 68 69 63 61 67 6f
tab 0, row 3, @0x1f88
tl: 24 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 29
col  1: [10]  4f 50 45 52 41 54 49 4f 4e 53
col  2: [ 6]  62 6f 73 74 6f 6e
end_of_block_dump
End dump data blocks tsn: 7 file#: 6 minblk 10 maxblk 10

SCOTT@test> select rowid,ora_rowscn,deptx.* from deptx ;
ROWID                ORA_ROWSCN       DEPTNO DNAME          LOC
------------------ ------------ ------------ -------------- -------------
AAAO1FAAGAAAAAKAAA  12688068875           10 ACCOUNTING     new york
AAAO1FAAGAAAAAKAAB  12688068875           20 RESEARCH       dallas
AAAO1FAAGAAAAAKAAC  12688068875           30 SALES          chicago
AAAO1FAAGAAAAAKAAD  12688068875           40 OPERATIONS     boston

SCOTT@test> alter system checkpoint ;
System altered.

SCOTT@test> alter system dump datafile 6 block 10 ;
System altered.

Block header dump:  0x0180000a
Object id on Block? Y
seg/obj: 0xed45  csc: 0x02.f44490bc  itc: 2  flg: O  typ: 1 - DATA
     fsl: 0  fnx: 0x0 ver: 0x01

Itl           Xid                  Uba         Flag  Lck        Scn/Fsc
0x01   0x000a.016.00001ff2  0x00800038.0ed0.0f  ----    4  fsc 0x0000.00000000
0x02   0x000a.022.00001ff2  0x00800037.0ed0.0b  C---    0  scn 0x0002.f4448f75

data_block_dump,data header at 0x8420c5c
===============
tsiz: 0x1fa0
hsiz: 0x1a
pbl: 0x08420c5c
bdba: 0x0180000a
     76543210
flag=--------
ntab=1
nrow=4
frre=-1
fsbo=0x1a
fseo=0x1f44
avsp=0x1f2a
tosp=0x1f2a
0xe:pti[0]  nrow=4  offs=0
0x12:pri[0] offs=0x1f44
0x14:pri[1] offs=0x1f5e
0x16:pri[2] offs=0x1f74
0x18:pri[3] offs=0x1f88
block_row_dump:
tab 0, row 0, @0x1f44
tl: 26 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 0b
col  1: [10]  41 43 43 4f 55 4e 54 49 4e 47
col  2: [ 8]  6e 65 77 20 79 6f 72 6b
tab 0, row 1, @0x1f5e
tl: 22 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 15
col  1: [ 8]  52 45 53 45 41 52 43 48
col  2: [ 6]  64 61 6c 6c 61 73
tab 0, row 2, @0x1f74
tl: 20 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 1f
col  1: [ 5]  53 41 4c 45 53
col  2: [ 7]  63 68 69 63 61 67 6f
tab 0, row 3, @0x1f88
tl: 24 fb: --H-FL-- lb: 0x1  cc: 3
col  0: [ 2]  c1 29
col  1: [10]  4f 50 45 52 41 54 49 4f 4e 53
col  2: [ 6]  62 6f 73 74 6f 6e
end_of_block_dump
End dump data blocks tsn: 7 file#: 6 minblk 10 maxblk 10

--可以發現一個奇特的情況,塊沒有變化,scn是從undo段來的.這樣每次的邏輯讀都要讀undo段.

SYS@test> alter system checkpoint ;
System altered.

SYS@test> alter system flush buffer_cache ;
System altered.

SYS@test> @ &r/bh 6 10
old  23:   dbarfil = &1 and
new  23:   dbarfil = 6 and
old  24:   dbablk = &2
new  24:   dbablk = 10
HLADDR                DBARFIL       DBABLK        CLASS CLASS_TYPE         STATE               TCH   CR_SCN_BAS   CR_SCN_WRP   CR_UBA_FIL   CR_UBA_BLK   CR_UBA_SEQ BA               OBJECT_NAME
---------------- ------------ ------------ ------------ ------------------ ---------- ------------ ------------ ------------ ------------ ------------ ------------ ---------------- --------------------
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006D9AC000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006CF0C000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006CCA0000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006E01A000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006D25C000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006B074000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006ACA8000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006ACF4000 DEPTX
8 rows selected.

SYS@test> @ &r/bh 6 10
old  23:   dbarfil = &1 and
new  23:   dbarfil = 6 and
old  24:   dbablk = &2
new  24:   dbablk = 10
HLADDR                DBARFIL       DBABLK        CLASS CLASS_TYPE         STATE               TCH   CR_SCN_BAS   CR_SCN_WRP   CR_UBA_FIL   CR_UBA_BLK   CR_UBA_SEQ BA               OBJECT_NAME
---------------- ------------ ------------ ------------ ------------------ ---------- ------------ ------------ ------------ ------------ ------------ ------------ ---------------- --------------------
000000007B6FDFD8            6           10            1 data block         cr                    1   4294967295        65535            0            0            0 000000006EB40000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006D9AC000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006CF0C000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006CCA0000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006E01A000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006D25C000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006B074000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006ACA8000 DEPTX
000000007B6FDFD8            6           10            1 data block         free                  0            0            0            0            0            0 000000006ACF4000 DEPTX
9 rows selected.

--出現的STATE=CR塊. 而不是XCUR.

SYS@test> alter system flush buffer_cache ;
System altered.

SCOTT@test> @ &r/10046on 12
Session altered.

SCOTT@test> select rowid,ora_rowscn,deptx.* from deptx ;
ROWID                ORA_ROWSCN       DEPTNO DNAME          LOC
------------------ ------------ ------------ -------------- -------------
AAAO1FAAGAAAAAKAAA  12688068875           10 ACCOUNTING     new york
AAAO1FAAGAAAAAKAAB  12688068875           20 RESEARCH       dallas
AAAO1FAAGAAAAAKAAC  12688068875           30 SALES          chicago
AAAO1FAAGAAAAAKAAD  12688068875           40 OPERATIONS     boston

SCOTT@test> @ &r/10046off
Session altered.

=====================
PARSING IN CURSOR #9 len=43 dep=0 uid=57 oct=3 lid=57 tim=1395066357333567 hv=1599918056 ad='75289110'
select rowid,ora_rowscn,deptx.* from deptx
END OF STMT
PARSE #9:c=1000,e=740,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,tim=1395066357333561
BINDS #9:
EXEC #9:c=0,e=42,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=1395066357333671
WAIT #9: nam='SQL*Net message to client' ela= 1 driver id=1650815232 #bytes=1 p3=0 obj#=0 tim=1395066357333711
WAIT #9: nam='db file sequential read' ela= 9 file#=6 block#=9 blocks=1 obj#=60741 tim=1395066357333776
WAIT #9: nam='db file scattered read' ela= 32 file#=6 block#=10 blocks=7 obj#=60741 tim=1395066357333990
WAIT #9: nam='db file sequential read' ela= 8 file#=2 block#=153 blocks=1 obj#=0 tim=1395066357334040
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FETCH #9:c=0,e=350,p=9,cr=4,cu=0,mis=0,r=1,dep=0,og=1,tim=1395066357334092
WAIT #9: nam='SQL*Net message from client' ela= 387 driver id=1650815232 #bytes=1 p3=0 obj#=0 tim=1395066357334518
WAIT #9: nam='SQL*Net message to client' ela= 1 driver id=1650815232 #bytes=1 p3=0 obj#=0 tim=1395066357334573
FETCH #9:c=0,e=45,p=0,cr=1,cu=0,mis=0,r=3,dep=0,og=1,tim=1395066357334603
*** 2015-04-09 10:52:37.005
WAIT #9: nam='SQL*Net message from client' ela= 6928105 driver id=1650815232 #bytes=1 p3=0 obj#=0 tim=1395066364262753
STAT #9 id=1 cnt=4 pid=0 pos=1 obj=60741 op='TABLE ACCESS FULL DEPTX (cr=5 pr=9 pw=0 time=337 us)'
=====================

--注意看~,可以發現要讀file#=2 block#=153 blocks=1.而這個正是undo檔案.

SYS@test> column name format a35
SYS@test> show parameter undo_tablespace
NAME             TYPE     VALUE
---------------- -------- ---------
undo_tablespace  string   UNDOTBS1

SYS@test> SELECT name,file#, CHECKPOINT_CHANGE#, UNRECOVERABLE_CHANGE# ,LAST_CHANGE#, OFFLINE_CHANGE#, ONLINE_CHANGE#,status FROM v$datafile where file#=2;
NAME                                       FILE# CHECKPOINT_CHANGE# UNRECOVERABLE_CHANGE# LAST_CHANGE# OFFLINE_CHANGE# ONLINE_CHANGE# STATUS
----------------------------------- ------------ ------------------ --------------------- ------------ --------------- -------------- -------
/mnt/ramdisk/test/undotbs01.dbf                2        12688069220                     0                            0              0 ONLINE

--另外你可以看到一個奇怪的現象:ORA_ROWSCN會變(順著時間的推移,原來undo slot的資訊會被覆蓋).我關閉資料庫再開啟.

SCOTT@test> select rowid,ora_rowscn,deptx.* from deptx ;

ROWID                ORA_ROWSCN       DEPTNO DNAME          LOC
------------------ ------------ ------------ -------------- -------------
AAAO1FAAGAAAAAKAAA  12688069729           10 ACCOUNTING     new york
AAAO1FAAGAAAAAKAAB  12688069729           20 RESEARCH       dallas
AAAO1FAAGAAAAAKAAC  12688069729           30 SALES          chicago
AAAO1FAAGAAAAAKAAD  12688069729           40 OPERATIONS     boston


--再關閉資料庫再開啟.
SCOTT@test> select rowid,ora_rowscn,deptx.* from deptx ;

ROWID                ORA_ROWSCN       DEPTNO DNAME          LOC
------------------ ------------ ------------ -------------- -------------
AAAO1FAAGAAAAAKAAA  12688070201           10 ACCOUNTING     new york
AAAO1FAAGAAAAAKAAB  12688070201           20 RESEARCH       dallas
AAAO1FAAGAAAAAKAAC  12688070201           30 SALES          chicago
AAAO1FAAGAAAAAKAAD  12688070201           40 OPERATIONS     boston

SYS@test> @&r/bh 6 10

HLADDR                DBARFIL       DBABLK        CLASS CLASS_TYPE         STATE               TCH   CR_SCN_BAS   CR_SCN_WRP   CR_UBA_FIL   CR_UBA_BLK   CR_UBA_SEQ BA               OBJECT_NAME
---------------- ------------ ------------ ------------ ------------------ ---------- ------------ ------------ ------------ ------------ ------------ ------------ ---------------- --------------------
000000007B6FDFD8            6           10            1 data block         cr                    1   4294967295        65535            0            0            0 000000006C8FA000 DEPTX

總結:
1.對於只讀表空間資料檔案,出現延遲塊清除,在下次訪問塊時是透過undo構造塊,但是由於是隻讀,不會更改塊內資訊.
2.這樣每次訪問都會訪問undo段,如果大量出現,也許會影響效能.

--補充一些測試:
SCOTT@test> alter tablespace mssm read write  ;
Tablespace altered.

SCOTT@test> set autot traceonly
SCOTT@test> select rowid,ora_rowscn,deptx.* from deptx ;
Execution Plan
----------------------------------------------------------
Plan hash value: 428208148
---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |     4 |    80 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| DEPTX |     4 |    80 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          5  consistent gets
          8  physical reads
        116  redo size
        983  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          4  rows processed
--可以發現產生redo.

SCOTT@test> set autot off

SYS@test> alter system checkpoint ;
System altered.

SYS@test> alter system dump datafile 6 block 10;
System altered.

Block header dump:  0x0180000a
Object id on Block? Y
seg/obj: 0xed45  csc: 0x02.f44497e6  itc: 2  flg: O  typ: 1 - DATA
     fsl: 0  fnx: 0x0 ver: 0x01

Itl           Xid                  Uba         Flag  Lck        Scn/Fsc
0x01   0x000a.016.00001ff2  0x00800038.0ed0.0f  C-U-    0  scn 0x0002.f4449645 <= 已經發生塊清除
0x02   0x000a.022.00001ff2  0x00800037.0ed0.0b  C---    0  scn 0x0002.f4448f75

SCOTT@test> select power(2,33)+to_number('f4449645','xxxxxxxxxxxxxx') from dual;
POWER(2,33)+TO_NUMBER('F4449645','XXXXXXXXXXXXXX')
--------------------------------------------------
                                       12688070213

SCOTT@test> select rowid,ora_rowscn,deptx.* from deptx ;
ROWID                ORA_ROWSCN       DEPTNO DNAME          LOC
------------------ ------------ ------------ -------------- -------------
AAAO1FAAGAAAAAKAAA  12688070213           10 ACCOUNTING     new york
AAAO1FAAGAAAAAKAAB  12688070213           20 RESEARCH       dallas
AAAO1FAAGAAAAAKAAC  12688070213           30 SALES          chicago
AAAO1FAAGAAAAAKAAD  12688070213           40 OPERATIONS     boston

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

相關文章