【物化檢視】幾種物化檢視日誌分析

楊奇龍發表於2011-04-11
物化檢視日誌在建立時有多種選項:ROWID、PRIMARY KEY和OBJECT ID三種型別,同時還可以指定SEQUENCE或明確指定列名。
上面這些情況產生的物化檢視日誌的結構都不相同。
所有物化檢視都會包括的列:
SNAPTIME$$:用於表示重新整理時間。
DMLTYPE$$:用於表示DML操作型別,I表示INSERT,D表示DELETE,U表示UPDATE。
OLD_NEW$$:用於表示這個值是新值還是舊值。N(EW)表示新值,O(LD)表示舊值,U表示UPDATE操作。
CHANGE_VECTOR$$表示修改向量,用來表示被修改的是哪個或哪幾個欄位。
如果WITH後面跟了ROWID,則物化檢視日誌中會包含:
M_ROW$$:用來儲存發生變化的記錄的ROWID。
如果WITH後面跟了PRIMARY KEY,則物化檢視日誌中會包含主鍵列。
如果WITH後面跟了OBJECT ID,則物化檢視日誌中會包含:
SYS_NC_OID$:用來記錄每個變化物件的物件ID。
如果WITH後面跟了SEQUENCE,則物化檢視日子中會包含:
SEQUENCE$$:給每個操作一個SEQUENCE號,從而保證重新整理時按照順序進行重新整理。
如果WITH後面跟了一個或多個COLUMN名稱,則物化檢視日誌中會包含這些列。

yang@rac1>create table yang_rowid (id number,name varchar2(30),val number);
Table created.

yang@rac1>create materialized view log on yang_rowid with rowid ,sequence (name,val);
Materialized view log created.

yang@rac1>create table yang_pk (id number primary key ,name varchar2(30) ,val number);
Table created.

yang@rac1>create materialized view log on yang_pk with primary key;
Materialized view log created.

yang@rac1>create type yang_object as  object(id number ,name varchar2(30) ,val number);
  2  /
Type created.
yang@rac1>create table yang_oid of yang_object;
Table created.
yang@rac1>create materialized view log on yang_oid with object id;
Materialized view log created.

yang@rac1>desc mlog$_yang_rowid
 Name                 Type
 ------------------   ------------
 NAME                 VARCHAR2(30)
 VAL                  NUMBER
 M_ROW$$              VARCHAR2(255)
 SEQUENCE$$           NUMBER
 SNAPTIME$$           DATE
 DMLTYPE$$            VARCHAR2(1)
 OLD_NEW$$            VARCHAR2(1)
 CHANGE_VECTOR$$      RAW(255)
 XID$$                NUMBER

yang@rac1>desc mlog$_yang_pk
 Name                 Type
 ------------------- -------
 ID                   NUMBER
 SNAPTIME$$           DATE
 DMLTYPE$$            VARCHAR2(1)
 OLD_NEW$$            VARCHAR2(1)
 CHANGE_VECTOR$$      RAW(255)
 XID$$                NUMBER

yang@rac1>desc mlog$_yang_oid
 Name               Type
 ---------------  ---------
 SYS_NC_OID$        RAW(16)
 SNAPTIME$$         DATE
 DMLTYPE$$          VARCHAR2(1)
 OLD_NEW$$          VARCHAR2(1)
 CHANGE_VECTOR$$    RAW(255)
 XID$$              NUMBER
物件表的物化檢視日誌建立後包含系統物件標識列。
一、主鍵列、ROWID列、OBJECT ID列、SEQUENCE列和建立物化檢視時指明的列。
主鍵、ROWID或OBJECT ID用來唯一表示物化檢視日誌中的記錄。
SEQUENCE會根據操作發生的順序對物化檢視日誌中的記錄編號。
建立物化檢視時指明的列會在物化檢視日誌中進行記錄。
yang@rac1>select name,val,m_row$$,sequence$$,dmltype$$ from mlog$_yang_rowid;
NAME                                  VAL M_ROW$$                   SEQUENCE$$ D
------------------------------ ---------- ------------------------- ---------- -
s                                       1 AAAgNtAAIAACaJcAAA                 1 I
s                                       1 AAAgNtAAIAACaJcAAA                 2 U
lily                                    1 AAAgNtAAIAACaJcAAA                 4 D

yang@rac1>insert into yang_pk values (1,'ll',2);
1 row created.
yang@rac1>update yang_pk set name='lily' where id=1;
1 row updated.
yang@rac1>delete yang_pk;
1 row deleted.
yang@rac1>select id ,dmltype$$ from mlog$_yang_pk;
ID D
--- -
1 I --insert
1 U --update
1 D --delete
yang@rac1>select id ,dmltype$$,OLD_NEW$$ from mlog$_yang_pk;
ID D O
-- - -
1  I N --new
1  U U --update
1  D O --old
--時間列
當基本發生DML操作時,會記錄到物化檢視日誌中,這時指定的時間4000年1月1日0時0分0秒。如果物化檢視日誌供多個物化檢視使用,則一個物化檢視重新整理後會將它重新整理的記錄的時間更新為它重新整理的時間。
下面建立快速重新整理的兩個物化檢視來演示時間列的變化。
(只有建立快速重新整理的物化檢視才能使用物化檢視日誌,如果只建立一個物化檢視,則物化檢視重新整理完會將物化檢視日誌清除掉。)
yang@rac1>create materialized view mv_yang_rowid refresh fast on commit with rowid as
  2      select name, val from yang_rowid;
Materialized view created.

yang@rac1>insert into yang_rowid values (1,'lily',2);
1 row created.
yang@rac1>update yang_rowid set name='yang' where id=1;
2 rows updated.
yang@rac1>delete yang_rowid;
2 rows deleted.
yang@rac1>select snaptime$$ from mlog$_yang_rowid;
SNAPTIME$$
-----------------------------
Jan 01 4000 00:00:00
Jan 01 4000 00:00:00
Jan 01 4000 00:00:00
Jan 01 4000 00:00:00
Jan 01 4000 00:00:00

yang@rac1>commit;
Commit complete.
yang@rac1>select snaptime$$ from mlog$_yang_rowid;
no rows selected

yang@rac1>select * from mv_yang_rowid;
no rows selected
yang@rac1>create materialized view mv_yang_rowid2 refresh fast  with rowid as
  2   select name, val from yang_rowid;
Materialized view created.
yang@rac1>insert into yang_rowid values (1,'lily',2);
1 row created.
yang@rac1>update yang_rowid set name='yang' where id=1;
1 row updated.
yang@rac1>select snaptime$$ from mlog$_yang_rowid;
SNAPTIME$$
-----------------------------
Jan 01 4000 00:00:00
Jan 01 4000 00:00:00
--問題出現了。就是提交時日期沒有更新,仍然是初始值。
yang@rac1>commit;
Commit complete.

yang@rac1>select snaptime$$ from mlog$_yang_rowid;
SNAPTIME$$
-----------------------------
Jan 01 4000 00:00:00
Jan 01 4000 00:00:00
==
DMLTYPE$$ 的值分為: U ,I ,D 分別為更新,插入,刪除。
OLD_NEW$$ 的值分為:N(EW)表示新值,O(LD)表示舊值,U表示UPDATE操作
yang@rac1>insert into yang_rowid values (1,'lily',2);
1 row created.
yang@rac1>insert into yang_rowid values (2,'lily',1);
1 row created.
yang@rac1>insert into yang_rowid values (3,'lily',1);
1 row created.
yang@rac1>update yang_rowid set name='yang' where id=1;
1 row updated.
yang@rac1>delete yang_rowid where id=2;
1 row deleted.
yang@rac1>select name,val, m_row$$, dmltype$$, old_new$$ from mlog$_yang_rowid;
NAME     VAL M_ROW$$                  D O
-------- ---------- ----------------- - -
yang      2 AAAgNtAAIAACaJcAAA        D O
yang      2 AAAgNtAAIAACaJcAAB        D O
yang      2 AAAgNtAAIAACaJcAAC        D O
lily      2 AAAgNtAAIAACaJcAAD        D O
lily      2 AAAgNtAAIAACaJcAAE        I N
lily      1 AAAgNtAAIAACaJcAAF        I N
lily      1 AAAgNtAAIAACaJcAAG        I N
lily      2 AAAgNtAAIAACaJcAAE        U U
lily      1 AAAgNtAAIAACaJcAAF        D O

9 rows selected.
yang@rac1>create materialized view log on yang_rowid with rowid, sequence (name, val) including new values;
Materialized view log created.
yang@rac1>insert into yang_rowid values (1,'lily',1);
1 row created.
yang@rac1>insert into yang_rowid values (2,'lily',2);
1 row created.
yang@rac1>insert into yang_rowid values (3,'lily',3);
1 row created.
yang@rac1>update yang_rowid set name='yang' where id=1;
2 rows updated.
yang@rac1>delete yang_rowid where id=2;
1 row deleted.
yang@rac1>select name,val, m_row$$, dmltype$$, old_new$$,change_vector$$ from mlog$_yang_rowid;
NAME                                  VAL M_ROW$$                   D O CHANGE_VECTOR$$
------------------------------ ---------- ------------------------- - - --------------
lily                                    1 AAAgNtAAIAACaJcAAK        I N FE
lily                                    2 AAAgNtAAIAACaJcAAA        I N FE
lily                                    3 AAAgNtAAIAACaJcAAB        I N FE
yang                                    1 AAAgNtAAIAACaJcAAH        U U 00
yang                                    1 AAAgNtAAIAACaJcAAH        U N 00
lily                                    1 AAAgNtAAIAACaJcAAK        U U 04
yang                                    1 AAAgNtAAIAACaJcAAK        U N 04
lily                                    2 AAAgNtAAIAACaJcAAA        D O 00

8 rows selected.
當物化檢視日誌是含有關鍵字 including new values時,每一條對應UPDATE操作的原記錄DMLTYPE$$和OLD_NEW$$都為U,
===更改向量Oracle是透過CHANGE_VECTOR$$列來記錄每條記錄發生變化的欄位的。
CHANGE_VECTOR$$列是RAW型別,其實Oracle採用的方式就是用每個BIT位去對映一個列。
插入操作顯示為:FE,
刪除顯示為:OO
更新操作則根據更新欄位的位置而顯示不同的值。比如說更新表yang_PK 的第二個欄位name,顯示00000100 為04
而當更新主鍵時 update操作分解為一個刪除操作(表示為OO)和插入操作(表示為FF)
yang@rac1>insert into yang_pk values(2,'yang',2);
1 row created.
yang@rac1>insert into yang_pk values(3,'yang',3);
1 row created.
yang@rac1>update yang_pk set name='lily' where id=3;
1 row updated.
yang@rac1>update yang_pk set id=4 where id=3;
1 row updated.
yang@rac1>select id,dmltype$$, old_new$$,change_vector$$ from mlog$_yang_pk;
        ID D O CHANGE_VECTOR$$
---------- - - ---------------
         1 I N FE
         2 I N FE
         3 I N FE
         3 U U 04
         3 D O 00
         4 I N FF
6 rows selected.
--對於object id 日誌:
yang@rac1>select object_id,dmltype$$, old_new$$,change_vector$$ from mlog$_yang_oid;
no rows selected

yang@rac1>insert into yang_oid values (1,'lily',1);
1 row created.
yang@rac1>insert into yang_oid values (2,'lily',2);
1 row created.
yang@rac1>insert into yang_oid values (3,'lily',3);
1 row created.
yang@rac1>update yang_oid set name='yang',val=5 where id=2;
1 row updated.
yang@rac1>delete yang_oid where id=3;
1 row deleted.
yang@rac1>select object_id,dmltype$$, old_new$$,change_vector$$ from mlog$_yang_oid;

OBJECT_ID                        D O CHANGE_VECTOR$$
-------------------------------- - - ----------------------------------------
A09F6E50AAD2008DE040010A6F9704FB I N FE
A09F6E50AAD3008DE040010A6F9704FB I N FE
A09F6E50AAD4008DE040010A6F9704FB I N FE
A09F6E50AAD3008DE040010A6F9704FB U U 30  --00110000 表示同時更新了第四列和第五列。為什麼不是第二和第三呢?下面給解釋
A09F6E50AAD4008DE040010A6F9704FB D O 00
yang@rac1>update yang_oid set name='yang',val=5 ,id=4 where id=2;
1 row updated.
yang@rac1>select object_id,dmltype$$, old_new$$,change_vector$$ from mlog$_yang_oid;

OBJECT_ID                        D O CHANGE_VECTOR$$
-------------------------------- - - ----------------------------------------
A09F6E50AAD2008DE040010A6F9704FB I N FE
A09F6E50AAD3008DE040010A6F9704FB I N FE
A09F6E50AAD4008DE040010A6F9704FB I N FE
A09F6E50AAD3008DE040010A6F9704FB U U 30
A09F6E50AAD4008DE040010A6F9704FB D O 00
A09F6E50AAD3008DE040010A6F9704FB U U 38
6 rows selected.
從下面的查詢可知由於有兩個隱含列,所以name,val 列分別為第4,5列了。即為
00110000
yang@rac1>select name, segcollength from sys.col$ where obj# =
  2         (select object_id from user_objects where object_name = 'YANG_OID');

NAME                           SEGCOLLENGTH
------------------------------ ------------
SYS_NC_OID$                       16
SYS_NC_ROWINFO$              1
ID                                       22
NAME                                 30
VAL                                     22

yang@rac1>update yang_pk set name='yang',val=5 where id=6;
1 row updated.
yang@rac1>select id,dmltype$$, old_new$$,change_vector$$ from mlog$_yang_pk;
        ID D O CHANGE_VECTOR$$
---------- - - ----------------------------------------
         1 I N FE
         2 I N FE
         3 I N FE
         3 U U 04
         3 D O 00
         4 I N FF
         2 D O 00
         6 I N FF
         6 U U 0C  ==00001100
9 rows selected.
可以看到,正如上面分析的,INSERT為FE,DELETE為00,對第一列的更新為02,第二列為04,第二列和第三列都更新為0C。更新了主鍵時update被分解。
yang@rac1>update yang_pk set name='yang',val=5 ,id=2 where id=6;
1 row updated.
yang@rac1>select id,dmltype$$, old_new$$,change_vector$$ from mlog$_yang_pk;
        ID D O CHANGE_VECTOR$$
---------- - - ----------------
         1 I N FE
         2 I N FE
         3 I N FE
         3 U U 04
         3 D O 00
         4 I N FF
         2 D O 00
         6 I N FF
         6 U U 0C --第2,3列被更新。
         6 D O 00 --update主鍵操作被分解為刪除,插入操作。
         2 I N FF
11 rows selected.

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

相關文章