Oracle临时表空间主要用来做查询和存放一些缓冲区数据。临时表空间消耗的主要原因是需要对查询的中间结果进行排序。
重启数据库可以释放临时表空间,如果不能重启实例,而一直保持问题sql语句的执行,temp表空间会一直增长。直到耗尽硬盘空间。
网上有人猜测在磁盘空间的分配上,oracle使用的是贪心算法,如果上次磁盘空间消耗达到1GB,那么临时表空间就是1GB。也就是说当前临时表空间文件的大小是历史上使用临时表空间最大的大小。
临时表空间的主要作用:
索引create或rebuild
Order by 或 group by
Distinct 操作
Union 或 intersect 或 minus
Sort-merge joins
analyze
查看临时表空间大小
查看临时表文件大小和已使用空间
select t1."Tablespace" "Tablespace",
t1."Total (G)" "Total (G)",
nvl(t2."Used (G)", 0) "Used(G)",
t1."Total (G)" - nvl(t2."Used (G)", 0) "Free (G)"
from
(
select tablespace_name "Tablespace", to_char((sum(bytes/1024/1024/1024)),'99,999,990.900') "Total (G)"
from dba_temp_files
groupby tablespace_name
union
select tablespace_name "Tablespace", to_char((sum(bytes/1024/1024/1024)),'99,999,990.900') "Total (G)"
from dba_data_files
where tablespace_name like'TEMP%'
groupby tablespace_name
) t1,
(
selecttablespace, round(sum(blocks)*8/1024) "Used (G)" from v$sort_usage
groupbytablespace
) t2
where t1."Tablespace"=t2.tablespace(+)
查看当前临死表使用空间大小与正在占用临时表空间的sql语句
select sess.SID, segtype, blocks*8/1000 "MB" ,sql_text
from v$sort_usage sort, v$session sess,v$sql sql
wheresort.SESSION_ADDR = sess.SADDR
andsql.ADDRESS = sess.SQL_ADDRESS
orderby blocks desc;
select'the ' || name || ' temp tablespaces ' || tablespace_name ||
' idle ' ||
round(100 - (s.tot_used_blocks / s.total_blocks) * 100, 3) ||
'% at ' || to_char(sysdate, 'yyyymmddhh24miss')
from (select d.tablespace_name tablespace_name,
nvl(sum(used_blocks), 0) tot_used_blocks,
sum(blocks) total_blocks
from v$sort_segment v, dba_temp_files d
where d.tablespace_name = v.tablespace_name(+)
groupby d.tablespace_name) s,
v$database;
修改临时文件大小
select'ALTER database TEMPFILE ' || file_name || ' resize 100M ;'
from dba_temp_files
where tablespace_name = 'ONLYDWTEMP';
ALTER database TEMPFILE '/oradata/ONLYDWTEMP06.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP07.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP08.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP09.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP10.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP01.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP02.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP03.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP04.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP05.dbf' resize 100M ;
SQL> ALTER database TEMPFILE '/oradata/ONLYDWTEMP09.dbf' resize 100M ;
ALTER database TEMPFILE '/oradata/ONLYDWTEMP09.dbf' resize 100M
ORA-03297: file contains used data beyond requested RESIZE value
创建新的临时表空间
SQL> create temporary tablespace TEMP1 TEMPFILE '/oradata/TEMP1_01.dbf' size 100M;
Tablespace created
SQL> create temporary tablespace TEMP2 TEMPFILE '/oradata/TEMP2_01.dbf' size 100M;
Tablespace created
将当前临时表空间指定为新的临时表空间
SQL> alter database default temporary tablespace TEMP1;
Database altered