<b>臨時表空間概念</b>
臨
時表空間用來管理資料庫排序操作以及用于存儲臨時表、中間排序結果等臨時對象,當oracle裡需要用到sort的時候,并且當pga中
sort_area_size大小不夠時,将會把資料放入臨時表空間裡進行排序。像資料庫中一些操作: create index、
analyze、select distinct、order by、group by、 union all、
intersect、minus、sort-merge joins、hash
join等都可能會用到臨時表空間。當操作完成後,系統會自動清理臨時表空間中的臨時對象,自動釋放臨時段。這裡的釋放隻是标記為空閑、可以重用,其實實
質占用的磁盤空間并沒有真正釋放。這也是臨時表空間有時會不斷增大的原因。
時表空間存儲大規模排序操作(小規模排序操作會直接在ram裡完成,大規模排序才需要磁盤排序disk
sort)和散列操作的中間結果.它跟永久表空間不同的地方在于它由臨時資料檔案(temporary
files)組成的,而不是永久資料檔案(datafiles)。臨時表空間不會存儲永久類型的對象,是以它不會也不需要備份。另外,對臨時資料檔案的操
作不産生redo日志,不過會生成undo日志。
創
建臨時表空間或臨時表空間添加臨時資料檔案時,即使臨時資料檔案很大,添加過程也相當快。這是因為oracle的臨時資料檔案是一類特殊的資料檔案:稀疏
檔案(sparse file),當臨時表空間檔案建立時,它隻會寫入檔案頭部和最後塊資訊(only writes to the header
and last block of the file)。它的空間是延後配置設定的.這就是你建立臨時表空間或給臨時表空間添加資料檔案飛快的原因。
另外,臨時表空間是nologging模式以及它不儲存永久類型對象,是以即使資料庫損毀,做recovery也不需要恢複temporary tablespace。
<b>臨時表空間資訊</b>
<b></b>
<b>檢視執行個體的臨時表空間</b>
sql1:
sql2:
<b></b>
<b>檢視臨時表空間資訊:</b>
官方文檔關于v$tempfile的介紹如下
column
datatype
description
file#
number
absolute file number
creation_change#
creation system change number (scn)
creation_time
date
creation time
ts#
tablespace number
rfile#
relative file number in the tablespace
status
varchar2(7)
status of the file (offline|online)
enabled
varchar2(10)
enabled for read and/or write
bytes
size of the file in bytes (from the file header)
blocks
size of the file in blocks (from the file header)
create_bytes
creation size of the file (in bytes)
block_size
block size for the file
name
varchar2(513)
name of the file
dba_temp_files describes all temporary files (tempfiles) in the database.
<b>datatype</b>
<b>null</b>
<b>description</b>
file_name
name of the database temp file
file_id
file identifier number of the database temp file
tablespace_name
varchar2(30)
not null
name of the tablespace to which the file belongs
size of the file (in bytes)
size of the file (in oracle blocks)
char(9)
file status:
·
· available
relative_fno
tablespace-relative file number
autoextensible
varchar2(3)
indicates whether the file is autoextensible (yes) or not (no)
maxbytes
maximum size of the file (in bytes)
maxblocks
maximum size of the file (in oracle blocks)
increment_by
default increment for autoextension
user_bytes
size of the useful portion of the file (in bytes)
user_blocks
size of the useful portion of the file (in oracle blocks)
這
四列中, bytes , blocks
顯示的是臨時檔案有多少byte大小,包含多少個資料塊。而user_bytes,user_blocks是可用的byte和資料塊個數。是以,我們可以
知道臨時檔案中有一部分是被系統占用的,大概可以了解成檔案頭資訊,這一部分大小是128個block,如下圖所示:

<b>管理臨時表空間</b>
<b>建立臨時表空間</b>
下面是一個簡單的建立臨時表空間的例子,具體很多細節可以參考官方文檔,這裡省略,不做過多介紹。
<a href="http://docs.oracle.com/cd/b10501_01/server.920/a96540/statements_75a.htm">http://docs.oracle.com/cd/b10501_01/server.920/a96540/statements_75a.htm</a>
<a href="http://docs.oracle.com/cd/b28359_01/server.111/b28310/tspaces002.htm#admin11366">http://docs.oracle.com/cd/b28359_01/server.111/b28310/tspaces002.htm#admin11366</a>
<b>增加資料檔案</b>
當臨時表空間太小時,就需要擴充臨時表空間(添加資料檔案、增大資料檔案、設定檔案自動擴充);有時候需要将臨時資料檔案分布到不同的磁盤分區中,提升io性能,也需要通過删除、增加臨時表空間資料檔案。
<b>删除資料檔案</b>
例如,我想删除臨時表空間下的某個檔案,那麼我們有兩種方式删除臨時表空間的資料檔案。
方法1:
注意:這種删除臨時表空間的寫法會将對應的實體檔案删除。
方法2:
注意:删除臨時表空間的臨時資料檔案時,不需要指定including datafiles 選項也會真正删除實體檔案,否則需要手工删除實體檔案。
<b>調整檔案大小</b>
如下例子,需要将臨時資料檔案從1g大小調整為2g
<b>檔案脫機聯機</b>
預設臨時表空間并不能脫機,否則會報錯,如下所示
sql> alter tablespace temp offline;
alter tablespace temp offline
*
error at line 1:
ora-03217: invalid option for alter of temporary tablespace
<b>設定檔案自動擴充</b>
<b>移動重命名檔案</b>
例如,我需要将/u01/app/oracle/oradata/gsp/temp4.dbf這個檔案重命名為/u01/app/oracle/oradata/gsp/temp04.dbf
1: 将臨時表空間的臨時檔案脫機
2:移動或重命名相關的臨時檔案
3: 使用腳本alter database rename file
4: 将臨時表空間的臨時檔案聯機
<b>删除臨時表空間</b>
注意:不能删除目前使用者的預設表空間,否則會報ora-12906錯誤
如果需要删除某一個預設的臨時表空間,則必須先建立一個臨時表空間,然後指定新建立的表空間為預設表空間,然後删除原來的臨時表空間
<b>臨時表空間組</b>
臨進表空間組:
臨進表空間組是oracle 10g引入的一個新特性,它是一個邏輯概念,不需要顯示的建立和删除。隻要把一個臨時表空間配置設定到一個組中,臨時表空間組就自動建立,所有的臨時表空間從臨時表空間組中移除就自動删除。
一個臨時表空間組必須由至少一個臨時表空間組成,并且無明确的最大數量限制.
a temporary tablespace group
contains at least one tablespace. there is no limit for a group to have a
maximum number of tablespaces
如果删除一個臨時表空間組的所有成員,該組也自動被删除。
臨時表空間的名字不能與臨時表空間組的名字相同。
it shares the namespace of tablespaces, thus its name cannot be the same as that of any tablespace.
可以在建立臨時表空間是指定表空間組,即隐式建立。
檢視臨時表空間組:
也可以指定已經建立好的臨時表空間的臨時表空間組。
從組中移除:
當為資料庫指定臨時表空間或為使用者指定臨時表空間時,可以使用臨時表空間組的名稱
<b>切換臨時表空間</b>
1:檢視舊臨時表空間資訊
select * from v$tempfile
select username, temporary_tablespace from dba_users
2:建立中轉的臨時表空間
3:添加相應的資料檔案
4:切換臨時表空間。
alter database default temporary tablespace tmp;
5:删除舊的臨時表空間資料檔案
drop tablespace temp including contents and datafiles;
6:如果有必要,重新指定使用者臨時表空間為建立的臨時表空間
<b>收縮臨時表空間</b>
排序等操作使用的臨時段,使用完成後會被标記為空閑,表示可以重用,占用的空間不會立即釋放,有時候臨時表空間會變得非常大,此時可以通過收縮臨時表空間來釋放沒有使用的空間。收縮臨時表空間是oracle 11g新增的功能。
<b>監控臨時表空間</b>
檢視臨時表空間使用情況:
檢視臨時表空間對應的臨時檔案的使用情況
查找消耗臨時表空間資源比較多的sql語句