日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Oracle 中UNDO与REDO的差别具体解释

發布時間:2023/12/9 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Oracle 中UNDO与REDO的差别具体解释 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一 為了更清楚的看出2者差別,請看下表:


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UNDO ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? REDO
Record ofHow to undo a changeHow to reproduce a change
Used forRollback, Read-ConsistencyRolling forward DB Changes
Stored inUndo segmentsRedo log files
Protect Against ??Inconsistent reads in multiuser systems ??Data loss

簡單看來,UNDO主要記錄怎樣撤銷事務和保證讀一致性;REDO則是負責數據庫前滾(重做)。保護數據不丟失。

?二 以下我們來通過實例說明undo 和 redo的關系:

1 我們將證明下面事實:

- oracle 中redo包括undo;

-?checkpoint 會導致臟數據寫入datafile;

-?buffers 會被寫入當前的undo 表空間


2 操作步驟:

- 創建1個undo表空間:undotbs2 - 創建1個表空間:test_undo - 在表空間test_undo創建表:test_undo_tab (txt char(1000)) - 向表test_undo_tab插入2條記錄txt – teststring1, teststring2。運行手工checkpoint操作 - 手工日志切換、切換undo 表空間 - 更新teststring1為teststring_uncommitted而且不提交 - 新開一個session 更新?teststring2為teststring_uncommitted而且提交 - 檢查update前后的值都被記錄在當前redo log中 - 檢查undo 表空間不包括更新之前的值 - 進行手工checkpoint,這樣undo信息將被寫入磁盤 - 檢查undo 表空間包括更新前的值

3 詳細實現:

?- 查找當前undo表空間
SQL> show parameter undo_tablespaceNAME TYPE VALUE ------------------------------------ ----------- ------------------------------ undo_tablespace string UNDOTBS1
- 創建Undo表空間 undotbs2:
SQL> create undo tablespace undotbs2 datafile '/u01/app/oracle/undotbs2.dbf'2 size 100m;Tablespace created.
- 創建表空間 test_undo SQL> create tablespace test_undo datafile '/u01/app/oracle/test_undo.dbf'2 size 128k;Tablespace created.

- 創建測試表 test_undo_tab:
SQL> create table test_undo_tab(txt char(1000)) tablespace test_undo;Table created.SQL> insert into test_undo_tab values ('teststring1');1 row created.SQL> insert into test_undo_tab values ('teststring2');1 row created.SQL> commit;
- 運行手工檢查點。將以上改變寫入數據文件:
SQL> alter system checkpoint;System altered.
- 設置undotbs2為當前undo表空間:
SQL> alter system set undo_tablespace=undotbs2;System altered.SQL> show parameter undo_tablespace;NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ undo_tablespace string UNDOTBS2
- 進行日志切換使當前日志不包括字符串teststring
SQL> alter system switch logfile;System altered.

- 查找當前日志
SQL> col member for a30 SQL> select member, l.status from v$log l, v$logfile f2 where l.group# = f.group#3 and l.status = 'CURRENT';MEMBER STATUS ------------------------------ ---------------- /u01/app/oracle/oradata/orcl/r CURRENT edo02.log

- 更新測試表中一行而且不提交
SQL> update test_undo_tab set txt = 'teststring_uncommitted'2 where txt = 'teststring1';1 row updated.
- 新開一個session 更新另外一行而且提交
SQL> update test_undo_tab set txt = 'teststring_committed'where txt = 'teststring2';commit;

- 查看這時候的redo log應該包括redo 和 undo (提交的和未提交的數據信息)
[oracle@dylan ~]$ strings /u01/app/oracle/oradata/orcl/redo02.log | grep teststring teststring_uncommitted teststring1 teststring_committed teststring2 - 檢查當前數據文件應該是不包括更新后的數值(僅僅有更新前數據)由于還未觸發檢查點
[oracle@dylan ~]$ strings /u01/app/oracle/test_undo.dbf | grep teststring
teststring2 teststring1

- 此時觸發檢查點
SQL> alter system checkpoint;
- 再次檢查數據文件發現數據已為最新值(提交的和未提交的值)
[oracle@dylan ~$ strings /u01/app/oracle/test_undo.dbf|grep teststringteststring_committed , teststring_uncommitted


- 最后檢查Undotbs2表空間發現包括更新前的數值 [oracle@dylan ~]$ strings /u01/app/oracle/undotbs2.dbf | grep teststringteststring2 teststring1
- 清理創建的對象 SQL>drop tablespace test_undo including contents and datafiles;alter system set undo_tablespace=undotbs1;drop tablespace undotbs2 including contents and datafiles;


三 進一步探討:


Let’s see what will happen if undo is stored in redo logs only.

假設僅將undo信息存儲于redo logs會怎么樣?

A redo log can be reused once changes protected by it have been written to datafiles (and archivelogs if database is in archivelog mode).

It implies that if I make a change and do not commit it?
- Change is written to a redo log ?假設我改變的數據而沒提交。此時改變將記錄到redo log
- checkpoint takes place ?檢查點發生
- uncommitted change is written to datafile ?后未提交的數據寫入了數據文件
- I decide to rollback the change ?這時我打算回滾
- If redo log has not been overwritten ?假設redo log沒被覆蓋
. search entire redo log for the undo and then rollback ?那么搜素整個redo log進行回滾操作
else (redo log has been overwritten)
. undo information is not available for rollback. ? ?否則將無法回滾,undo信息已丟失!

One might argue that if somehow a redo log is not allowed to be overwritten until it contains active undo, we might be able to manage with undo stored in redo logs only. This solution is not feasible as
- size of redo logs will grow enormously large very soon as thet contain both undo and redo (a user might decide not to end a transaction for months)
- to rollback a change, enormous amount of data in redo logs (both redo and undo) will have to be searched leading to degraded performance
- there will be contention on redo logs as they are being used for both
. writing redo and undo
. reading to rollback a change?

有人或許會爭論:那就不同意redo log 覆蓋undo 信息直到包括新的undo,這樣redo log將變得異常大從而影響系統性能!

Hence, undo information has to be stored separately from redo and is used for rolling back uncommited transactions . The undo stored in undo buffers/undo tablespace is additionally used for
- read consistency ? 讀一致性
- flashback query ? ? ?閃回查詢
- flashback version query ? 閃回版本號查詢







Reference:?http://oracleinaction.com/undo-and-redo-in-oracle/ http://oraclenz.wordpress.com/2008/06/22/differences-between-undo-and-redo/









--------------------------------------- Dylan ? ?Presents.

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Oracle 中UNDO与REDO的差别具体解释的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。