Oracle临时表空间主要用来做查询和存放一些缓冲区数据。临时表空间消耗的主要原因是需要对查询的中间结果进行排序。重启数据库可以释放临时表空间,如果不能重启实例,而一直保持问题sql语句的执行,temp表空间会一直增长
Oracle临时表空间主要用来做查询和存放一些缓冲区数据。临时表空间消耗的主要原因是需要对查询的中间结果进行排序。
重启数据库可以释放临时表空间,如果不能重启实例,而一直保持问题sql语句的执行,temp表空间会一直增长。直到耗尽硬盘空间。
临时表空间的主要作用:
Distinct 操作
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(+)
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;
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;
' 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';
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
创建新的临时表空间
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