以下總結(jié)了關(guān)于 Oracle 數(shù)據(jù)庫(kù)臨時(shí)表空間的相關(guān) SQL 語(yǔ)句:
Oracle 臨時(shí)表空間創(chuàng)建和添加數(shù)據(jù)文件:
--創(chuàng)建臨時(shí)表空間 tempdata
create temporary tablespace tempdata tempfile '/oradata/orcl/tempdata01.dbf' size 30g autoextend off;
--新增臨時(shí)表空間數(shù)據(jù)文件
alter tablespace tempdata add tempfile '/oradata/orcl/tempdata02.dbf' size 30g autoextend off;
--刪除臨時(shí)表空間數(shù)據(jù)文件
alter tablespace tempdata drop tempfile '/oradata/orcl/tempdata02.dbf' including datafiles;
--調(diào)整臨時(shí)表空間數(shù)據(jù)文件大小
alter database tempfile '/oradata/orcl/tempdata01.dbf' resize 2G;
--設(shè)置自動(dòng)擴(kuò)展
alter database tempfile '/oradata/orcl/tempdata01.dbf' autoextend on;
--切換默認(rèn)臨時(shí)表空間
alter database default temporary tablespace tempdata;
--刪除臨時(shí)表空間
drop tablespace temp including contents and datafiles cascade constraints;
--收縮臨時(shí)表空間
alter tablespace temp shrink space keep 8G;
alter tablespace temp shrink tempfile '/oradata/orcl/tempdata01.dbf';
查看當(dāng)前默認(rèn)臨時(shí)表空間:
SELECT PROPERTY_NAME, PROPERTY_VALUE FROM DATABASE_PROPERTIES WHERE PROPERTY_NAME='DEFAULT_TEMP_TABLESPACE';
查詢temp表空間使用率:
select df.tablespace_name "Tablespace",
df.totalspace "Total(MB)",
nvl(FS.UsedSpace, 0) "Used(MB)",
(df.totalspace - nvl(FS.UsedSpace, 0)) "Free(MB)",
round(100 * (1-( nvl(fs.UsedSpace, 0) / df.totalspace)), 2) "Pct. Free(%)"
FROM (SELECT tablespace_name, round(SUM(bytes) / 1048576) TotalSpace
FROM dba_TEMP_files
GROUP BY tablespace_name) df,
(SELECT tablespace_name, ROUND(SUM(bytes_used) / 1024 / 1024) UsedSpace
FROM gV$temp_extent_pool
GROUP BY tablespace_name) fs WHERE df.tablespace_name = fs.tablespace_name(+)
查看臨時(shí)表空間對(duì)應(yīng)的臨時(shí)文件的使用情況:
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;
查詢實(shí)時(shí)使用temp表空間的sql_id和sid:
set linesize 260 pagesize 1000
col machine for a40
col program for a40
SELECT se.username,
sid,
serial#,
se.sql_id
machine,
program,
tablespace,
segtype,
(su.BLOCKS*8/1024/1024) GB
FROM v$session se, v$sort_usage su
WHERE se.saddr = su.session_addr
order by su.BLOCKS desc;
/*需要注意的是這里查詢sql_id要用v$session視圖的sql_id,而不要用v$sort_usage視圖的sql_id,v$sort_usage視圖里面的sql_id是不準(zhǔn)確的*/
查詢歷史的temp表空間的使用的SQL_ID:
select a.SQL_ID,
a.SAMPLE_TIME,
a.program,
sum(trunc(a.TEMP_SPACE_ALLOCATED / 1024 / 1024)) MB
from v$active_session_history a
where TEMP_SPACE_ALLOCATED is not null
and sample_time between
to_date('&date1', 'yyyy-mm-dd hh24:mi:ss') and
to_date('&date2', 'yyyy-mm-dd hh24:mi:ss')
group by a.sql_id,a.SAMPLE_TIME,a.PROGRAM
order by 2 asc,4 desc;
本次分享到此結(jié)束啦~
如果覺(jué)得文章對(duì)你有幫助,點(diǎn)贊、收藏、關(guān)注、評(píng)論,一鍵四連支持,你的支持就是我創(chuàng)作最大的動(dòng)力。
?? 技術(shù)交流可以 關(guān)注公眾號(hào):Lucifer三思而后行 ??
本文摘自 :https://blog.51cto.com/l