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(+);
发现使用率并不高,只有 1% 左右,起初群友打算直接删除部分临时表空间文件进行释放:
1 2 3 4 5 6 7
alter tablespace TEMP drop tempfile 14; alter tablespace TEMP drop tempfile 15; alter tablespace TEMP drop tempfile 16; alter tablespace TEMP drop tempfile 17; alter tablespace TEMP drop tempfile 18; alter tablespace TEMP drop tempfile 19; alter tablespace TEMP drop tempfile 20;
执行后发现临时文件并没有被删除,只是变成了 OFFLINE 状态。所以,打算直接更换临时表空间:
1 2 3
create temporary tablespace TEMP1 tempfile '+DATA' size 1G autoextend on; alter database default temporary tablespace temp1; drop tablespace temp including contents and datafiles;
发现有很多会话还占用了旧临时表空间 TEMP,所以无法删除 TEMP 表空间 HANG 住是正常现象。
我打算先将占用 TEMP 表空间的 INACTIVE 会话都杀掉:
1 2 3 4 5
select'alter system kill session ''' || s.sid || ',' || s.serial# || ''' immediate;' as kill_sql from v$sort_usage u join v$session s on u.session_addr = s.saddr where s.status = 'INACTIVE' and u.tablespace = 'TEMP';
再查看剩余占用 TEMP 的 ACTIVE 会话,发现这些会话都在执行一些 SELECT 语句:
1 2 3 4 5 6 7 8 9 10 11 12
select s.sid, s.serial#, s.username, s.program, s.status, u.tablespace, u.blocks*8/1024 mb from v$sort_usage u join v$session s on u.session_addr = s.saddr where s.status = 'ACTIVE' and u.tablespace = 'TEMP' order by mb desc;
跟业务沟通之后,确认可以杀掉这些会话:
1 2 3 4 5 6 7 8
select distinct 'alter system kill session ''' || s.sid || ',' || s.serial# || ',@' || s.inst_id || ''' immediate;' as kill_sql from gv$tempseg_usage u join gv$session s on s.inst_id = u.inst_id and s.saddr = u.session_addr where u.tablespace = 'TEMP' order by 1;
接着将所有占用 TEMP 的会话都杀掉,TEMP 临时表空间被成功 Drop。
继续切换为 TEMP 表空间:
1 2 3 4 5 6
create temporary tablespace TEMP tempfile '+DATA' size 1G autoextend on; alter database default temporary tablespace temp; drop tablespace temp1 including contents and datafiles;