一 为了更清楚的看出2者差别,请看下表:



                                             
UNDO                                                                   REDO

Record of How to undo a change How to reproduce a change
Used for Rollback, Read-Consistency Rolling forward DB Changes
Stored in Undo segments Redo 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_tablespace

NAME                                 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 f
2 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 teststring

teststring_committed                                                                                                               ,
teststring_uncommitted




- 最后检查Undotbs2表空间发现包括更新前的数值
[oracle@dylan ~]$ strings /u01/app/oracle/undotbs2.dbf | grep teststring

teststring2
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   闪回版本号查询






                                   http://oraclenz.wordpress.com/2008/06/22/differences-between-undo-and-redo/













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


Oracle 中UNDO与REDO的差别具体解释的更多相关文章

  1. Oracle中undo表空间的切换

    查看操作系统: SQL>  !cat /etc/redhat-releaseRed Hat Enterprise Linux Server release 7.4 (Maipo)查看数据库版本: ...

  2. Oracle中NVARCHAR2与VARCHAR2的差别

    NVARCHAR2在计算长度时和字符集相关的: 比如数据库是中文字符集时以长度10为例, 1.NVARCHAR2(10)是能够存进去10个汉字的.假设用来存英文也仅仅能存10个字符. 2.而VARCH ...

  3. Oracle性能优化之oracle中常见的执行计划及其简单解释

    一.访问表执行计划 1.table access full:全表扫描.它会访问表中的每一条记录(读取高水位线以内的每一个数据块). 2.table access by user rowid:输入源ro ...

  4. Java中Vector与ArrayList的差别具体解释

    首先看这两类都实现List接口,而List接口一共同拥有三个实现类.各自是ArrayList.Vector和LinkedList.List用于存放多个元素,可以维护元素的次序,而且同意元素的反复. 3 ...

  5. 【知识点整理】Oracle中NOLOGGING、APPEND、ARCHIVE和PARALLEL下,REDO、UNDO和执行速度的比较

    [知识点整理]Oracle中NOLOGGING.APPEND.ARCHIVE和PARALLEL下,REDO.UNDO和执行速度的比较 1  BLOG文档结构图 2  前言部分 2.1  导读和注意事项 ...

  6. Oracle Undo 和 Redo

    1. REDO(重做信息) Redo log file(重做日志文件),是数据库的事务日志. Oracle维护着两类重做日志文件:在线(online)重做日志文件和归档(archived)重做日志文件 ...

  7. 转:C#中Undo/Redo的一个简易实现

    一个比较常见的改进用户体验的方案是用Redo/Undo来取代确认对话框,由于这个功能比较常用,本文简单的给了一个在C#中通过Command模式实现Redo/Undo方案的例子,以供后续查询. clas ...

  8. Visio中的Undo和Redo

    1.Visio默认Undo和Redo操作是可用的,Appliacation中的UndoEnabled标志Undo和Redo操作是否可用. m_Visio.Window.Application.Undo ...

  9. 记一次ORACLE的UNDO表空间爆满分析过程

    这篇文章是记录一次ORACLE数据库UNDO表空间爆满的分析过程,主要整理.梳理了同事分析的思路.具体过程如下所示: 早上收到一数据库服务器的UNDO表空间的告警邮件,最早一封是7:55发出的(监控作 ...

随机推荐

  1. PIE加载自定义服务数据详细介绍

    这段时间我一直在研究如何用PIE加载在线地图服务,遇到了许多问题,多亏了技术员小姐姐的帮助,才让我能正确加载ArcGIS Online在线服务.天地图在线地图和谷歌在线地图.我是根据博客园PIE官方博 ...

  2. 小程序:前端防止用户重复提交&即时消息(IM)重复发送问题解决

    背景: 最近参与开发的小程序,涉及到即时消息(IM)发送的功能: 聊天界面如下,通过键盘上的[发送]按钮,触发消息发送功能 问题发现: 功能开发完毕,进入测试流程:测试工程师反馈说: 在Android ...

  3. POJ 1654 乱搞题?

    题意: 从一个点出发,8个方向,给出每一步的方向,求出走过的路径形成的多边形的面积. 思路: 先普及一下向量叉乘.. (摘自度娘) 也就是x1y2-x2y1. 那这不就好说了嘛. 一个经过原点的闭合多 ...

  4. C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. mac安装python3 pandas tushare

    1,升级pip python3 -m pip install --upgrade pip 2,安装依赖包 pip install --user numpy scipy jupyter pandas s ...

  6. Git Learning Part I - Install Git and configure it

    Why we need 'Git' GIt version control: 1. record the history about updating code and deleting code 2 ...

  7. 图片文字滚动插件jQuery Scrollbox

    图片文字滚动插件jQuery Scrollbox附件中提供了五种图片.文字滚动样式,只需调用jquery库和jQuery Scrollbox插件,然后再加一段简单的jquery代码即可使用,兼容性良好 ...

  8. (转载)Android常用的Dialog对话框用法

    Android常用的Dialog对话框用法 Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的 ...

  9. 三维重建:深度相机方案对比-KinectFusion的基本原理(尺度)

    算法原理请参考此文:  kinect fusion 3D重建基本算法  http://log.csdn.net/xiaohu50/article/details/51592503 三维重建为三维空间实 ...

  10. XML文件操作之dom4j

    能够操作xml的api还是挺多的,DOM也是可以的,不过在此记录下dom4j的使用,感觉确实挺方便的 所需jar包官网地址:http://www.dom4j.org/dom4j-1.6.1/ dom4 ...