UNDO(二)

Managing Undo
  Beginning with Release 11g, for a default installation, Oracle Database automatically manages undo. There is typically no need for DBA intervention. However, if your installation uses Oracle Flashback operations, you may need to perform some undo management tasks to ensure the success of these operations.
In this chapter:
■ What Is Undo?
■ Introduction to Automatic Undo Management
■ Setting the Minimum Undo Retention Period
■ Sizing a Fixed-Size Undo Tablespace
■ Managing Undo Tablespaces
■ Migrating to Automatic Undo Management
■ Undo Space Data Dictionary Views
管理UNDO
从11g开始,自动管理undo数据库的默认安装选项。一般不需要DBA干涉。不过,如果你的安装选择了ORACLE闪回操作,你可能需要进行一些UNDO管理任务来确保这些操作的成功。

在这个章节:
■ 什么是UNDO?
■ 自动UNDO管理的介绍
■ 设置最小UNDO保留时间
■ 设置固定大小的UNDO表空间
■ 管理UNDO表空间
■ 转换为UNDO自动管理
■ UNDO空间数据字典视图

What Is Undo?
  Oracle Database creates and manages information that is used to roll back, or undo, changes to the database. Such information consists of records of the actions of transactions, primarily before they are committed. These records are collectively referred to as undo.
Undo records are used to:
■ Roll back transactions when a ROLLBACK statement is issued
■ Recover the database
■ Provide read consistency
■ Analyze data as of an earlier point in time by using Oracle Flashback Query
■ Recover from logical corruptions using Oracle Flashback features
  When a ROLLBACK statement is issued, undo records are used to undo changes that were made to the database by the uncommitted transaction. During database recovery, undo records are used to undo any uncommitted changes applied from the redo log to the data files. Undo records provide read consistency by maintaining the before image of the data for users who are accessing the data at the same time that another user is changing it.

什么是UNDO?
数据库创建和管理用来回滚或者撤消数据库的变更的信息。这些记录的信息使事务的活动保持一致,尤其是在他们提交之前。这些记录的集合就是UNDO。
UNDO记录被用于:
■ 当一个ROLLBACK语句发出时,回滚事务
■ 恢复数据库
■ 提供读一致性
■ 获取早前时间点的数据,就好像使用闪回查询
■ 通过使用ORACLE闪回特性,从逻辑中断中恢复

  当发出ROLLBACK语句,UNDO记录会被用于撤销那些未提交的事务所做的已经被应用到数据库的变更。在数据库恢复期间,UNDO记录被用于撤销任何未提交的从REDO中应用到数据文件中的变更。在其他用户修改数据的同时,UNDO记录通过管理数据的前镜像来为那些正在获取数据的用户提供读一致性。

Introduction to Automatic Undo Management
  This section introduces the concepts of Automatic Undo Management and discusses the following topics:
■ Overview of Automatic Undo Management
■ About the Undo Retention Period

自动undo管理的介绍
这一节介绍自动段管理的概念以及讨论以下几个主题:
■自动undo管理的概览
■关于undo的保留时间

Overview of Automatic Undo Management
  Oracle
provides a fully automated mechanism, referred to as automatic undo
management, for managing undo information and space. With automatic undo
management, the database manages undo segments in an undo tablespace.
Beginning with Release 11g, automatic undo management is the default
mode for a newly installed database. An auto-extending undo tablespace
named UNDOTBS1 is automatically created when you create the database
with Database Configuration Assistant (DBCA). An undo tablespace can
also be created explicitly.

自动undo管理的概览
oracle提供一个完善的自动机制来自动管理undo的信息和空间。使用undo自动管理,数据库在undo表空间中管理undo段。从11g开始,自动undo管理是新安装的数据库的默认模式。一个自动扩展的undo表空间UNDOTBS1会在你使用DBCA创建数据库的时候自动创建。一个undo表空间也可以显式地创建。

  When the instance starts, the database automatically selects the first available undo tablespace. If no undo tablespace is available, the instance starts without an undo tablespace and stores undo records in the SYSTEM tablespace. This is not recommended, and an alert message is written to the alert log file to warn that the system is running without an undo tablespace.
    If the database contains multiple undo tablespaces, you can optionally specify at startup that you want to use a specific undo tablespace. This is done by setting the UNDO_TABLESPACE initialization parameter, as shown in this example:
UNDO_TABLESPACE = undotbs_01

If the tablespace specified in the initialization parameter does not exist, the STARTUP command fails. The UNDO_TABLESPACE parameter can be used to assign a specific undo tablespace to an instance in an Oracle Real Application Clusters environment. The database can also run in manual undo management mode. In this mode, undo space is managed through rollback segments, and no undo tablespace is used.

  当一个实例启动的时候,数据库自动选择第一个可用的undo表空间。假如没有undo表空间可用,实例一样可以启动,但会把undo数据存储在SYSTEM表空间中。这是不被建议的,同时一个alert信息会被写入到alert日志中,警告系统正在以一种没有undo表空间的方式运行。
  假如数据库包含多个undo表空间,你可以在启动的时候通过设置参数UNDO_TABLESPACE来指定你要使用哪个undo表空间,如UNDO_TABLESPACE = undotbs_01。
  如果在初始化参数中指定的表空间不存在,那么启动命令会失败。参数UNDO_TABLESPACE可以被用来在RAC环境中给每个实例分配指定的UNDO表空间。数据库也可以运行在人工管理UNDO的模式。在这个模式下,UNDO空间通过回滚段来管理,并且没有使用UNDO表空间。

Note: Space management for rollback segments is complex. Oracle strongly recommends leaving the database in automatic undo management mode.
注意,通过回滚段来管理undo空间很复杂。ORACLE强烈建议使用自动UNDO管理模式。
The following is a summary of the initialization parameters for undo management:

Initialization Parameter Description
UNDO_MANAGEMENT If AUTO or null, enables automatic undo management. If MANUAL, sets manual undo management mode. The default is AUTO.
UNDO_TABLESPACE Optional, and valid only in automatic undo management mode. Specifies the name of an undo tablespace. Use only when the database has multiple undo tablespaces and you want to direct the database instance to use a particular undo
tablespace.

When automatic undo management is enabled, if the initialization parameter file contains parameters relating to manual undo management, they are ignored.

Note: Earlier releases of Oracle Database default to manual undo management mode. To change to automatic undo management, you must first create an undo tablespace and then change the UNDO_MANAGEMENT initialization parameter to AUTO. If your Oracle Database is release 9i or later and you want to change to automatic undo management, see Oracle Database Upgrade Guide for instructions.

  A null UNDO_MANAGEMENT initialization parameter defaults to automatic undo management mode in Release 11g and later, but defaults to manual undo management mode in earlier releases. You must therefore use caution when upgrading a previous release to Release 11g. Oracle Database Upgrade Guide describes the correct method of migrating to automatic undo management mode, including information on how to size the undo tablespace.

关于undo有两个初始化参数:

UNDO_MANAGEMENT, 如果设置为AUTO或者为空,则表示启用自动undo管理,如果是MANUAL,则表示启用人工undo管理。默认值为AUTO.

UNDO_TABLESPACE 用于指定当前实例要使用哪个undo表空间。只有数据库有多个undo表空间的时候需要指定。

当自动管理undo被启用,数据库会忽略初始化参数文件中包含和人工管理undo相关的参数。

注意:oracle早期版本默认使用人工管理方式。要改成自动管理模式,你必须先创建一个undo表空间,然后把undo_management设置为自动。如果你的数据库是9i或者之后的版本,可以看官档Oracle Database Upgrade Guide。

  在11g或者之后的版本中,undo_mangement的默认值是auto,但是在早先的版本则是manual。所以在将旧版本升级为11g的时候,必须注意undo的管理方式。Oracle Database Upgrade Guide中有提供正确的方式迁移到自动undo管理模式,包括如何设置undo表空间的大小。

About the Undo Retention Period
  After a transaction is committed, undo data is no longer needed for rollback or transaction recovery purposes. However, for consistent read purposes, long-running queries may require     this old undo information for producing older images of data blocks. Furthermore, the success of several Oracle Flashback features can also depend upon the availability of older undo information. For these reasons, it is desirable to retain the old undo information for as long as possible.
When automatic undo management is enabled, there is always a current undo retention period, which is the minimum amount of time that Oracle Database attempts to retain old undo information before overwriting it. Old (committed) undo information that is older than the current undo retention period is said to be expired and its space is available to be overwritten by new transactions. Old undo information with an age that is less than the current undo retention period is said to be unexpired and is retained for consistent read and Oracle Flashback operations.

  Oracle Database automatically tunes the undo retention period based on undo tablespace size and system activity. You can optionally specify a minimum undo retention period (in seconds) by setting the UNDO_RETENTION initialization parameter.
The exact impact this parameter on undo retention is as follows:
■ The UNDO_RETENTION parameter is ignored for a fixed size undo tablespace. The database always tunes the undo retention period for the best possible retention, based on system activity and undo tablespace size.

■ For an undo tablespace with the AUTOEXTEND option enabled, the database attempts to honor the minimum retention period specified by UNDO_RETENTION.
When space is low, instead of overwriting unexpired undo information, the tablespace auto-extends. If the MAXSIZE clause is specified for an auto-extending undo tablespace, when the maximum size is reached, the database may begin to overwrite unexpired undo information. The UNDOTBS1 tablespace that is automatically created by DBCA is auto-extending.

关于undo的保留时间
  在一个事务提交后,undo数据不再需要用于回滚或者事务恢复。但是,对于一致性读来说,一个长查询可能需要这些旧的undo信息来提供数据块的旧镜像。另外,一些数据库闪回特性也同样依赖于这些旧的undo信息。这些情况需要我们尽可能地长时间保留undo信息。
当使用的是自动管理模式的时候,仍然有一个当前undo保留时间,它是数据库保留旧undo数据(被覆盖前)的最小时间。已经超过当前undo保留时间的旧的(即已经提交的)undo信息会被标识为expired(过期)并且它的空间可以被新的事务覆盖。旧的undo信息(已提交)如果它还没有超过当前undo保留时间,那么它会被标识为unexpired(未过期),并且用于读一致性和oracle闪回操作。
  oracle数据库基于undo表空间的大小和系统的活动来自动优化undo保留时间。你可以通过设置undo_retention初始化参数来指定一个最小保留时间(秒)。
这个参数对undo保留时间的确切影响是:
■对于一个固定大小的undo表空间来说,UNDO_RETENTION参数会被忽略。数据库会基于UNDO表空间的大小和系统活动持续优化UNDO保留时间,以此来获得最优的保留时间。
■对于一个允许自动扩展的undo表空间来说,数据库会使用UNDO_RETENTION指定的最小保留时间。当空间不够的时候,表空间会自动扩展,而不是去覆盖UNEXPIRED的UNDO信息。如果UNDO表空间制定了最大的大小,当达到这个值的时候,数据库就可能开始覆盖未过期的UNDO信息(非活动但未过期)。DBCA自动创建的UNDOTBS1表空间是自动扩展的。

Automatic Tuning of Undo Retention
  Oracle Database automatically tunes the undo retention period based on how the undo tablespace is configured.
■ If the undo tablespace is configured with the AUTOEXTEND option, the database dynamically tunes the undo retention period to be somewhat longer than the longest-running active query on the system. However, this retention period may be insufficient to accommodate Oracle Flashback operations. Oracle Flashback operations resulting in snapshot too old errors are the indicator that you must intervene to ensure that sufficient undo data is retained to support these operations. To better accommodate Oracle Flashback features, you can either set the UNDO_RETENTION parameter to a value equal to the longest expected Oracle Flashback operation, or you can change the undo tablespace to fixed size.

■ If the undo tablespace is fixed size, the database dynamically tunes the undo retention period for the best possible retention for that tablespace size and the current system load. This best possible retention time is typically significantly greater than the duration of the longest-running active query.

  If you decide to change the undo tablespace to fixed-size, you must choose a tablespace size that is sufficiently large. If you choose an undo tablespace size that is too small, the following two errors could occur:

■ DML could fail because there is not enough space to accommodate undo for new transactions.

■ Long-running queries could fail with a snapshot too old error, which means that there was insufficient undo data for read consistency.

Note: Automatic tuning of undo retention is not supported for LOBs. This is because undo information for LOBs is stored in the segment itself and not in the undo tablespace. For LOBs, the database attempts to honor the minimum undo retention period specified by UNDO_RETENTION. However, if space becomes low, unexpired LOB undo information may be overwritten.

UNDO保留时间的自动优化
ORACLE数据库基于UNDO表空间的设置来自动优化UNDO保留时间
■如果UNDO表空间配置了自动扩展选项,数据库会动态把UNDO的保留时间优化到比系统中活动的跑了最久的查询稍微久一点。但是,这个保留时间可能不适应ORACLE的闪回操作。当oracle闪回操作报快照过旧的时候你就需要人为干涉来确保有效的undo数据被保留来支持这些操作。为了更好地适应oracle闪回特性,你也可以把undo_retention设置为你想要的最长闪回时间,或者你可以把undo表空间设置为固定大小。

■如果undo表空间是固定大小的,数据库会根据表空间大小和当前系统活动来动态优化undo保留时间以求获得最恰当的保留时间。这个最优保留时间一般会明显大于跑了最久的查询会话。
如果你决定把undo表空间改为固定大小,你必须配置足够大的大小。如果你配得太小,可能会导致以下错误:
■因为没有足够的空间来容纳新事务的UNDO数据导致DML语句失败
■因为没有有效的undo数据来提供读一致性导致需要跑很久的查询失败,报快照过旧。

注意:undo保留时间的自动优化不支持LOB数据。这是因为LOB数据的undo信息是保留在它自己的段上面,而不是保留在undo表空间中。对于LOB,数据库会尝试着保留UNDO_RETENTION指定的最小保留时间。不过,如果空间变得太少,未过期的LOB的UNDO信息可能会被覆盖。
 
Retention Guarantee
  To guarantee the success of long-running queries or Oracle Flashback operations, you can enable retention guarantee. If retention guarantee is enabled, the specified minimum undo retention is guaranteed; the database never overwrites unexpired undo data even if it means that transactions fail due to lack of space in the undo tablespace. If retention guarantee is not enabled, the database can overwrite unexpired undo when space is low, thus lowering the undo retention for the system. This option is disabled by default.
  You enable retention guarantee by specifying the RETENTION GUARANTEE clause for the undo tablespace when you create it with either the CREATE DATABASE or CREATE UNDO TABLESPACE statement. Or, you can later specify this clause in an ALTER TABLESPACE statement. You disable retention guarantee with the RETENTION NOGUARANTEE clause.
  You can use the DBA_TABLESPACES view to determine the retention guarantee setting for the undo tablespace. A column named RETENTION contains a value of GUARANTEE, NOGUARANTEE, or NOT APPLY, where NOT APPLY is used for tablespaces other than the undo tablespace.

保留时间的guarantee(即保证保留时间)
你可以通过启用保留时间的guarantee特性,来确保长查询会话以及oracle闪回操作的成功。如果启用了retention guarantee,那么将保证undo数据保留到满足指定的最小保留时间。
即便当空间不够的时候,数据库也从不覆盖未过期的undo数据,虽然事务会因为undo表空间的空间不够而失败。相反,如果数据库没有启动retention guarantee,那么当空间不足的时候,数据库会覆盖未过期的undo数据,因此会降低系统的undo保留时间。guarantee默认是关闭的。
你可以通过在create database 或者 create tablespace的时候指定retention guarantee语句来启用gurantee特性。或者在创建之后通过alter tablespace语句来启用它。RETENTION NOGUARANTEE则是关闭这个特性。
你可以使用dba_tablespace来查看undo表空间是否使用了retention guarantee特性,RETENTION列包含了三个值,GUARANTEE,NOGUARANTEE或者NOT APPLY。NOT APPLY表示非UNDO TABLESPACE.

Undo Retention Tuning and Alert Thresholds
  For a fixed-size undo tablespace, the database calculates the best possible retention based on database statistics and on the size of the undo tablespace. For optimal undo management, rather than tuning based on 100% of the tablespace size, the database tunes the undo retention period based on 85% of the tablespace size, or on the warning alert threshold percentage for space used, whichever is lower. (The warning alert threshold defaults to 85%, but can be changed.) Therefore, if you set the warning alert threshold of the undo tablespace below 85%, this may reduce the tuned size of the undo retention period.

undo保留时间优化以及告警阀值
对于一个固定大小的UNDO表空间来说,数据库基于数据库统计信息以及UNDO表空间的大小来计算处最恰当的保留时间。
对于理想的UNDO管理,不是基于表空间大小的100%的优化,数据库基于UNDO表空间大小的85%来优化,或者基于更低的警告阀值(告警阀值默认是85%,但可以被更改)。
所以,如果你设置UNDO表空间的告警阀值低于85%,它可能会减小UNDO的保留时间。

Tracking the Tuned Undo 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.

追踪UNDO保留时间的优化
你可以通过查询V$UNDOSTAT的TUNED_UNDORETENTION列(秒)来测定当前的保留时间。这个视图包含过去4天每隔10分钟收集一次统计信息的集合。(超过4天的,需要查询DBA_HIST_UNDOSTAT视图)。

select to_char(begin_time, 'DD-MON-RR HH24:MI') begin_time,
to_char(end_time, 'DD-MON-RR HH24:MI') end_time, tuned_undoretention
from v$undostat order by end_time;
BEGIN_TIME END_TIME TUNED_UNDORETENTION
--------------- --------------- -------------------
04-FEB-05 00:01 04-FEB-05 00:11 12100
...
07-FEB-05 23:21 07-FEB-05 23:31 86700
07-FEB-05 23:31 07-FEB-05 23:41 86700
07-FEB-05 23:41 07-FEB-05 23:51 86700
07-FEB-05 23:51 07-FEB-05 23:52 86700
576 rows selected.

Setting the Minimum Undo Retention Period
  You specify the minimum undo retention period (in seconds) by setting the UNDO_RETENTION initialization parameter. As described in "About the Undo Retention Period" on page 16-3, the current undo retention period may be automatically tuned to be greater than UNDO_RETENTION, or, unless retention guarantee is enabled, less than UNDO_RETENTION if space in the undo tablespace is low.
WARNING: Enabling retention guarantee can cause multiple DML operations to fail. Use with caution.

设置undo最小保留时间
你可以通过设置UNDO_RETENTION初始化参数来指定最小undo保留时间。如同先前描述的关于UNDO保留时间的优化,当前的undo保留时间是可能因为自动优化导致大于UNDO_RETENTION的。或者,除非启用了retention guarantee,当undo表空间的空间不够的时候,这个保留时间也可能小于UNDO_RETENTION.
注意:启用retention guarantee可能导致多个DML操作失败。小心使用。

To set the minimum undo retention period:
■ Do one of the following:
– Set UNDO_RETENTION in the initialization parameter file.
UNDO_RETENTION = 1800
– Change UNDO_RETENTION at any time using the ALTER SYSTEM statement:
ALTER SYSTEM SET UNDO_RETENTION = 2400;
The effect of an UNDO_RETENTION parameter change is immediate, but it can only be
honored if the current undo tablespace has enough space.

设置最小undo保留时间

任选其一:

■在初始化参数文件中配置 : UNDO_RETENTION = 1800

■在任意时间使用:ALTER SYSTEM SET UNDO_RETENTION = 2400;

UNDO_RETENTION参数的修改是即时的,但它只有在当前的undo表空间空间充足的情况下才是被接受的。

上一篇:3 分钟轻松搭建 Ruby 项目自动化持续集成


下一篇:node.js+mongodb 爬虫