oracle Undo 管理

undo在oracle中是指维护用于回滚,撤销数据库改变信息的一种方式。这些信息是指未决事务的行为。
undo主要用于:
1、使用rollback语句回滚事务。
2、恢复数据库。
3、提供读一致性。
4、使用Oracle Flashback Query分析先前时间点数据。
5、使用Oracle Flashback 特性恢复逻辑损坏。


当系统发出rollback命令时,undo 信息通过记录的信息将数据库的改变恢复到commit之前的状态。在数据库恢复期间,undo信息被用来从redo log中撤销任何未提交到数据文件的事务。当一个用户在访问数据时,Undo记录通过维护访问数据的前镜像数据来保证当有其他用户改变相同数据时数据库的读一致性。

启用自动undo管理
1、设置UNDO_MANAGEMENT为auto。
2、设置UNDO_TABLESPACE。
3、设置UNDO_RETENTION 。
4、为undo表空间添加RETENTION GUARANTEE/NOGUARANTEE 选项。


UNDO_RETENTION 的行为:
1、当undo表空间不足时,oracle可能覆盖未过期的undo信息。
2、如果undo表空间开启AUTOEXTEND选项,oracle试图捍卫最小的undo保留期。当空间不足时,表空间会自动扩展,而不是覆盖未过期的undo信息。但是如果MAXSIZE 选项也同样被指定,达到最大值后,oracle才会开始覆盖未过期的undo信息。

当长时间运行的查询因为读一致性请求undo信息,而此时undo信息已被覆盖时,查询会失败并返回snapshot too old错误。

Retention Guarantee的行为:
为保证成功执行长时间运行的查询或oracle flashback操作,可以开启retention guarantee。如果 retention guarantee 被启用,指定的最小的undo retention将得到保证;尽管事务可能由于缺少undo空间而失败,oracle也不会覆盖未过期的undo信息。如果retention guarantee未开启,oracle会在undo空间不足时覆盖未过期的undo信息。retention guarantee默认未开启。
注,开启retention guarantee 可能导致DML操作失败,使用时需慎重。

Undo Retention调整
1、If the undo tablespace is fixed size, the database tunes the retention period for the best possible undo retention for that tablespace size and the current system load. This tuned retention period can be significantly greater than the specified minimum retention period.
2、If the undo tablespace is configured with the AUTOEXTEND option, the database tunes the undo retention period to be somewhat longer than the longest-running query on the system at that time. Again, this tuned retention period can be greater than the specified minimum retention period.

You can determine the current retention period by querying the TUNED_UNDORETENTION column of the V$UNDOSTAT view. This view contains one row for each 10-minute statistics collection interval over the last 4 days. (Beyond 4 days, the data is available in the DBA_HIST_UNDOSTAT view.) TUNED_UNDORETENTION is given in seconds.

SQL> select to_char(begin_time, 'DD-MON-RR HH24:MI') begin_time,
  2  to_char(end_time, 'DD-MON-RR HH24:MI') end_time, tuned_undoretention
  3  from v$undostat order by end_time;

BEGIN_TIME           END_TIME             TUNED_UNDORETENTION
-------------------- -------------------- -------------------
14-12月-11 14:01     14-12月-11 14:11                     900
14-12月-11 14:11     14-12月-11 14:21                     900
14-12月-11 14:21     14-12月-11 14:31                     900
14-12月-11 14:31     14-12月-11 14:41                     900
14-12月-11 14:41     14-12月-11 14:51                     900
14-12月-11 14:51     14-12月-11 15:01                     900
14-12月-11 15:01     14-12月-11 15:11                     900
14-12月-11 15:11     14-12月-11 15:21                     900
14-12月-11 15:21     14-12月-11 15:31                     900
14-12月-11 15:31     14-12月-11 15:41                     900
14-12月-11 15:41     14-12月-11 15:51                     900
...

设置Undo Retention Period
1、在初始化参数文件中设置:UNDO_RETENTION = 1800
2、使用 ALTER SYSTEM 语句:ALTER SYSTEM SET UNDO_RETENTION = 1800;

调整undo 表空间大小
使用自动扩展的undo表空间或使用undo advisor调整固定大小的表空间。


管理undo表空间
创建undo表空间
1、使用CREATE DATABASE创建undo表空间。
CREATE DATABASE rbdb1
     CONTROLFILE REUSE
     .
     .
     .
     UNDO TABLESPACE undotbs_01 DATAFILE '/u01/oracle/rbdb1/undo0101.dbf';
2、使用CREATE UNDO TABLESPACE创建undo表空间。
使用AUTOEXTEND 选项
CREATE UNDO TABLESPACE undotbs_02
  DATAFILE '+DGROUP1'   SIZE 2M REUSE AUTOEXTEND ON;
增加MAXSIZE 选项
CREATE UNDO TABLESPACE undotbs_02
  DATAFILE '+DGROUP1'   SIZE 2M REUSE AUTOEXTEND ON MAXSIZE 4M;
 
增加数据文件
SQL> ALTER TABLESPACE undotbs_02
  2   ADD DATAFILE '+DGROUP1' AUTOEXTEND ON  MAXSIZE UNLIMITED;
 
重命名数据文件
ALTER TABLESPACE undotbs_01
    RENAME DATAFILE '/u01/oracle/rbdb1/undo0102.dbf' TO '/u01/oracle/rbdb1/undo0101.dbf';
 
使数据文件online或者offline
ALTER TABLESPACE undotbs_01 online|offline;
 
开始或者结束一个联机备份
ALTER TABLESPACE undotbs_01 BEGIN|END BACKUP;
 
删除undo tablespace
Drop tablespace undotbs_01;
Drop undo表空间的时候必须是在未使用的情况下才能进行。如果undo表空间正在使用(例如事务失败,但是还没有恢复成功),那么drop表空间命令将失败。在drop表空间的时候可以使用including contents。

切换undo tablespace
切换undo表空间有两种方式:
1、 使用命令动态修改;
2、 修改初始化参数后重新启动数据库。

SQL> alter system set undo_tablespace=undotbs_02;

系统已更改。

SQL> show parameter undo

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS_02
SQL>
当切换命令完成后,所有的事务就会在新的回滚表空间内进行。
以下几种情况会导致切换命令失败:
1.  表空间不存在;
2. 表空间不是一个undo表空间;
3.  表空间已经被另一个实例使用。
注意:切换的操作不等待旧undo表空间的事务提交。如果旧undo表空间有事务未提交,那么旧的undo表空间进入pending offline状态,在这种模式下所有的事务能够继续进行,但是undo表空间不能被其他实例使用,也不能被删除,直到所有的事务提交后,undo表空间才进入offline模式。

相关视图

View Description
V$UNDOSTATContains statistics for monitoring and tuning undo space. Use this view to help estimate the amount of undo space required for the current workload. The database also uses this information to help tune undo usage in the system. This view is meaningful only in automatic undo management mode.
V$ROLLSTATFor automatic undo management mode, information reflects behavior of the undo segments in the undo tablespace
V$TRANSACTIONContains undo segment information
DBA_UNDO_EXTENTSShows the status and size of each extent in the undo tablespace.
DBA_HIST_UNDOSTATContains statistical snapshots of V$UNDOSTAT information. Please refer to Oracle Database 2 Day DBA for more information.

整理自网络并参考:
http://docs.oracle.com/cd/B19306_01/server.102/b14231/undo.htm


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值