臨時表空間作用
Oracle臨時表空間主要用來做查詢和存放一些緩沖區(qū)數(shù)據(jù),。臨時表空間消耗的主要原因是需要對查詢的中間結(jié)果進行排序,。 重啟數(shù)據(jù)庫可以釋放臨時表空間,,如果不能重啟實例,,而一直保持問題sql語句的執(zhí)行,,temp表空間會一直增長,。直到耗盡硬盤空間。
網(wǎng)上有人猜測在磁盤空間的分配上,,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 group by 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%' group by tablespace_name ) t1, ( select tablespace, round(sum(blocks)*8/1024) "Used (G)" from v$sort_usage group by tablespace ) 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 where sort.SESSION_ADDR = sess.SADDR and sql.ADDRESS = sess.SQL_ADDRESS order by 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(+) group by 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
創(chuàng)建新的臨時表空間
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
刪除舊的臨時表空間
SQL> drop tablespace ONLYDWTEMP including contents and datafiles; Tablespace dropped
檢查當前用戶的臨時表空間
9i之前,如果一個數(shù)據(jù)庫用戶沒有被指定默認臨時表空間,,那么oracle就會使用system表空間作為該用戶的臨時表空間,,這是很危險的。在9i里邊,database可以被指定一個默認臨時表空間,。這樣如果數(shù)據(jù)庫用戶沒有被明確指定臨時表空間,oracle9i就會自動指定database的默認臨時表空間作為該用戶的臨時表空間,。 我們可以通過下面的語句來查詢數(shù)據(jù)庫的默認臨時表空間。
select * from database_properties where property_name = 'DEFAULT_TEMP_TABLESPACE';
如下語句可以查詢數(shù)據(jù)庫用戶的臨時表空間,。
select username,default_tablespace,temporary_tablespace from dba_users
如果刪除了用戶的臨時表空間,,而這個臨時表空間又不是默認臨時表空間(如果是數(shù)據(jù)庫的默認臨時表空間是刪不掉的),,用戶的臨時表空間不會自動轉(zhuǎn)換到數(shù)據(jù)庫的默認臨時表空間上。這時如果使用該用戶登陸,,并執(zhí)行查詢用到臨時空間,,就會報錯。解決方式就是將用戶的臨時表空間修改過來。
SQL> alter user CTL temporary tablespace TEMP1;
User altered
SQL> alter user ODS temporary tablespace TEMP2;
User altered
select username,default_tablespace,temporary_tablespace from dba_users
user
tablespace temporary tablespace SCOTT SYSTEM ONLYDWTEMP CTL CTL TEMP1 ODS ODSD TEMP2 SQL> drop tablespace TEMP2 including contents and datafiles;
Tablespace dropped
select username,default_tablespace,temporary_tablespace from dba_users
user
tablespace temporary tablespace SCOTT SYSTEM ONLYDWTEMP CTL CTL TEMP1 ODS ODSD TEMP2 SQL> conn ods/ods@test_BI_DW
Connected to Oracle9i Enterprise Edition Release 9.2.0.8.0
Connected as ods
SQL> select do.object_name from dba_objects do, dba_objects db order by 1;
Tablespace TEMP2 not found
SQL> alter user ODS temporary tablespace TEMP1;
User altered
以上的方法只是暫時釋放了臨時表空間的磁盤,是治標但不是治本的方法,。真正的治本方法是找出數(shù)據(jù)庫中消耗資源比較大的sql語句,然后對其進行優(yōu)化處理,。
Select se.username,
se.sid, su.extents, su.blocks * to_number(rtrim(p.value)) as Space, tablespace, segtype, sql_text from v$sort_usage su, v$parameter p, v$session se, v$sql s where p.name = 'db_block_size' and su.session_addr = se.saddr and s.hash_value = su.sqlhash and s.address = su.sqladdr order by se.username, se.sid 本文來自CSDN博客,,轉(zhuǎn)載請標明出處:http://blog.csdn.net/47522341/archive/2009/02/05/3864171.aspx |
|