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.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281681.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281681.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281681.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php實現文件留言,PHP文件操作及實例:留言板

一、文件操作函數1.創建文件:touch(./xxx.php);bool touch ( string $filename [, int $time time() [, int $atime ]] )2.復制文件:copy(./xxx.php,./yyy.php);3.移動或重命名:rename(./xxx.php,./yyy.php);4.刪除文件:unlink(.…

WPF-11 路由事件之一

什么是路由事件?我們從兩個維度來理解路由事件:功能的角度來看,路由事件是一種事件類型,不僅僅可以在事件源上處理事件響應,還可以在元素樹的多個偵聽器上處理事件響應(事件偵聽器是附加和調用事件處理程序的元素。事件…

個人總結的一個中高級Java開發工程師或架構師需要掌握的一些技能...

近三年,其實都是在做一個項目,項目是一個大型的多節點部署的項目,做了好幾個版本,中間用到了很多技術和框架, 也用了一些管理工具和敏捷實踐。我這里不是來說項目的,因為最近看了一些招聘信息,結…

Android 進程常駐(5)----開機廣播的簡單守護以及總結

這是一個輕量級的庫,配置幾行代碼。就能夠實如今android上實現進程常駐,也就是在系統強殺下,以及360獲取root權限下。clean master獲取root權限下都無法殺死進程 支持系統2.3到6.0 支持大部分設備,包含三星。華為。oppo&#xff0…

[k8s]metricbeat的kubernetes模塊kube-metric模塊

正確姿勢啟動metricbeat metricbeat.modules: - module: systemmetricsets:- cpu- filesystem- memory- network- processenabled: trueperiod: 10sprocesses: [.*]cpu_ticks: falseoutput.elasticsearch:hosts: ["http://192.168.x.x:9200"]setup.template.name: &q…

如何為 Task 添加超時功能

前言假設有如下代碼,功能是首先從緩存獲取數據,如果沒有命中緩存,則直接從數據庫獲取:var data await GetFromCache(); if (data is null) {data await GetFromDB(); }對于獲取緩存數據,我們需要限制一下GetFromCach…

php 隨機指定位數,php生成一個可選位數的隨機碼

echo coding(6);function coding($num){$str_arr array(‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,‘y‘,‘z‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘…

Animate與transform的使用

Animate是用css給前端加載動畫的效果&#xff1a; 網址&#xff1a;https://daneden.github.io/animate.css/ <!DOCTYPE html> <html lang"en"> <head><link rel"stylesheet" href"static/css/Animate.css"><meta ch…

angular中的cookies與cookieStore區別

設置cookie用put()方法: $cookies.put(key, value[, options]); $cookieStore.put(key, value); 例如設置一個cookie&#xff0c;名為“userName”&#xff0c;值為“yangmin”&#xff1a; //使用$cookies設置cookie $cookies.put(userName, yangmin); //使用$cookieStore設置…

ASP.NET Core 6框架揭秘實例演示[29]:搭建文件服務器

通過HTTP請求獲取的Web資源很多都來源于存儲在服務器磁盤上的靜態文件。對于ASP.NET應用來說&#xff0c;如果將靜態文件存儲到約定的目錄下&#xff0c;絕大部分文件類型都是可以通過Web的形式對外發布的。“Microsoft.AspNetCore.StaticFiles” 這個NuGet包中提供了三個用來處…

js 棧(進制轉換)

<!DOCTYPE html>Documentposted 2017-12-07 19:33 mysure 閱讀(...) 評論(...) 編輯 收藏 刷新評論刷新頁面返回頂部轉載于:https://www.cnblogs.com/ar13/p/8000718.html

流程展示 php,js實現動態的流程進度展示條

這次給大家帶來js實現動態的流程進度展示條&#xff0c;js實現動態流程進度展示條的注意事項有哪些&#xff0c;下面就是實戰案例&#xff0c;一起來看一下。一、設計思路分為以下幾步(僅供參考)【豎線線】這個采用ul的list標簽制作&#xff0c;保證了可隨時添加&#xff0c;以…

【我們一起寫框架】C#的AOP框架

原文:【我們一起寫框架】C#的AOP框架前言 AOP&#xff0c;大家都是聽過的&#xff0c;它是一種面向切面的設計模式。 不過AOP雖然是被稱為設計模式&#xff0c;但我們應該很少能看到AOP設計的框架。為什么呢&#xff1f; 因為&#xff0c;AOP單獨設計的框架幾乎是無法使用的。普…

新浪微博授權認證過程

為什么80%的碼農都做不了架構師&#xff1f;>>> 一、授權認證 1、請求用戶授權Token URL&#xff1a; https://api.weibo.com/oauth2/authorize HTTP請求方式:GET/POST 請求參數 必選 類型及范圍 說明 client_id true string 申請應用時分配的AppKey。 redire…

VisualStudio 使用 FastTunnel 輔助搭建遠程調試環境

有時候需要遠程調試一些用戶問題&#xff0c;期望能使用本機的 Visual Studio 開發環境&#xff0c;調試遠程的用戶的設備上的應用。這時會遇到的一個問題是如何讓本機的 Visual Studio 可以連接上遠程的用戶的設備&#xff0c;從而進行調試。本文將告訴大家如何采用 FastTunne…

深入理解null的原理

--null的原理 --oracle一直將null和空字符串’’<長度為0>同等對待<如’’ is null是true,’’null為false,如果聲明a varchar2:’’,那么a is null為true,a’’為false>--1.null的運算 --算術表達式和null 運算總為null,實際上所有的操作符除了||連接操作符外&…

阻止中文輸入法輸入拼音的時候觸發input事件

阻止中文輸入法輸入拼音的時候觸發input事件 前言 最近看element-ui源碼的時候看到el-input發現的。這個少見的事件。 compositionstart、compositionend事件&#xff08;MDN解釋) compositionstart事件觸發于一段文字的輸入之前&#xff08;類似于 keydown 事件&#xff0c;但…

Python1

python介紹python是一種解釋型的&#xff0c;面對對象的。帶有動態語義的高級程序設計語言python簡史1989年,Guido(龜叔)為ABC 語言寫的一個插件。因Monty Python的喜劇團體的原因,故給這個語言起名為python。linux也是1989年誕生的,1991年正式發布linux1.0內核;1990年, 發布py…

ncut算法matlab實現,ncut_multiscale_1_6 經典的圖像分割算法 的Matlab代碼。 238萬源代碼下載- www.pudn.com...

文件名稱: ncut_multiscale_1_6下載收藏√ [5 4 3 2 1 ]開發工具: matlab文件大小: 587 KB上傳時間: 2015-04-17下載次數: 4提 供 者: HH詳細說明&#xff1a;經典的圖像分割算法NCut的Matlab代碼。-Matlab code of classic image segmentation algorithm NCut .文件列表(…

使用.NET從零實現基于用戶角色的訪問權限控制

使用.NET從零實現基于用戶角色的訪問權限控制本文將介紹如何實現一個基于.NET RBAC 權限管理系統&#xff0c;如果您不想了解原理&#xff0c;可查看推送的另一篇文章關于Sang.AspNetCore.RoleBasedAuthorization[1] 庫是使用介紹&#xff0c;直接使用該庫即可。背景在設計系統…