【Oracle】管理还原数据(undo)

1. 查看undo相关参数

SYS@LGR> show parameter undo

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS1

1)undo_management:自动管理回滚段模式(AUM)
2)undo_retention:900秒(15分钟)
·OLTP:15分钟(建议值)
·DSS系统:1-2小时(建议值)
3)undo_tablespace:当前使用的undo表空间

2.切换表空间
创建一个新的undo表空间,表空间名称为UNDOTBS2,100M,切换数据库的undo表空间为UNDOTBS2

--查看当前的undo表空间的名称

SYS@LGR> show parameter undo_tablespace;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ---------
undo_tablespace                      string      UNDOTBS1
--创建一个新的undo表空间

SYS@LGR> create undo tablespace UNDOTBS2 datafile'/u01/app/oracle/oradata/lgr/undotbs02.dbf'size
  2  100M autoextend on next 10M;

Tablespace created.
--切换当前的undo表空间为UNDOTBS2

SYS@LGR> alter system set undo_tablespace=UNDOTBS2;

System altered.
--验证

SYS@LGR> show parameter undo_tablespace;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ---------
undo_tablespace                      string      UNDOTBS2
--删除已经无用的undo表空间UNDOTBS1

SYS@LGR> drop tablespace UNDOTBS1 including contents and datafiles;

Tablespace dropped.

注:创建undo表空间的时候,初始可以设置为自动扩展,当系统稳定的运行一段时间后,需要手工调整一下undo表空间,将自动取消,为的是避免某用户忽略了提交事务而无意识的占用大量空间。

3.设置undo数据保留期限及强制保留
1)更改undo表空间,保存2小时

--查看参数undo_retention的值,默认为15分钟(900秒)

SYS@LGR> show parameter undo_retention 

NAME                                 TYPE        VALUE
------------------------------------ ----------- ---------
undo_retention                       integer     900
--将参数undo_retention修改为7200秒,此参数为动态参数

SYS@LGR> alter system set undo_retention=7200;

System altered.
--验证,修改成功

SYS@LGR> show parameter undo_retention;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ---------
undo_retention                       integer     7200

2)查看面undo表空间的保留模式(默认为非强制保留)

SYS@LGR> select tablespace_name,retention from dba_tablespaces where contents='UNDO';

TABLESPACE_NAME                RETENTION
------------------------------ -----------
UNDOTBS2                       NOGUARANTEE

3)打开/关闭undo表空间的确保保留期(guarantee以后,就要求确保7200的数据必须在undo中)

SYS@LGR> alter tablespace UNDOTBS2 retention guarantee;

Tablespace altered.

SYS@LGR> select tablespace_name,retention from dba_tablespaces where contents='UNDO';

TABLESPACE_NAME                RETENTION
------------------------------ -----------
UNDOTBS2                       GUARANTEE

SYS@LGR> alter tablespace UNDOTBS2 retention noguarantee;

Tablespace altered.

SYS@LGR> select tablespace_name,retention from dba_tablespaces where contents='UNDO';

TABLESPACE_NAME                RETENTION
------------------------------ -----------
UNDOTBS2                       NOGUARANTEE
注:确保保留期的开启与关闭,只适合undo表空间 
尝试对非undo表空间设置保留期会产生一下错误:

SYS@LGR> alter tablespace system retention guarantee;
alter tablespace system retention guarantee
*
ERROR at line 1:
ORA-30044: 'Retention' can only specified for undo tablespace
  1. 读一致性
    1)读一致性
    当会话1将某字段1更新成5,那么,会话2此时查询的时候,显示的是1,因为他读的是undo的数据,这就是读一致性
    2)通过这个视图可以查看当前的事务用的是哪个回滚段
select * from v$transaction;

3) 一个实例对应一个回滚段表空间,一个实例中可以存在多个回滚段表空间,但活动的只有一个
4)undo中的数据类型:
· 未提交
· 已提交,未满足undo retention
· 过期,可以被覆盖

原文地址:https://www.cnblogs.com/NextAction/p/7366708.html