8.5 管理undo表空间
8.5.1 undo表空间的作用
1、使读写一致
2、可以回滚事物
3、事物恢复
4、闪回操作
8.5.2 undo表空间的初始化参数
1、undo_tablespace:指定要使用的undo表空间
2、undo_management:指定undo数据的管理模式,如果为auto,则为自动撤销管理模式,为manual,则为回滚段管理模式
3、undo_retention:控制undo数据的最大保留时间,默认为900s
SQL> col name for a20;
SQL> col type for a10;
SQL> col value for a20;
SQL> show parameter undo_tablespace;
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
undo_tablespace string UNDOTBS1
SQL> show parameter undo_management;
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
undo_management string AUTO
SQL> show parameter undo_retention;
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
undo_retention integer 900
8.5.3 撤销表空间的基本操作
1、创建undo表空间
需要注意一下两个方面:
a、undo表空间对应的数据文件大小通常由DML操作可能产生的最大数据量来确定,通常该数据文件的大小至少为1G;
b、undo表空间只用于存储撤销数据,不要再undo表空间内建立任何数据对象
SQL> col tablespace_name for a10;
SQL> col file_name for a50;
SQL> col bytes for 999,999,999;
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
TBS_BIG /ora/app/oracle/oradata/stone1/datafilebig.dbf 20,971,520
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf 10,485,760
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf 10,485,760
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf 10,485,760
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf 20,971,520
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
10 rows selected.
SQL> create undo tablespace undo_test1
2 datafile '/ora/app/oracle/oradata/stone1/undo_test1.dbf'
3 size 10m;
Tablespace created.
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
TBS_BIG /ora/app/oracle/oradata/stone1/datafilebig.dbf 20,971,520
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf 10,485,760
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf 10,485,760
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf 10,485,760
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf 20,971,520
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
UNDO_TEST1 /ora/app/oracle/oradata/stone1/undo_test1.dbf 10,485,760
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
11 rows selected.
2、修改undo表空间
SQL> alter tablespace undo_test1
2 add datafile '/ora/app/oracle/oradata/stone1/undo_test2.dbf'
3 size 10m;
Tablespace altered.
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
TBS_BIG /ora/app/oracle/oradata/stone1/datafilebig.dbf 20,971,520
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf 10,485,760
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf 10,485,760
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf 10,485,760
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf 20,971,520
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
UNDO_TEST1 /ora/app/oracle/oradata/stone1/undo_test2.dbf 10,485,760
UNDO_TEST1 /ora/app/oracle/oradata/stone1/undo_test1.dbf 10,485,760
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
12 rows selected.
3、切换undo表空间
oracle 11g默认的undo表空间是undotbs1
SQL> alter system set undo_tablespace=undo_test1;
System altered.
SQL> show parameter undo_tablespace;
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
undo_tablespace string UNDO_TEST1
4、删除undo表空间
SQL> alter system set undo_tablespace=undotbs1;
System altered.
SQL> drop tablespace undo_test1;
Tablespace dropped.
SQL> show parameter undo_tablespace;
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
undo_tablespace string UNDOTBS1
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
TBS_BIG /ora/app/oracle/oradata/stone1/datafilebig.dbf 20,971,520
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf 10,485,760
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf 10,485,760
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf 10,485,760
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf 20,971,520
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
10 rows selected.
5、查询undo表空间信息
(1)当前实例正在使用的undo表空间
查询初始化参数undo_tablespaces
SQL> show parameter undo_tablespace;
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
undo_tablespace string UNDOTBS1
(2)实例的所有undo表空间
查询数据字典dba_tablespaces
SQL> select tablespace_name as name from dba_tablespaces where contents='UNDO';
NAME
--------------------
UNDOTBS1
(3)undo表空间的统计信息
在数据库运行高峰期搜集undo表空间的统计信息,根据统计信息确定undo表空间的大小
查询动态性能视图v$undostat
SQL> select to_char(begin_time,'hh24:mi:ss') as BeginTime,
2 to_char(end_time,'hh24:mi:ss') as EndTime,
3 undoblks as numbers
4 from v$undostat
5 order by begin_time;
BEGINTIME ENDTIME NUMBERS
------------------------ ------------------------ ----------
20:19:49 20:29:49 3
20:29:49 20:39:49 55
20:39:49 20:49:49 1
20:49:49 20:59:16 1
(4)显示undo段统计信息
使用自动undo管理模式时,oracle会在undo表空间上自动建立10个undo段
查询动态性能视图v$rollname显示所有联机undo段的名称
查询动态性能视图v$rollstat显示undo段的统计信息
SQL> select rn.name,rs.xacts,rs.writes,rs.extents
2 from v$rollname rn,v$rollstat rs
3 where rn.usn=rs.usn;
NAME XACTS WRITES EXTENTS
-------------------- ---------- ---------- ----------
SYSTEM 0 46174 6
_SYSSMU1_3780397527$ 0 14538 4
_SYSSMU2_2232571081$ 0 39166 5
_SYSSMU3_2097677531$ 0 71240 7
_SYSSMU4_1152005954$ 0 18086 5
_SYSSMU5_1527469038$ 0 17184 6
_SYSSMU6_2443381498$ 0 20054 5
_SYSSMU7_3286610060$ 0 30472 5
_SYSSMU8_2012382730$ 0 17480 7
_SYSSMU9_1424341975$ 0 156986 6
_SYSSMU10_3550978943 0 17594 4
NAME XACTS WRITES EXTENTS
-------------------- ---------- ---------- ----------
$
11 rows selected.
name:标识undo段的名称
xacts:标识undo段所包含的活动事物个数
writers:标识在undo段写入的字节数
extents:标识undo段的区个数
(5)显示活动事物信息
使用动态性能视图v$session显示会话的详细信息
使用动态性能视图v$transaction显示事物的详细信息
SQL> select name,status from v$transaction;
no rows selected
(6)显示undo区信息
在数据字典dba_undo_extents中,用户可以查询undo表空间中所有区得详细信息,包括undo区的大小和状态等信息
SQL> desc dba_undo_extents;
Name Null? Type
----------------------------------------- -------- ----------------------------
OWNER CHAR(3)
SEGMENT_NAME NOT NULL VARCHAR2(30)
TABLESPACE_NAME NOT NULL VARCHAR2(30)
EXTENT_ID NUMBER
FILE_ID NOT NULL NUMBER
BLOCK_ID NUMBER
BYTES NUMBER
BLOCKS NUMBER
RELATIVE_FNO NUMBER
COMMIT_JTIME NUMBER
COMMIT_WTIME VARCHAR2(20)
STATUS VARCHAR2(9)
8.6 管理临时表空间
8.6.1 临时表空间概述
主要用于内存排序区不够而必须将数据写到磁盘的那个逻辑区域
下面几种操作经常会经常用的临时表空间:
select distinct不重复检索
union 联合查询
minus计算
analyze分析
连接两个没有索引的表
8.6.2 创建临时表空间
SQL> create temporary tablespace temp_01
2 tempfile '/ora/app/oracle/oradata/stone1/temp_01.tbf'
3 size 10m;
Tablespace created.
SQL> alter database default temporary tablespace temp_01;
Database altered.
8.6.3 查询临时表空间信息
临时表空间信息存放在dba_temp_files中
在v$tempfiles中可以查看临时表空间的使用情况
SQL> col file_name for a50;
SQL> col tablespace_name for a20;
SQL> show linesize;
linesize 80
SQL> set linesize 100;
SQL> show linesize;
linesize 100
SQL> select file_name,tablespace_name,bytes from dba_temp_files;
FILE_NAME TABLESPACE_NAME BYTES
-------------------------------------------------- -------------------- ------------
/ora/app/oracle/oradata/stone1/temp01.dbf TEMP 30,408,704
/ora/app/oracle/oradata/stone1/temp_01.tbf TEMP_01 10,485,760
8.6.4 关于临时表空间组
作用:
a、避免因大量排序数据导致单一临时表空间不足
b、一个用户有多个会话时,可以使用组中的不同临时表空间
c、使并行的服务器在单节点上使用多个临时表空间
1、创建临时表空间组
SQL> col name for a10;
SQL> select tablespace_name as name,file_name,bytes from dba_temp_files;
NAME FILE_NAME BYTES
---------- -------------------------------------------------- ------------
TEMP /ora/app/oracle/oradata/stone1/temp01.dbf 30,408,704
TEMP_01 /ora/app/oracle/oradata/stone1/temp_01.tbf 10,485,760
SQL> create temporary tablespace tp1
2 tempfile '/ora/app/oracle/oradata/stone1/tp1.tpf'
3 size 10m
4 tablespace group group1;
Tablespace created.
SQL> create temporary tablespace tp2
2 tempfile '/ora/app/oracle/oradata/stone1/tp2.tpf'
3 size 10m
4 tablespace group group1;
Tablespace created.
SQL> select tablespace_name as name,file_name,bytes from dba_temp_files;
NAME FILE_NAME BYTES
---------- -------------------------------------------------- ------------
TEMP /ora/app/oracle/oradata/stone1/temp01.dbf 30,408,704
TEMP_01 /ora/app/oracle/oradata/stone1/temp_01.tbf 10,485,760
TP1 /ora/app/oracle/oradata/stone1/tp1.tpf 10,485,760
TP2 /ora/app/oracle/oradata/stone1/tp2.tpf 10,485,760
2、转移临时表空间到另一个组
SQL> create temporary tablespace tp3
2 tempfile '/ora/app/oracle/oradata/stone1/tp3.tpf'
3 size 10m
4 tablespace group group3;
Tablespace created.
SQL> col group_name for a15;
SQL> select * from dba_tablespace_groups;
GROUP_NAME TABLESPACE_NAME
--------------- --------------------
GROUP1 TP1
GROUP1 TP2
GROUP3 TP3
SQL> alter tablespace tp1 tablespace group group3;
Tablespace altered.
SQL> select * from dba_tablespace_groups;
GROUP_NAME TABLESPACE_NAME
--------------- --------------------
GROUP3 TP1
GROUP1 TP2
GROUP3 TP3
3、把临时表空间组分配给指定的用户使用
SQL> alter user scott temporary tablespace group3;
User altered.
4、设置默认的临时表空间组
SQL> alter database stone1 default temporary tablespace group3;
Database altered.
SQL> alter database stone1 default temporary tablespace temp;
Database altered.
5、删除临时表空间组
SQL> drop tablespace tp1 including contents and datafiles;
Tablespace dropped.
SQL> select * from dba_tablespace_groups;
GROUP_NAME TABLESPACE_NAME
--------------- --------------------
GROUP1 TP2
GROUP3 TP3
SQL> drop tablespace tp3 including contents and datafiles;
Tablespace dropped.
SQL> select * from dba_tablespace_groups;
GROUP_NAME TABLESPACE_NAME
--------------- --------------------
GROUP1 TP2