Oracle 11g 臨時表空間管理

做者 | JiekeXu
html

來源 | JiekeXu之路(ID: JiekeXu_IT)
web

轉載請聯繫受權 | (微信ID:xxq1426321293)算法

你們好,我是 JiekeXu,很高興又和你們見面了,今天分享Oracle 11g 臨時表空間管理相關內容 本文發佈於微信公衆號【JiekeXu之路】,歡迎點擊上方藍字關注我,標星或置頂,更多幹貨第一時間到達!

近期咱們運維的數據庫有幾臺出現了 temp 臨時表空間使用率太高告警的問題,發現有些 DBA 居然選擇直接添加數據文件或者直接 resize 30G 來消除告警。這樣致使臨時文件很大佔用不少磁盤空間,沒有想到優化管理它,臨時表空間過大隻有重啓實例使用率纔會降低,若是沒有臨時表空間實例重啓也會自動建立出來,那麼今天抽出點時間來講說臨時表空間的管理。
sql


1、臨時表空間shell


臨時表空間包含僅在會話期間持續存在的臨時數據。臨時表空間能夠提升沒法裝入內存的多個排序操做的併發性,還能夠提升排序期間空間管理操做的效率。在臨時表空間中,針對特定實例的全部排序操做共享一個排序段,而且每一個執行須要臨時空間的排序操做的實例都存在排序段。排序段是由在啓動後使用臨時表空間進行排序的第一個語句建立的,只有在關閉時才釋放。默認狀況下,爲每一個新的 Oracle 數據庫安裝建立一個名爲 TEMP 的臨時表空間。也可使用 create TABLESPACE 語句建立額外的臨時表空間。
數據庫

 

臨時表空間用於存儲如下內容:apache

中間結果排序;微信

臨時表和臨時索引;網絡

臨時 Lob;session

臨時 B tree

 

Oracle 臨時表空間做用

Oracle 臨時表空間主要用來作查詢和存放一些緩衝區數據。臨時表空間消耗的主要緣由是須要對查詢的中間結果進行排序。重啓數據庫能夠釋放臨時表空間,若是不能重啓實例,而一直保持問題 SQL 語句的執行,temp 表空間會一直增加直到耗盡磁盤空間。網絡上有人猜想在磁盤空間的分配上,oracle 使用的是貪心算法,若是上次磁盤空間消耗達到 1GB,那麼臨時表空間就是 1GB。也就是說當前臨時表空間文件的大小是歷史上使用臨時表空間最大的大小。

 

臨時表空間的主要做用:

  索引 create 或 rebuild;

  Order by 或 group by;

  Distinct 操做;

  Union 或 intersect 或 minus;

  Sort-merge joins;

  analyze.

==========================================

oracle 臨時表空間的增刪改查等管理操做

一、查看臨時表空間 (dba_temp_files 視圖)(v_$tempfile 視圖)

select tablespace_name,file_name,bytes/1024/1024file_size,autoextensible from dba_temp_files;select status,enabled, name, bytes/1024/1024 file_size from v_$tempfile;--sys用戶查看

 2、查看臨時表空間當前使用的大小和歷史最大使用的大小

select 'max' asstatus,tablespace_name,sum(bytes_cached) / 1024 / 1024 as usagefrom v$temp_extent_poolgroup by tablespace_nameunion allselect 'current' as status,ss.tablespace_name,sum((ss.used_blocks * ts.blocksize)) / 1024 / 1024 asusagefrom gv$sort_segment ss, sys.ts$ tswhere ss.tablespace_name = ts.namegroup by ss.tablespace_name;

三、查看臨時表空間的使用狀況(GV_$TEMP_SPACE_HEADER 視圖必須在 sys 用戶下才能查詢)

GV_$TEMP_SPACE_HEADER 視圖記錄了臨時表空間的使用大小與未使用的大小

dba_temp_files 視圖的 bytes 字段記錄的是臨時表空間的總大小

SELECT temp_used.tablespace_name, total - used as"Free", total as"Total", round(nvl(total -used, 0) * 100 / total, 3) "Free percent" FROM (SELECT tablespace_name, SUM(bytes_used) /1024 / 1024 used FROM GV_$TEMP_SPACE_HEADER GROUP BYtablespace_name) temp_used, (SELECTtablespace_name, SUM(bytes) / 1024 / 1024 total FROM dba_temp_files GROUP BYtablespace_name) temp_total WHERE temp_used.tablespace_name =temp_total.tablespace_name;

查看臨時表空間對應的臨時文件的使用狀況

SELECT TABLESPACE_NAME AS TABLESPACE_NAME , BYTES_USED/1024/1024/1024 AS TABLESAPCE_USED , BYTES_FREE/1024/1024/1024 AS TABLESAPCE_FREEFROM V$TEMP_SPACE_HEADERORDERBY 1 DESC;

四、查找消耗資源比較多的 SQL 語句

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;

五、查看當前臨時表空間使用大小與正在佔用臨時表空間的 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;

六、擴展臨時表空間:

方法1、增大臨時文件大小:

SQL> alter database tempfile '/u01/app/oracle/oradata/orcl/temp01.dbf' resize 100m;

方法2、將臨時數據文件設爲自動擴展:

SQL> alter database tempfile '/u01/app/oracle/oradata/orcl/temp01.dbf' autoextend on next 5m maxsize unlimited;

方法3、向臨時表空間中添加數據文件:

SQL> alter tablespace temp add tempfile '/u01/app/oracle/oradata/orcl/temp02.dbf' size 100m;

七、建立臨時表空間:

SQL> create temporary tablespace temp1 tempfile '/u01/app/oracle/oradata/orcl/temp11.dbf' size 10M; 

八、更改系統的默認臨時表空間:

--查詢默認臨時表空間

select * from database_properties where property_name='DEFAULT_TEMP_TABLESPACE';

--修改默認臨時表空間

alter database default temporary tablespace temp1;

全部用戶的默認臨時表空間都將切換爲新的臨時表空間:

select username,temporary_tablespace,default_ fromdba_users;

--更改某一用戶的臨時表空間:

alter user scott temporary tablespace temp; 

九、刪除臨時表空間

刪除臨時表空間的一個數據文件:

SQL> alter database tempfile '/u01/app/oracle/oradata/orcl/temp02.dbf' drop;

刪除臨時表空間(完全刪除):

SQL> drop tablespace temp1 including contents and datafiles cascade constraints;

十、縮小臨時表空間大小

alter database tempfile '/u01/app/oracle/oradata/orcl/temp02.dbf' resize 100M;

2、臨時表空間組


臨時表空間組是 ORACLE 10g 引入的一個新特性,它是一個邏輯概念,不須要顯示的建立和刪除。只要把一個臨時表空間分配到一個組中,臨時表空間組就自動建立,全部的臨時表空間從臨時表空間組中移除就自動刪除。

一個臨時表空間組必須由至少一個臨時表空間組成,而且無明確的最大數量限制.

A temporary tablespace group contains at least one tablespace. There is no limitfor a group to have a maximum number of tablespaces

若是刪除一個臨時表空間組的全部成員,該組也自動被刪除。臨時表空間的名字不能與臨時表空間組的名字相同。

It shares the namespace of tablespaces, thus its name cannot be the same as thatof any tablespace.

能夠在建立臨時表空間時指定表空間組,即隱式建立。

 

1)建立臨時表空間組:

create temporary tablespace tempts1 tempfile '/u01/app/oracle/temp1_02.dbf' size 2M tablespace group group1;
create temporary tablespace tempts2 tempfile '/u01/app /oracle/temp2_02.dbf' size 2tablespace group group2;

 2)查詢臨時表空間組:dba_tablespace_groups 視圖

select * from dba_tablespace_groups;GROUP_NAME TABLESPACE_NAME------------------------------------------------------------GROUP1 TEMPTS1GROUP2 TEMPTS2

 3)將表空間從一個臨時表空間組移動到另一個臨時表空間組:

alter tablespace tempts1 tablespace group GROUP2 ;
select * from dba_tablespace_groups; GROUP_NAME TABLESPACE_NAME------------------------------------------------------------GROUP2 TEMPTS1GROUP2                        TEMPTS2

 4)把臨時表空間組指定給用戶

alter user scott temporary tablespace GROUP2;

 5)在數據庫級設置臨時表空間

alter database <db_name> default temporary tablespace GROUP2;

 6)刪除臨時表空間組 (刪除組成臨時表空間組的全部臨時表空間)

drop tablespace tempts1 including contents and datafiles;
select * from dba_tablespace_groups;GROUP_NAME TABLESPACE_NAME------------------------------------------------------------GROUP2 TEMPTS2 drop tablespace tempts2 including contents and datafiles;
select * from dba_tablespace_groups;GROUP_NAME TABLESPACE_NAME

3、臨時表空間 shrink 操做


對臨時表空間進行shrink(11g新增功能)

排序等操做使用的臨時段,使用完成後會被標記爲空閒,表示能夠重用,佔用的空間不會當即釋放,有時候臨時表空間會變得很是大,此時能夠經過收縮臨時表空間來釋放沒有使用的空間。收縮臨時表空間是 ORACLE11g 新增的功能。

 

Large sort operations performed by the database mayresult in a temporary tablespace growing and occupying a considerable amount ofdisk space. After the sort operation completes, the extra space is notreleased; it is just marked as free and available for reuse. Therefore, asingle large sort operation might result in a large amount of allocated temporary space that remains unused after the sort operation is complete. Forthis reason, the database enables you to shrink locally managed temporary tablespaces and release unused space.

數據庫執行的大型排序操做可能會致使臨時表空間的增加和佔用大量的磁盤空間。排序操做完成後,額外的空間不會被釋放;它只是被標記爲免費的,能夠重用。所以,單個大型排序操做可能會致使大量分配的臨時空間,這些空間在排序操做完成後仍未使用。因爲這個緣由,數據庫使您可以縮小本地管理的臨時表空間並釋放未使用的空間。

You use the SHRINK SPACE clause ofthe ALTER TABLESPACE statement to shrink a temporary tablespace, orthe SHRINK TEMPFILE clause of the ALTER TABLESPACE statement toshrink a specific temp file of a temporary tablespace. Shrinking frees as muchspace as possible while maintaining the other attributes of the tablespace ortemp file. The optional KEEP clause defines a minimum size for the tablespace or temp file。

可使用 ALTER TABLESPACE 語句的 SHRINK SPACE 子句來收縮臨時表空間,或者使用ALTER TABLESPACE statement 的 SHRINK TEMPFILE 子句來收縮臨時表空間的特定臨時文件。在保持表空間或臨時文件的其餘屬性的同時儘量減小空間可選的 KEEP 子句爲表空間或臨時文件定義了最小大小。


Shrinking is an online operation, which means that usersessions can continue to allocate sort extents if needed, and already-runningqueries are not affected.


收縮是一個在線操做,這意味着用戶會話能夠繼續在須要時分配排序區,而且已經運行的查詢不受影響。

 

--將 temp 表空間收縮爲10G

alter tablespace temp shrink space keep 10G; 

--自動將表空間的臨時文件縮小到最小可能的大小

ALTER TABLESPACE temp SHRINK TEMPFILE ’/u01/app/oracle/oradata/temp01.dbf’; 

--查看臨時表空間大小

Select * from dba_temp_free_space;Select * from v$temp_space_header;select tablespace_name,file_id,bytes/1024/1024 MB,blocks,maxbytes/1024/1024 MB,maxblocks from dba_temp_files; --這個語句仍是很好用的,快收藏哦!selectd.tablespace_name "Name",d.status"Status",TO_CHAR(NVL(a.BYTES/1024/1024,0),'99,999,990.90') "Size(M)",TO_CHAR(NVL(t.BYTES,0)/1024/1024,'99999999.99')USE,TO_CHAR(NVL(t.BYTES/a.BYTES*100,0),'990.00') "Used%"FROM SYS.dba_tablespaces d,(SELECT tablespace_name,SUM(BYTES) BYTES FROM dba_temp_files GROUP BY tablespace_name) a,(SELECT tablespace_name,SUM(bytes_cached) BYTES FROM v$temp_extent_pool GROUP BY tablespace_name) tWHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name =t.tablespace_name(+)AND d.extent_management LIKE 'LOCAL' AND d.CONTENTS LIKE 'TEMPORARY';

前面指定大小爲 16G,報錯ORA-03297:文件包含在請求的 RESIZE 值之外使用的數據。偏小了,若爲 18G則不會報錯,若是不指定保留空間大小, shrink space 會收縮臨時表空間到最小的可能空間大小。

這樣就解決了臨時表空間使用率過大的問題,固然,也可使用 resize 臨時文件來解決,那麼須要 resize 到多少纔算合適呢?咱們能夠查詢臨時空間使用狀況來判斷,視圖v$sort_usage 記錄了排序空間的使用狀況,找到最大的塊號。

Select (Selectmax(segblk#) from v$sort_usage)*8192/1024/1024/1024 Gb from dual;Alter databasetempfile '/oracle/oradata/ORCL/datafile/temp01.tmp' resize 18G;

總結:Shrink 操做完後,部分行數據的 rowid 發生了變化,同時,部分行數據的物理存儲的順序也會發生變化,而 table 所位於的 block 的區域卻沒有變化。因此,shrink 只移動了 table 其中一部分的行數據,來完成釋放空間,並且,這個過程是在 table 當前所使用的 block 中完成的。

 


參考連接:

https://www.cnblogs.com/einyboy/archive/2012/08/10/2631464.html

https://docs.oracle.com/cd/E11882_01/server.112/e25494/tspaces.htm#ADMIN01103

天天進步一點,一年後的進步將會很大,遠遠大於 「1」;天天退步,即便退步一點點,一年後幾乎退步爲 "0"。若是本文對您有一點兒幫助,請多支持「在看」與轉發,您的鼓勵將是我最大的動力,一塊兒加油,奧利給



Oracle 12c 及以上版本補丁更新說明及下載方法(收藏版)

Oracle 11.2.0.4 RAC 最新補丁下載(11.2.0.4.200714)

11g RAC 在線存儲遷移實現 OCR 磁盤組完美替換

如何經過 Shell 監控異常等待事件和活躍會話 

個人 OCM 之路|書寫無悔青春追夢永不止步

Oracle 19c 之多租戶 PDB 鏈接與訪問(三)

案例:RMAN 備份控制文件報錯 ORA-00230

Oracle 12C 最新補丁下載與安裝操做指北

DBA 經常使用的軟件工具備哪些(分享篇)?

Oracle 相關認證證書查詢及真僞辨別

Oracle 每日一題系列合集


點亮在看,你最好看!

本文分享自微信公衆號 - JiekeXu之路(JiekeXu_IT)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索