一 为了更清楚的看出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. 基于python中staticmethod和classmethod的区别(详解)

    例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object):   def foo(self,x):     print "executing foo ...

  2. DOS批处理中%cd%与%~dp0的区别详解

    转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解     Windows下批处理中%cd%和%~dp0都能用来表示当前 ...

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

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

  4. oracle中的exists 和 in 用法详解

    以前一直不知道exists和in的用法与效率,这次的项目中需要用到,所以自己研究了一下.下面是我举两个例子说明两者之间的效率问题. 前言概述: “exists”和“in”的效率问题,涉及到效率问题也就 ...

  5. C#中struct和class的区别详解

    本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...

  6. Java中的==和equals的区别详解

    1.基础知识 (1)String x = "hello"; (2)String x = new String ("hello"); 第1种方式的工作机制是,首先 ...

  7. C#中struct和class的区别详解 (转载)

    本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...

  8. 深入Oracle的left join中on和where的区别详解

    -- from http://blog.itpub.net/30175262/viewspace-1472060/ 今天遇到一个求某月所有天数的统计结果,如果某日的结果是0也需要显示出来,即: 日期 ...

  9. C#中struct与class的区别详解

    转自:http://blog.csdn.net/justlovepro/archive/2007/11/02/1863734.aspx 有这么几点不同: 1.struct 是值类型,class是对象类 ...

  10. Lua中ipairs和pairs的区别详解

    迭代器for遍历table时,ipairs和pairs的区别: 区别一:ipairs遇到nil会停止,pairs会输出nil值然后继续下去 区别二: , b = , x = , y = , " ...

随机推荐

  1. [转帖]Systemd 指令

    一.由来 历史上,Linux 的启动一直采用init进程. 下面的命令用来启动服务. $ sudo /etc/init.d/apache2 start # 或者 $ service apache2 s ...

  2. [转帖]Oracle 通过 Exadata 云基础设施 X9M 提供卓越的数据库性能和规模

    https://www.modb.pro/db/397202 32个节点的RAC 服务器 每个服务器 两个 64核心的AMD CPU 四个线程干管理 252个线程进行数据库处理 252*32=8064 ...

  3. MySQL数据库存储varchar时多大长度会出现行迁移?

    最近客户现场有人问过mysql数据库的一些参数配置的问题, 这边数据库需要将strict 严格模式关掉, 目的是为了保证数据库在插入字段时不会出现8126的长度限制错误问题. 但是一直很困惑, mys ...

  4. Android APP升级时解析程序包时出现问题

    一个新的测试机在自动下载升级安装更新版本APP时,报出"解析程序包时出现问题"错误.原因众说纷纭, 一番搜索,下面的回答比较全面: https://stackoverflow.co ...

  5. Flask的cookie、session

    目录 七.设置cookies 7.1 设置cookie的参数 7.2 查询cookie 八.flask的session 实现session的两种思路 8.1 设置session(使用版) 8.2 设置 ...

  6. C#使用命令行打开diskpart修改盘符

    参考链接: https://www.cnblogs.com/k98091518/p/6019296.html https://learn.microsoft.com/zh-cn/windows-ser ...

  7. ChatGPT 中,G、P、T 分别是什么意思?

    流行的技术名词按发音难度排序,ChatGPT 肯定排在前面. 到底它为什么叫做 ChatGPT 呢? 先说 GPT:Generative Pre-Training Transformer Genera ...

  8. FMEA:总监和架构师都在用的高可用架构分析方法

    FMEA:总监和架构师都在用的高可用架构分析方法 记得之前准备春晚项目的时候,团队成员在一起过架构,老板最常问的问题是"这个组件挂了怎么办?有什么影响?",我当时还在心里默默嘀咕: ...

  9. Go语言的100个错误使用场景(一)|代码和项目组织

    目录 前言 1. Go: Simple to learn but hard to master 1.1 Go 语言概述 1.2 简单不等于容易 1.3 使用 Go 的100个错误 2. Code an ...

  10. 基于新浪微博海量用户行为数据、博文数据数据分析:包括综合指数、移动指数、PC指数三个指数

    基于新浪微博海量用户行为数据.博文数据数据分析:包括综合指数.移动指数.PC指数三个指数 项目介绍 微指数是基于海量用户行为数据.博文数据,采用科学计算方法统计得出的反映不同事件领域发展状况的指数产品 ...