臨時表空間日常維護處理
Oracle臨時表空間主要用來做查詢和存放一些緩衝區資料。臨時表空間消耗的主要原因是需要對查詢的中間結果進行排序
監控臨時表空間:
COL TEMP_FILE FOR A60;
SELECT ROUND((F.BYTES_FREE + F.BYTES_USED)/1024/1024/1024, 2) AS "TOTAL(GB)",
ROUND(((F.BYTES_FREE + F.BYTES_USED) - NVL(P.BYTES_USED, 0))/1024/1024/1024,2) AS "FREE(GB)",
D.FILE_NAME AS "TEMP_FILE",
ROUND(NVL(P.BYTES_USED, 0)/1024/1024/1024, 2) AS "USED(GB)" ,
ROUND((F.BYTES_USED + F.BYTES_FREE)/1024/1024/1024, 2) AS "TOTAL(GB)",
ROUND(((F.BYTES_USED + F.BYTES_FREE) - NVL(P.BYTES_USED, 0))/1024/1024/1024, 2) AS "FREE(GB)" ,
ROUND(NVL(P.BYTES_USED, 0)/1024/1024/1024, 2) AS "USED(GB)"
FROM SYS.V_$TEMP_SPACE_HEADER F ,DBA_TEMP_FILES D ,SYS.V_$TEMP_EXTENT_POOL P
WHERE F.TABLESPACE_NAME(+) = D.TABLESPACE_NAME
AND F.FILE_ID(+) = D.FILE_ID
AND P.FILE_ID(+) = D.FILE_ID;
檢視臨時表空間對應的臨時檔案的使用情況:
SELECT TABLESPACE_NAME AS TABLESPACE_NAME ,
BYTES_USED/1024/1024/1024 AS TABLESAPCE_USED ,
BYTES_FREE/1024/1024/1024 AS TABLESAPCE_FREE
FROM V$TEMP_SPACE_HEADER
ORDER BY 1 DESC;
查詢消耗臨時表空間資源比較多的SQL語句
set line 300
col username for a10
col tablespace for a10
col sql_id for a13
col prev_sql_id for a13
select s.sid,
s.serial#,
s.username,
s.sql_id,
s.prev_sql_id,
u.tablespace,
u.contents,
u.segtype,
u.extents,
u.blocks,
round(((u.blocks * P.VALUE) / 1024 / 1024), 2) MB
from v$session s, v$sort_usage u, SYS.V_$SYSTEM_PARAMETER P
where s.saddr = u.session_addr
AND UPPER(P.NAME) = 'DB_BLOCK_SIZE'
order by MB DESC;
---執行以下命令釋放temp表空間(詳見文件:How to Release the Temp LOB Space and Avoid Hitting ORA-1652 (文件 ID 802897.1) )
declare
clb clob;
ch varchar2(32767);
k number;
begin
dbms_lob.createtemporary(clb,true,dbms_lob.call);
for i in 1..1500 loop
ch:=lpad('o',32767,'Y');
dbms_lob.writeappend(clb,length(ch),ch);
end loop;
k:=dbms_lob.getlength(clb);
dbms_lob.freetemporary(clb);
dbms_output.put_line('the clob length: '||k);
end;
/
切換臨時表空間
1:檢視舊臨時表空間資訊
SELECT * FROM V$TEMPFILE
SELECT USERNAME, TEMPORARY_TABLESPACE FROM DBA_USERS
2:建立中轉的臨時表空間
3:新增相應的資料檔案
4:切換臨時表空間。
ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TMP;
5:刪除舊的臨時表空間資料檔案
DROP TABLESPACE TEMP INCLUDING CONTENTS AND DATAFILES;
6:如果有必要,重新指定使用者臨時表空間為新建的臨時表空間
ALTER USER ODS TEMPORARY TABLESPACE TMP;
ALTER USER EDS TEMPORARY TABLESPACE TMP;
ALTER USER ETL TEMPORARY TABLESPACE TMP;
ALTER USER DM TEMPORARY TABLESPACE TMP;
收縮臨時表空間
排序等操作使用的臨時段,使用完成後會被標記為空閒,表示可以重用,佔用的空間不會立即釋放,有時候臨時表空間會變得非常大,此時可以通過收縮臨時表空間來釋放沒有使用的空間。收縮臨時表空間是ORACLE 11g新增的功能。
SQL> ALTER TABLESPACE TEMP SHRINK SPACE KEEP 8G;
SQL> ALTER TABLESPACE TEMP SHRINK TEMPFILE '/u01/app/oracle/oradata/GSP/temp02.dbf'