重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3391|回复: 0
打印 上一主题 下一主题

[认证考试] OCP课程32:管理Ⅰ之UNDO

[复制链接]
跳转到指定楼层
楼主
发表于 2016-1-28 10:59:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

课程目标:

  • 监控和管理undo数据
  • undo和redo的不同
  • 配置undo保留时间
  • 保证undo保留时间
  • 使用undo顾问

1、undo数据

undo数据是:

  • 原始数据,没有修改数据的拷贝
  • 用于改变数据的每一个事务
  • 至少要保留到事务结束
  • 用于支持:
    • 回滚操作
    • 读一致性操作
    • 闪回查询,闪回事务和闪回表
    • 恢复失败的事务

当数据库中的一个进程更改数据时,Oracle将保存旧的数据(undo数据)。修改前对数据进行拷贝。undo数据可以让你回滚未提交的数据。undo支持读一致性和闪回查询。undo也可以“倒带”(闪回)事务和表。

读一致查询提供了数据一致的查询结果,读一致性查询要成功,原始数据必须要作为undo信息存在。如果原始数据不再可用,将收到一个“snapshot too old”的错误(ora-01555)。只要undo信息还在,Oracle数据库就可以重建数据以满足读一致的查询。

闪回版本查询可以查看数据在过去某个时间的版本,只要undo保存了尽可能长时间的undo数据,闪回查询就可以成功完成。Oracle闪回事务使用undo创建补偿事务,可以回退事务及其相关的事务。使用Oracle闪回表,可以恢复一个表到一个特定的时间点。

undo数据也用来恢复失败的事务。当用户决定提交或回滚事务时,用户会话结束异常(可能是由于网络错误或客户机上的故障),就发生了一次失败的事务。失败的事务也可能发生在实例崩溃或发出SHUTDOWN ABORT命令。

事务失败时,数据库回退由用户所做的所有更改,从而恢复原始数据。

undo信息至少要保留到事务结束:

  • 用户取消事务(事务回滚)。
  • 用户结束事务(事务提交)。
  • 用户执行DDL语句,如CREATE、DROP、RENAME或ALTER语句。如果当前事务中包含任何DML语句,数据库首先提交事务,然后执行并提交DDL。
  • 用户会话终止异常(事务回滚)。
  • 用户会话正常退出(交易提交)。

保留的撤销数据的量和保留的时间取决于数据库活动的数量和数据库的配置。


2、事务和undo数据

当一个事务开始时,被分配一个undo段。在整个事务的生命周期中,当数据改变时,复制原始数据到undo段。通过V$TRANSACTION动态性能视图可以查看将哪些事务分配给了哪个undo段。

undo段是由实例自动创建的特殊段,用于支持事务处理。与其他段一样,undo段也是由区构成。根据需要undo段自动增长和收缩。

在完成事务之前或者用完所有undo空间之前,事务根据需要会一直写undo段的区。如果区被写满,事务就会从段的下一个区获取空间。

例子:查看undo段

SQL> select owner,segment_name,bytes/1024/1024 mb from dba_segments where tablespace_name='UNDOTBS1';

OWNER      SEGMENT_NAME                           MB

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

SYS        _SYSSMU10_1197734989$               2.125

SYS        _SYSSMU9_1650507775$                2.125

SYS        _SYSSMU8_517538920$                 2.125

SYS        _SYSSMU7_2070203016$                2.125

SYS        _SYSSMU6_1263032392$                1.125

SYS        _SYSSMU5_898567397$                 2.125

SYS        _SYSSMU4_1254879796$                2.125

SYS        _SYSSMU3_1723003836$                2.125

SYS        _SYSSMU2_2996391332$                1.125

SYS        _SYSSMU1_3724004606$                2.125

10 rows selected.


3、存储undo信息

undo信息在undo段中,undo段只能在undo表空间中。(不能在undo表空间中创建其他段类型,比如表)

dbca自动创建一个小文件的undo表空间。也可以创建一个大文件undo表空间。在一个繁忙的OLTP环境中,有大量的短并发事务,可能会出现文件头争用,有多个数据文件的表空间可以解决这个潜在的问题。

虽然数据库可能有多个undo表空间,但只能指定一个作为当前的undo表空间。

Oracle自动创建undo段,所有者为SYS用户。由于undo段类似一个环形区域,每个段至少有2个区。默认最大区数量取决于数据库的块大小(8 kb大小的块有32765个区)。

undo表空间是永久的表空间,本地管理的表空间,自动区扩展,由数据库自动管理。

因为恢复失败的事务需要undo数据(比如实例崩溃),undo表空间只有在实例mount状态才能进行恢复。

例子:查看当前的undo表空间及大小

SQL> show parameter undo_tablespace

NAME                                 TYPE        VALUE

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

undo_tablespace                      string      UNDOTBS1

SQL> select tablespace_name,file_name,bytes/1024/1024 mb from dba_data_files where tablespace_name='UNDOTBS1';

TABLESPACE_NAME                FILE_NAME                                                  MB

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

UNDOTBS1                       +DATA/stone/datafile/undotbs1.258.893370691               100

例子:查看哪些会话使用了undo段及使用大小

SQL> select s.sid,s.serial#,s.sql_id,v.usn,segment_name,r.status, v.rssize/1024/1024 mb

  2  From dba_rollback_segs r, v$rollstat v,v$transaction t,v$session s

  3  Where r.segment_id = v.usn and v.usn=t.xidusn and t.addr=s.taddr

  4  order by segment_name;

       SID    SERIAL# SQL_ID               USN SEGMENT_NAME                   STATUS                   MB

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

        78       5253                        6 _SYSSMU6_1263032392$           ONLINE            1.1171875


4、undo数据和redo数据

undo数据和redo数据用于不同的地方。如果需要撤消更改就需要undo数据。如果需要重新进行更改,则需要redo数据。为保障数据不丢失,undo块更改也会写入重做日志。

事务提交后,LGWR将事务的变化写入磁盘重做日志文件。此外,重做日志文件通常是多路复用,磁盘上有多个重做日志文件副本。虽然被更改的数据可能尚未写入到数据文件,写入到重做日志文件就足够保证数据库的一致性了。

如果在进行提交之前停电,那么下次启动就会需要进行实例恢复,SMON进程读取redo数据重新进行更改,然后使用undo数据进行回滚,以保证数据的一致性。


5、管理undo

Oracle数据库提供了自动undo管理,可以为所有会话在专门的undo表空间中完全自动化的管理undo信息和表空间。系统自动为undo信息提供最佳的保留时间。更确切地说,自动扩展undo表空间的undo数据保留时间要比运行时间最长的查询稍长。固定大小的undo表空间,数据库动态地调整最佳的保留时间。

Oracle数据库11g默认(及以后的版本)使用自动undo管理。手动undo管理是与Oracle8i及早期版本向后兼容,需要更多的DBA介入。在手动undo管理模式下,undo空间通过回滚段管理(不通过undo表空间)。

Oracle强烈建议使用自动undo管理。


6、配置undo保留时间

undo_retention初始化参数指定undo数据至少要保留多少秒。为自动扩展undo表空间设置尽可能长的最小undo保留时间以满足可能发生的最长的闪回操作。对于自动扩展undo表空间,undo数据至少要保留该参数指定的时间,并根据查询对undo的需求自动进行调整。但这种自动调整保留时间可能不能满足某些闪回操作。

针对固定大小的undo表空间,系统根据undo表空间的大小和历史使用情况,自动调整最佳undo保存时间;如果没有启用保留保证,将会忽略undo_retention参数。因此,自动undo管理的undo_retention参数用于上图这三种情况,否则忽略该参数。

undo信息分为三类:

  • 未提交的undo信息(主动):支持正在运行的事务,回滚或者事务失败需要这些信息,不会被覆盖。
  • 已提交的undo信息(未过期):不再需要支持运行的事务,但仍需要满足undo保留时间间隔。也被称为“未过期”的undo信息。活动事务需要时,如果空间不足,会被覆盖。
  • 过期undo信息(过期):不再需要支持运行的事务。当活动事务请求undo空间的时候,将覆盖过期的undo信息。

7、undo保留保证

如果undo空间不足,为保证活动事务成功,那么默认会覆盖尚未过期的已提交事务的undo信息。

可以通过设置保留保证来改变这个默认行为。如果设置了保留保证,那么undo数据就必须保留设置的时间,即使其他的事务会因获取不到足够的undo空间而失败。

RETENTION GUARANTEE是一个表空间的属性而不是一个初始化参数。可以用SQL命令行语句修改。语法:

SQL> alter tablespace undotbs1 retention guarantee;

返回保证undo表空间到正常设置,使用下面的命令:

SQL> alter tablespace undotbs1 retention noguarantee;

保留保证仅适用于undo表空间。

例子:使用EM修改undo表空间保留保证属性


8、修改undo表空间到固定大小

将undo表空间设置为固定大小的原因有两个:支持闪回操作或者防止表空间增长过大。

如果决定设置undo表空间为固定大小,必须设置足够大才能避免以下错误:

  • DML的失败(因为没有足够的空间来撤消新事务)
  • “snapshot too old”错误(因为没有足够的撤销数据来保障读一致性)

Oracle建议将undo表空间设置为数据库正常,满负荷运行所需要的最小大小。自动收集的统计数据包括最长运行查询的持续时间和undo生成率。根据这些统计数据计算出的最小undo表空间的大小没有考虑闪回操作以及将来可能会运行的长时间查询。

可以使用undo顾问评估undo表空间的大小。


9、一般undo信息

在EM中,选择服务器>自动还原管理

有2页:一般信息和系统活动。在一般信息页面的顶部,可以看到这个实例undo表空间的undo保留设置。


10、使用undo顾问

“一般信息”页面的中间部分是undo顾问。对undo表空间的undo保留时间进行估计。


11、查看系统活动

页面顶部显示选定期间系统活动情况。

下面,有三个图:

(1)undo表空间的使用情况

(2)自动优化的undo保留时间

(3)undo生成率

例子:更换一个小的undo表空间,修改一张大表,然后再查询,模拟ora-01555错误

创建undo表空间

SQL> create undo tablespace undotbs2 datafile '+DATA' size 1m retention noguarantee;

Tablespace created.

指定该表空间为当前undo表空间

SQL> alter system set undo_tablespace=undotbs2 scope=memory;

System altered.

创建一张大表

SQL> conn hr/hr

Connected.

SQL> create table emp as select * from employees;

Table created.

SQL> insert into emp select * from emp;

107 rows created.

SQL> insert into emp select * from emp;

214 rows created.

SQL> insert into emp select * from emp;

428 rows created.

SQL> insert into emp select * from emp;

856 rows created.

SQL> insert into emp select * from emp;

1712 rows created.

SQL> insert into emp select * from emp;

3424 rows created.

SQL> insert into emp select * from emp;

6848 rows created.

SQL> insert into emp select * from emp;

insert into emp select * from emp

*

ERROR at line 1:

ORA-30036: unable to extend segment by 8 in undo tablespace 'UNDOTBS2'

SQL> commit;

Commit complete.

更新所有的行

SQL> declare

  2  begin

  3  for i in 1..10000 loop

  4  update emp set salary=i where employee_id=100;

  5  commit;

  6  end loop;

  7  end;

  8  /

在其他会话进行查询

SQL> select salary from emp;

    SALARY

----------

      3800

      3600

      2900

      2500

      4000

      3900

ERROR:

ORA-01555: snapshot too old: rollback segment number 17 with name

"_SYSSMU17_1622872040$" too small

9180 rows selected.

修改undo表空间

SQL> alter system set undo_tablespace=undotbs1;

System altered.

SQL> show parameter undo

NAME                                 TYPE        VALUE

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

undo_management                      string      AUTO

undo_retention                       integer     900

undo_tablespace                      string      UNDOTBS1


12、相关习题:

(1)In which situation may the UNDO_RETENTION parameter be ignored, even if it is set to a value?
A.  When the data file of the undo tablespace is autoextensible
B.  When there are more than one undo tablespace available in the database
C.  When the undo tablespace is of a fixed size and retention guarantee is not enabled
D.  When the undo tablespace is autoextensible and retention guarantee is not enabled

答案:C

(2)Your  database  instance  is  running  with  full  workload  after  database  creation.  You  have decided  to  use  a fixed-size  undo  tablespace.  You  want  to  use  the  undo  Advisor  to  estimate the capacity of the undo tablespace.  
Which  two  factors  must  you  consider  before  using  the  Undo  Advisor  to  estimate  the capacity of the undo tablespace? (Choose two.)
A.  The retention period to support flashback
B.  The expected length of the longest-running query
C.  The number of undo tablespaces in the database
D.  The size of the Flash Recovery Area for the database instance

答案:AB

(2)View the Exhibit and examine the undo tablespace attributes.  Your database instance  is experiencing  a  large  volume  of  transactions  from  non-DBA  users  in  the  last  one  hour.  The undo tablespace NDOTBS1 is full with transactions and no transaction was committed more than one hour ago. The database has two more undo tablespaces.
What happens to the new DML operations in this scenario?


A.  The DML commands will fail
B.  The undo data generated by the DML is stored in one of the two other undo tablespace
C.  The undo data generated by the DML will overwrite the extents that contain committed undo data
D.  The undo data generated by the DML is stored in the SYSTEM undo segment of the SYSTEM tablespace

答案:A

(3)Your  database  instance  is  configured  with  automatic  undo  management  and  the UNDO_RETENTION parameter is set to 900 seconds. You executed the following command to enable retention guarantee:
   SQL> ALTER TABLESPACE undotbs1 RETENTION GUARANTEE;
What affect would this command have on the database?
A.  The extents in the undo tablespace retain data until the next full database backup
B.  The extents containing committed undo in the undo tablespace are not overwritten for at least 15 minutes
C.  The  extents  containing  committed  data  in  the  undo  tablespace  are  not  overwritten  until  the  instance  is  shut down
D.  The extents containing committed undo in the undo tablepace are transferred to Flash Recovery Area before being overwritten

答案:B

(4)Which two statements are true regarding undo tablespaces? (Choose two.)
A.  The database can have more than one undo tablespace
B.  The UNDO_TABLESPACE parameter is valid in both automatic and manual undo management
C.  Undo segments automatically grow and shrink as needed, acting as circular storage buffer for their assigned transactions
D.  An  undo  tablespace  is  automatically  created  if  the  UNDO_TABLESPACE  parameter  is  not  set  and  the UNDO_MANAGEMENT parameter is set to AUTO during the database instance start up

答案:AC

(5)Which statement is true about the UNDO_RETENTION parameter when retention guarantee is not enabled?
A.  It is the time period after which the undo data becomes obsolete.
B.  It is the time period after which the committed undo data would be transferred to a temporary tablespace.
C.  It is  the minimum time  period  up  to  which the  committed  undo  data  would  be  retained  if free  undo  space  is available.
D.  It  is  the  time  period  after  which  the  undo  data  is  transferred  to  the  Flash  Recovery  Area  to  provide  read consistency.

答案:C

(6)Which three operations require undo data? (Choose three.)
A.  Committing a transaction
B.  Flashing back a transaction
C.  Recovering a failed transaction
D.  Running a read-consistent query
E.  Changing a tablespace status from READ ONLY to READ WRITE

答案:BCD

(7)Which two statements are true regarding transactions in an Oracle database? (Choose two.)
A.  Multiple transactions can use the same undo segment.
B.  A transaction is assigned an undo segment when it is started.
C.  More than one transaction cannot share the same extent in the undo tablespace.
D.  The  transactions  use  system  undo  segment  to  store undo  data  if  all  the  segments  in  the  undo  tablespace are used.

答案:AB

(8)View the Exhibit and examine the attributes of an undo tablespace. In an OLTP system, the user SCOTT has started a query on a large table in the peak transactional hour that performs bulk inserts. The query runs for more than 15 minutes and then SCOTT receives the following error:
   ORA-01555: snapshot too old
What could be the reason for this error?


A.  The query is unable to get a read-consistent image.
B.  There is not enough space in Flash Recovery Area.
C.  There is not enough free space in the flashback archive.
D.  The query is unable to place data blocks in undo tablespace.

答案:A


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-5-5 23:44 , Processed in 0.099588 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表