Oracle收缩临时表空间(二)

2014-11-24 17:54:50 · 作者: · 浏览: 1
7 234 234 230 0
GO_TEMP SYS.0.0 1 375 1 2,055 32,766 375 375 0
************** ------- --------- -------- -------- -------- -------- --------
sum 5 609 4,132 33,000 609 605 0


SQL> @temp_sort_users.sql -->获得当前排序的会话


1 1073,5166 GO_ADMIN oracle 2480 SQL*Plus oracle@SZD 375 GO_TEMP 1
B (TNS V1-
V3)
3、使用resize,缩小临时表空间,如不能缩小,转到下一步
SQL> SELECT 'alter database tempfile ''' || a.name || ''' resize ' || b.siz || 'M;' resize_command
2 FROM v$tempfile a
3 ,(SELECT ceil(tmsize.maxblk * bk.value / 1024 / 1024) siz
4 FROM (SELECT nvl(MAX(segblk#), 128) maxblk
5 FROM v$sort_usage) tmsize
6 ,(SELECT VALUE
7 FROM v$parameter
8 WHERE NAME = 'db_block_size') bk) b;


RESIZE_COMMAND
----------------------------------------------------------------------------------------
alter database tempfile '/u02/database/ORADB/temp/ORADB_tempORADB.dbf' resize 106M;
alter database tempfile '/u02/database/ORADB/temp/tempORADB.dbf' resize 106M;


-->实际上此时占用32GB的临时数据文件已经缩小
alter database tempfile '/u02/database/ORADB/temp/ORADB_tempORADB.dbf' resize 106M;


Database altered.


-->为便于演示,此时假定TEMP为过大的临时表空间且不能释放
-->下面调整表明已使用空间超出了分配的空间
SQL> alter database tempfile '/u02/database/ORADB/temp/tempORADB.dbf' resize 106M;
alter database tempfile '/u02/database/ORADB/temp/tempORADB.dbf' resize 106M
*
ERROR at line 1:
ORA-03297: file contains used data beyond requested RESIZE value


SQL> select count(*) from v$sort_usage where tablespace='TEMP'; -->当前有未释放的临时段


COUNT(*)
----------
4

/**************************************************/
/* Author: Robinson Cheng */
/* Blog: http://blog.csdn.net/robinson_0612 */
/* MSN: robinson_0612@hotmail.com */
/* QQ: 645746311 */
/**************************************************/

4、新建一个中转临时表空间
SQL> create temporary tablespace temp2 tempfile '/u02/database/ORADB/temp/ORADB_temp02.dbf'
2 size 10m autoextend on;

Tablespace created.


-->如果此时过大的临时表空间为缺省的临时表空间,则必须将缺省的临时表空间设置为新的临时表空间之后
SQL> select property_name,property_value from database_properties
2 where property_name like 'DEFAULT_TEMP_TABLESPACE';


PROPERTY_NAME PROPERTY_VALUE
------------------------------ --------------------
DEFAULT_TEMP_TABLESPACE TEMP

SQL> alter database default temporary tablespace temp2;


Database altered.


5、转移用户到中转临时表空间
-->过大临时表空间上的那些用户需要迁移到新建的临时表空间
-->查询dba_users视图查询哪些用户位于过大的临时表空间之上
-->并使用下面的命令将其切换到新的临时表空间
alter user temporary tablespace temp2;


6.等到过大临时表空间上的没有临时段被使用,即已经全部释放即可删除过大的临时表空间


SQL> show user; -->由于当前用户为scott,所以临时表空间未能释放
USER is "SCOTT"


SQL> conn / as sysdba -->切换到sysdba
Connected.


SQL> @temp_usage2 -->临时段已经被释放


TABLESPACE MB_TOTAL MB_USED MB_FREE
-------------------- ---------- ---------- ----------
GO_TEMP 106 0 106
TEMP 235 0 235


-->如果没有释放在可以kill session的情况下kill session.利用前面获得的sid,serial#来执行(前提是允许该情况发生).
alter system kill session '1064,9259'


7.删除过大的临时表空间


SQL> alter tablespace temp tempfile offline; -->先将其脱机


Tablespace altered.


SQL> drop tablespace temp including contents and datafiles; -->删除临时表空间及相应的文件


Tablespace dropped.


SQL> select s.name tb