Tablespace_回收Temp表空間

http://blog.csdn.net/java3344520/article/details/7250767 

 

前幾天重新建立大數據量表索引的時候temp表空間擴展了好大,一般的temp不會用太多,因此回收temp表空間

SQL>Alter tablespace TEMP coalesce;

ORA-03217:invalid option for alter of TEMPORARY TABLESPACE 

 

Cause: invalid option for alter of temporary tablespace was specified

Action: Specify one of the valid options: ADD TEMPFILE, TEMPFILE ONLINE, TEMPFILE OFFLINE

 

出錯原因:Oracle9i 以上本地管理的temp表空間是不能使用 alter database , alter tablespace 來更改的。因爲他們 在數據字典中沒有參考信息。 也不需要備份tempfile 文件 。 

There is no need to backup the temporary locally manged tablespaces because: 
1. Locally managed tempfiles are always set to NOLOGGING mode. So thus will have no undo. 
2. Extents are managed by bitmap in each datafile to keep track of free or used status of blocks in that datafile.
3. The data dictionary does not manage the tablespace. 
4. Rollback information is not generated because there is no update on the data dictionary 
5. Media recovery does not recognize tempfiles.


1)用戶缺省表空間查詢:

SQL>select username,temporary_tablespace from dba_users;

USERNAME                       TEMPORARY_TABLESPACE
------------------------------ ------------------------------
ISGIS                                  TEMP
SCOTT                               TEMP


 

2)表空間包含的數據文件:

SQL>select name from v$tempfile;

NAME
-----------------------------------------------
/opt/ora10g/oradata/gis/temp01.dbf


 

3)創建新表空間:

SQL>create temporary tablespace TEMP2 TEMPFILE '/opt/oracle/database/oradata/gis/temp02.dbf ' SIZE 512M REUSE AUTOEXTEND ON NEXT 50M MAXSIZE UNLIMITED;
 
Tablespace created

 

4)看哪些用戶在使用表間:

SQL>SELECT se.username,sid,serial#,sql_address,machine, program,tablespace,segtype,contents
  2  FROM v$session se,v$sort_usage su
  3  WHERE se.saddr=su.session_addr
  4  Order By machine ;

USERNAME      SID    SERIAL# SQL_ADDRESS MACHINE      PROGRAM       TABLESPACE       SEGTYPE   CONTENTS
------------------------------ ---------- ---------- ----------- ---------

5)修改系統默認表空間及確認:

SQL>alter database default temporary tablespace temp2;
Database altered

SQL>select username,temporary_tablespace from dba_users;
USERNAME                       TEMPORARY_TABLESPACE
------------------------------ ------------------------------
XMJL                                  TEMP2
MDDATA                           TEMP2

6)刪除舊錶空間:

 

 

查看臨時表空間temp空閒情況
select TABLESPACE_NAME,file_id,bytes_used/1024/1024,bytes_free/1024/1024 from v$TEMP_SPACE_HEADER;
temp使用0後 刪除temp表空間

 

SQL>drop tablespace temp including contents and datafiles;
 Tablespace dropped

其實到此步就應該釋放了臨時表空間佔用的存儲空間,爲了保持一致,可以刪掉這個過渡的表空間,建立和原來名稱一致的臨時表空間。

 

7)創建原來臨時表空間

 
SQL> create temporary tablespace TEMP TEMPFILE '/opt/oracle/database/oradata/gis/temp01.dbf' SIZE 500M REUSE AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED;
Tablespace created
 

8)修改系統默認表空間及確認:
SQL> alter database default temporary tablespace temp;
Database altered
 
SQL> select username,temporary_tablespace from dba_users;
USERNAME                       TEMPORARY_TABLESPACE
------------------------------ ------------------------------
XMJL                                 TEMP
MDDATA                           TEMP

9)刪除中轉的臨時表空間TEMP2

SQL>drop tablespace temp2 including contents and datafiles;
Tablespace dropped

10)確認臨時表空間屬性

SQL>select file_name,tablespace_name,bytes/1024/1024 MB,autoextensible from dba_temp_files;
FILE_NAME                                                                        TABLESPACE_NAME                        MB AUTOEXTENSIBLE
-------------------------------------------------------------------------------- ------------------------------ ---------- --------------
/opt/oracle/database/oradata/gis/temp01.dbf                                      TEMP                                  500 YES

 

 

 

+++++++++++++++++

對臨時表空間進行shrink(11g新增的功能)
--將temp表空間收縮爲20M
alter tablespace temp shrink space keep 20M; 
--自動將表空間的臨時文件縮小到最小可能的大小
ALTER TABLESPACE temp SHRINK TEMPFILE ’/u02/oracle/data/lmtemp02.dbf’; 

+++++++++++++++++

 

 

 

 

 

 temp表空間的使用情況,當temp表空間變得巨大的時候,根據session_addr可以得到session id,根據sqladdr和sqlhash可以得到正在執行的sql:
select se.username,
       se.sid,
       su.extents,
       su.blocks * to_number(rtrim(p.value)) as Space,
       tablespace,
       segtype,
       sql_text
  from v$sort_usage su, v$parameter p, v$session se, v$sql s
 where p.name = 'db_block_size'
   and su.session_addr = se.saddr
   and s.hash_value = su.sqlhash
   and s.address = su.sqladdr
 order by se.username, se.sid;
 
查看臨時表空間temp空閒情況
select TABLESPACE_NAME,file_id,bytes_used/1024/1024,bytes_free/1024/1024 from v$TEMP_SPACE_HEADER;
 
查看當前臨時表空間使用大小與正在佔用臨時表空間的sql語句
select sess.SID, segtype, blocks * 8 / 1000 "MB", sql_text
  from v$sort_usage sort, v$session sess, v$sql sql
 where sort.SESSION_ADDR = sess.SADDR
   and sql.ADDRESS = sess.SQL_ADDRESS
 order by blocks desc;

 

Queries To Monitor Temporary Tablespace Usage [ID 289894.1]

查看臨時表空間使用率

 select (s.tot_used_blocks/f.total_blocks)*100 as "percent used"

from (select sum(used_blocks) tot_used_blocks from v$sort_segment where tablespace_name='TEMP') s, (select sum(blocks) total_blocks from dba_temp_files where tablespace_name='TEMP') f;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章