3还原表空间(管理还原数据)
<p>[TOC]</p>
<h2>还原数据管理</h2>
<ul>
<li>进行DML操作前,将原来的数据复制到还原段上,实现改的同时可读。
<blockquote>
<p>还原数据的自动管理、创建和配置还原表空间、获取还原段的信息</p>
</blockquote></li>
</ul>
<h3>还原段的管理方法和用处</h3>
<ul>
<li>手动管理</li>
<li>自动管理</li>
</ul>
<h3>还原段的用处</h3>
<ul>
<li>事务回滚:rollback</li>
<li>事务恢复:未提交事务意外中断的恢复,受重做日志文件保护</li>
<li>保证数据读的一致性</li>
</ul>
<h2>还原段的类型</h2>
<ul>
<li>系统还原段</li>
<li>非系统还原段
<ul>
<li>自动管理还原段</li>
<li>手动管理还原段</li>
<li>私有还原段</li>
<li>仅有还原段</li>
</ul></li>
<li>延迟还原段</li>
</ul>
<h2>自动还原数据管理概念及配置</h2>
<ul>
<li>初始化参数文件中配置
<code>undo_management=auto (静态)</code>
<code>undo_tablespace=undotbs2 (动态)</code></li>
<li>至少创建一个还原表空间</li>
<li>查看数据数还原数据和管理模式
<code>select name,value from v$parameter where name like '%undo%';</code></li>
<li><code>undo_retention=900</code>这个参数值在后面学</li>
</ul>
<h2>还原表空间的创建和维护</h2>
<ul>
<li>通过 <code>create database</code>命令创建</li>
<li>通过 <code>create undo tablespace</code>命令创建</li>
<li>添加还原表空间数据文件、扩展还原表空间数据文件大小
<pre><code>select tablespace_name,status,contents from dba_tablespaces where tablespace_name like '%UNDO%';
select file_id,file_name,tablespace_name,bytes/1024/1024 MB from dba_data_files where tablespace_name like '%UNDO%';</code></pre></li>
</ul>
<h2>切换还原表空间</h2>
<ul>
<li><code>v$parameter.name.undo_tablespace</code> 指定系统当前还原表空间</li>
<li>一个数据库实例可以有多个还原表空间,但是只能有一个为活动还原表空间</li>
<li>切换命令 <code>alter system set undo_tablespace='表空间名';</code></li>
<li>切换实例:</li>
</ul>
<pre><code>--undo_tablespace默认还原表空间
select name,value from v$parameter where name like '%undo%';
--当前所有的还原表空间
select tablespace_name,status,contents from dba_tablespaces where contents='UNDO';
--还原表空间及其文件
select * from dba_data_files where tablespace_name like 'UNDO%';
--为两个实例各添加一个还原表空间undotest1、undotest2
CREATE UNDO TABLESPACE undotest1
RETENTION NOGUARANTEE
BLOCKSIZE 8K
FLASHBACK ON;
CREATE UNDO TABLESPACE undotest1 DATAFILE
'+DATA' SIZE 50M AUTOEXTEND ON NEXT 25M MAXSIZE UNLIMITED
RETENTION NOGUARANTEE
BLOCKSIZE 8K
FLASHBACK ON;
--分别在两个分支操作,要远程操作才成功
alter system set undo_tablespace=undotest2;
--试验完成改回来
alter system set undo_tablespace=UNDOTBS1;
alter system set undo_tablespace=UNDOTBS2;
--删除表空间
drop tablespace undotest1;
drop tablespace undotest2;</code></pre>
<h2>删除还原表空间</h2>
<p><code>drop tablespace jinlian_undo;</code></p>
<h2>自动还原数据管理的一些参数设置及应用实例</h2>
<ul>
<li>屏蔽自动模式使用手动操作还原表空间的错误
需要设置参数<code>undo_suppress_errors</code>,10g,11g,12c上已经默认为true了</li>
<li>undo_retention 参数作用,v$parameter.undo_retention=900,已经提交的在还原段中的数据保留15分钟。</li>
<li>retention属性,是否在undo-retention时间内禁止覆盖</li>
<li>有一个应该实例,在表空间管理文件中</li>
<li>实例是保证两小时内读的一致性,用两小时捞一份数据
<pre><code>--retention=GUARANTEE 禁止覆盖,retention=NOGUARANTEE 允许覆盖
--查询表属性,查询参数值
select tablespace_name,retention,contents from dba_tablespaces where contents='UNDO';
show parameter undo;
--修改表空间属性,修改参数值
alter tablespace undotbs1 retention GUARANTEE ;
alter system set undo_retention=7200 ;
--恢复表空间属性,恢复参数值
alter system set undo_retention=900;
alter tablespace undotbs1 retention NOGUARANTEE ;</code></pre></li>
</ul>
<h2>获得还原数据的信息</h2>
<ul>
<li>回滚数据的追踪:v$undostat等字典</li>
</ul>
<h2>临时还原模式</h2>
<ul>
<li>12c特有</li>
<li>临时还原数据的管理:temp_undo_enabled,不是很理解这一块
<code>select * from v$parameter where name like '%undo%';</code>
<code>show parameter temp_undo;</code></li>
<li>compatible至少要设置为12.1.0.0.0
<code>show parameter compatible;</code></li>
<li>物理数据库待机状态时是默认开启的,不影响参数文件</li>
<li>会话一级设置
<code>alter session set temp_undo_enabled=true;</code></li>
<li>数据库一级设置
<code>alter system seet temp_undo_enables=true;</code></li>
<li>对应地也有一个个类似v$undostat的动态数据字典
<code>select * from v$tempundostat ;</code></li>
</ul>