Oracle中flashback table功能為什麼需要開啟row movement功能

PiscesCanon發表於2017-07-10
前言:
對於row movement的說明,網上的一些資料:
1.在flashback中使用,當需要使用flashback table功能時,需要首先開啟row mvoement的選項,否則使用該功能會報錯。
2.Shrink Segment能幫助我們壓縮資料段、整理資料碎片、降低高水位,以提高效能、節省空間。它也同樣要求開啟ROW MOVEMENT。
3.一般用於分割槽表,將row movement設定為enable,有可能發生行的物理移動,行的rowdi會變化,某一行更新時,如果更新的是分割槽列,並且更新後的列值不屬於原來的這個分割槽,如果開啟了這個選項,就會把這行從這個分割槽中delete掉,並加到更新後所屬的分割槽。相當於一個隱式的觸發器,但不會觸發Insert/delete觸發器。如果沒有開啟這個選項,更新時就會報錯。
這裡只分析第一點。


作業系統版本:
  1. [oracle@oracle ~]$ uname -a
  2. Linux oracle.example.com 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
  3. [oracle@oracle ~]$ lsb_release -a
  4. LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
  5. Distributor ID: RedHatEnterpriseServer
  6. Description:    Red Hat Enterprise Linux Server release 6.5 (Santiago)
  7. Release:        6.5
  8. Codename:       Santiago

資料庫版本:
  1. SYS@proc> SELECT * FROM V$VERSION WHERE ROWNUM=1;

  2. BANNER
  3. --------------------------------------------------------------------------------
  4. Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

注意事項:
無法對sys使用者的表進行flashback table table_name to scn xxxxxx,如下:

  1. SYS@proc> create table t1 (id int);

  2. Table created.

  3. SYS@proc> insert into t1 values (1);

  4. 1 row created.

  5. SYS@proc> commit;

  6. Commit complete.
  7.  
  8. SYS@proc> select current_scn from v$database;

  9. CURRENT_SCN
  10. -----------
  11.     5828852

  12. SYS@proc> delete from t1;

  13. 1 row deleted.

  14. SYS@proc> commit;

  15. Commit complete.
  16.  
  17. SYS@proc> select * from t1;

  18. no rows selected

  19. SYS@proc> select * from t1 as of scn 5828852;

  20.         ID
  21. ----------
  22.          1

  23. SYS@proc> flashback table t1 to scn 5828852;
  24. flashback table t1 to scn 5828852
  25.                 *
  26. ERROR at line 1:
  27. ORA-08185: Flashback not supported for user SYS

構造環境:
  1. SCOTT@proc> drop table tt purge;

  2. Table dropped.

  3. SCOTT@proc> create table tt (id int,name varchar2(2000)) tablespace users;

  4. Table created.

  5. SCOTT@proc> insert into tt values(1,rpad('a',1800,'+'));

  6. 1 row created.

  7. SCOTT@proc> insert into tt values(2,rpad('b',1800,'+'));

  8. 1 row created.

  9. SCOTT@proc> insert into tt values(3,rpad('c',1800,'+'));

  10. 1 row created.

  11. SCOTT@proc> commit;

  12. Commit complete.

  13. SCOTT@proc> insert into tt values(4,rpad('d',1800,'+'));

  14. 1 row created.

  15. SCOTT@proc> commit;

  16. Commit complete.

  17. SCOTT@proc> select id,substr(name,1,1),dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block#,dbms_rowid.rowid_row_number(rowid) row# from tt;

  18.         ID SU       FILE#    BLOCK#       ROW#
  19. ---------- -- ---------- ---------- ----------
  20.          1 a           4        549          0
  21.          2 b           4        549          1
  22.          3 c           4        549          2
  23.          4 d           4        549          3

  24. SCOTT@proc> delete from tt where id=2;

  25. 1 row deleted.

  26. SCOTT@proc> commit;

  27. Commit complete.

  28. SCOTT@proc> select id,substr(name,1,1),dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block#,dbms_rowid.rowid_row_number(rowid) row# from tt;

  29.     ID     SU      FILE#     BLOCK#       ROW#
  30. ---------- -- ---------- ---------- ----------
  31.          1 a           4        549          0
  32.          3 c           4        549          2
  33.          4 d           4        549          3

  34. SCOTT@proc> insert into tt values(5,rpad('e',1800,'+'));

  35. 1 row created.

  36. SCOTT@proc> commit;

  37. Commit complete.

  38. SCOTT@proc> select id,substr(name,1,1),dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block#,dbms_rowid.rowid_row_number(rowid) row# from tt;

  39.         ID SU      FILE#     BLOCK#       ROW#
  40. ---------- -- ---------- ---------- ----------
  41.          1 a           4        549          0
  42.          5 e           4        549          1
  43.          3 c           4        549          2
  44.          4 d           4        549          3

實驗過程:
  1. SCOTT@proc> select id,substr(name,1,1),rowid,dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block#,dbms_rowid.rowid_row_number(rowid) row# from tt;

  2.         ID SU ROWID                   FILE#     BLOCK#       ROW#
  3. ---------- -- ------------------ ---------- ---------- ----------
  4.          1 a  AAAV78AAEAAAAIlAAA          4        549          0
  5.          5 e  AAAV78AAEAAAAIlAAB          4        549          1
  6.          3 c  AAAV78AAEAAAAIlAAC          4        549          2
  7.          4 d  AAAV78AAEAAAAIlAAD          4        549          3

  8. SCOTT@proc> select current_scn from v$database;

  9. CURRENT_SCN
  10. -----------
  11.     6013384

  12. SCOTT@proc> set time on
  13. 12:18:48 SCOTT@proc>
  14. 12:18:52 SCOTT@proc> delete from tt;

  15. 4 rows deleted.

  16. 12:19:03 SCOTT@proc> commit;

  17. Commit complete.

  18. 12:19:04 SCOTT@proc> select id from tt as of scn 6013384;

  19.         ID
  20. ----------
  21.          1
  22.          5
  23.          3
  24.          4

  25. 12:19:21 SCOTT@proc> flashback table tt to scn 6013384;
  26. flashback table tt to scn 6013384
  27.                 *
  28. ERROR at line 1:
  29. ORA-08189: cannot flashback the table because row movement is not enabled


  30. 12:19:56 SCOTT@proc> alter table tt enable row movement;

  31. Table altered.

  32. 12:20:06 SCOTT@proc> flashback table tt to scn 6013384;

  33. Flashback complete.

  34. 12:20:11 SCOTT@proc> select id,substr(name,1,1),rowid,dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block#,dbms_rowid.rowid_row_number(rowid) row# from tt;

  35.         ID SU ROWID                   FILE#     BLOCK#       ROW#
  36. ---------- -- ------------------ ---------- ---------- ----------
  37.          1 a  AAAV78AAEAAAAInAAA          4        551          0
  38.          5 e  AAAV78AAEAAAAInAAB          4        551          1
  39.          3 c  AAAV78AAEAAAAInAAC          4        551          2
  40.          4 d  AAAV78AAEAAAAInAAD          4        551          3

  41. 12:20:16 SCOTT@proc>
結論:可以看到,開啟了row movement之後,對錶TT進行flashback to scn操作之後,每個行的rowid均發生了變化。對於該功能,有可能發生物理行的移動,所以需要開啟row movement功能才能進行flashback操作。而在正常表中,表中行的rowid是不可以發生變化的。



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

相關文章