Manageing Undo Data

Objectives

After completing this lesson, you should be able to do the following:

  • Describe the purpose of undo data
  • Implement Automatic Undo Management
  • Create and configure undo segments
  • Obtain undo segment information from the data dictionary

image

Undo segment

It stores the location of the data and the data as it existed before being modified.

The header of an undo segment contains a transaction table where information about the current transactions using the undo segment is stored.

一个undo segment中时存储“旧”的数据, 它存储这这些数据的地点和数据被修改以前的内容。

也是一个循环利用的方式,类似 redo log 一样。

一个事务职能使用一个 undo segment来存储

多个事务可以同时使用一个undo segment。

目的:3个

事务回滚(用户个人需求),

读数据的一致性,

事务的恢复(如果instance坏掉了,那么oracle server 需要将所有的umcommit的内容恢复也是在undo segment中,This rollback is part of transaction recovery)

When the Oracle server begins executing a SELECT statement, it determines that current system change number(SCN) and ensures that any changes not committed before this SCN are not processed by the statement. Consider the case where a long-running query is executed at a time when several changes are being made. if a row has changes that were not commited at the start of the query, the Oracle server constructs a read-consistent image of the row by retrieving the before image of the changes from the undo segment and applying the changes to a copy of the row in memory.

当你要 select 时,系统会分配一个SCN(SYSTEM CHANGE NUMBER),只能增加不能减少的一个NUMBER,每读一个数据块( 数据块本身也有SCN), 当自己的SCN 大于数据块的SCN,意味着,这个数据块没有被修改。反之,到 UNDO SEGMENT 里找到相应数据块,读取该信息。

插入一条记录, 如下图 , insert ...

commit 只是将 redo log buffer 中的数据写入数据库而已。

Type of Undo Segments

SYSTME: 是由 SySTEM tablespace 创建的。这个undo segment只能用于 SYSTEM 表空间的对象。database被创建时就要被创建.

NON-SYSTEM : 至少需要一个这样的undo segment,因为还有很多别的 tablespace。(Auto mode, Manual mode, )

Deferred Undo segments : Deferred undo segments may be created when a tablespace is brought offline, They are used to roll back transactions when the tablespace is brought back online. They are dropped automatically when they are no longer needed.

Because deferred undo segments are maintained by the Oracle server, no maintenance is required on your part

Automatic Undo Management :

你只需要指定一个 undo tablespace, 并给与这个 tablespace 足够的空间, 其余的 oracle server 会进行维护.

name : _SYSSMUn$   其中 n 表示数字, 例如 _SYSSMU1$, _SYSSMU2$

以上的name, 是回滚段的名字( undo segment's name ), 以前这些内容是由 dba 创建的, 现在是自动创建的.

Configuration : 在初始化参数文件中指定参数 UNDO_MANAGEMENT( 使用自动模式 AUTO ) , UNDO_TABLESPACE( 指定哪个事 undo tablespace ) 第1个参数不能动态修改,即 如果已经启动了数据库就不能修改,第2个参数可以动态修改,修改的口令: alter system set undo_tablespace = UNDOTBS; ( 这个是 tablespace的名字 )

创建 undo tablespace

  • 在创建数据库时创建,create database db01 ... UNDO TABLESPACE undo1 DATAFILE ‘undo1db01.dbf’ SIZE 20M AUTOEXTEND ON
  • 也可以稍后创建,create undo tablespace undo1 DATAFILE ‘undo1db01.dbf’SIZE 20M;

最少要创建一个 undo tablespace.

Automatic undo management requires an UNDO tablespace. More than one UNDO tablespace may exist in the database, but only one UNDO tablespace can be active.

修改 undo tablespace

alter Tablespace undotbs ( undo tablespace name )

ADD datafile ‘undotbs2.dbf’SIZE 30M

autoextend ON;

( ADD DATAFILE, RENAME, DATAFILE[ONLINE|OFFLINE], BEFIN BACKUP, END BACKUP )

Switch undo tablespace

Only one UNDO tablespace can be in assinged to an instance at a time

More than one UNDO tablespace may exist within an instance, but only one can be active

Use the ALTER SYSTEM command for dynamic switching between UNDO tablespaces

ALTER system set UNDO_TABLESPACE=UNDOTBS2;

删除 undo tablespace

Drop tablespace undotbs2;

没有被 instance 使用时,可以被删除。如果要删除一个正在使用的 undo tablespace,你可以先 switch一个别的 undo tablespace,然后再删除。

To determine whether any active transaction

   1:  SELECT a.name, b.status
   2:   
   3:  FROM   v$rollname a, v$rollstat b
   4:   
   5:  WHERE a.name IN ( select segment_name
   6:   
   7:                                    from dba_segments
   8:   
   9:                                    where tablespace_name = ‘UNDOTBS’
  10:   
  11:                                  )
  12:   
  13:  AND a.usn = b.usn;
  14:   

v$undostat : This view displays a histogram of statistical data to show how well the system is working. You can use this view to estimate the amount of undo space required for the current workload. The database uses this view to tune undo usage in the system.

UNDO tablespace大小

需要 3 方面的信息,决定 undo tablespace 大小

  • UR UNDO_RETENTION in seconds ( 过了这个时间才能被覆盖,所以如果时间越长,那么 undo tablespace应该越大,因为不能及时覆盖 )
  • UPS Number of undo data blocks generated per second
  • DBS Overhead varies based on extent and file size ( db_block_size )

UndoSpace = [ UR * ( UPS * DBS )] + (DBS * 24)

前两个信息,在参数文件中可以找到 , UNDO_RETENTION , DB_BLOCK_SIZE

undo tablespace 总大小有多大,DBA 要考虑,虽然是自动管理。

UNDO_RETENTION : 决定信息在 undo 表空间存储的时间

第三个信息,V$UNDOSTAT中可以查询。

   1:  SELECT ( UR * (UPS * DBS)) + (DBS * 24) as “Bytes”
   2:   
   3:  FROM ( SELECT value as UR
   4:   
   5:  FROM v$parameter
   6:   
   7:  WHERE name = ‘undo_retention’),
   8:   
   9:  ( SELECT ( SUM(undoblks) /SUM((( end_time – begin_time)* 86400)) asUPS
  10:   
  11:  FROM v$undostat ),
  12:   
  13:  ( SELECT value as DBS 
  14:   
  15:  FROM v$parameter
  16:   
  17:  WHERE name = ‘db_block_size’);

Undo 配额

防范大的 transaction 出现,比如你有一个大的 transaction , 1000000 数据,那么会占用大量的 undo tablespace, 如果还是很长时间不释放的话,那么,会出现问题,此时,需要 undo_pool 这个配额来限制。

UNDO_POOL : 这个配额很有用,如果小了,那么向后的所有的 transaction 都不可以进行了,默认的是没有限制的了。

Obtaining Undo Segments Information

DBA_ROLLBACK_SEGS

V$ROLLNAME
V$ROLLSTAT

V$UNDOSTAT

V$SESSION

V$TRANSACTION

总结一下

数据库建立以后, 要先建立一个我们自己使用的undo 表空间, 然后将我们自己创建的表空间设置为 active, 当然, 这个创建的表空间大小要估量好, 根据经验. 或者等数据库运行一段时间来通过查询的到大概的 size 大小.

并且强烈建议使用自动方式管理Undo 表空间.

show parameter UNDO

Name                            Type                      Vaule

------------------------------------------------------------

undo_management         String                    AUTO

undo_retention                integer                  900 ( 15分钟)

undo_tablespace              string                    UNDOTBS1 (当前正在使用的undo表空间)

只能有一个 undo 表空间被 active, (在 auto 管理模式下), 如果undo 空间不够了, oracle会自动分配空间, 默认的空间限额是unlimit.

undo 表空间是会被重用的, 只有当事务没结束, 或开了 retention guarantee 或在 undo_retention 时间内部能被重用. 在undo_retention规定的时间内, 数据都是有效的, 过期都会设为无效, 状态被gaiwei Expired, 如果空间已满, 新事务的数据会自动覆盖掉已经提交的事务, 即使在 undo_retention时间内, 除非指定 retention guarantee模式, 才能保证在 undo_retention内不被覆盖.

undo表空间满了的处理方法:

1) 增加undo 表空间的数据文件

2) 切换到其他的 undo 表空间

 

 

 

 

 

 

 

 

 

posted @ 2012-10-19 11:27  神之一招  阅读(684)  评论(0编辑  收藏  举报