Temp 臨時表空間

Temporary Tablespacs 說明

            A temporary tablespace contains transient data that persists only for the duration of a session. No permanent schema objects can reside in a temporary tablespace. The database stores temporary tablespace data in temp files.

            Temporary tablespaces can improve the concurrency of multiple sort operations that do not fit in memory. These tablespaces also improve the efficiency of space management operations during sorts.

            When the SYSTEM tablespace is locally managed, a default temporary tablespace is included in the database by default during database creation. A locally managed SYSTEM tablespace cannot serve as default temporary storage.

-- 本地管理的system 表空間,不能作爲默認的臨時表空間。

            You cannot make a default temporary tablespace permanent.

            You can specify a user-named default temporary tablespace when you create a database by using the DEFAULT TEMPORARY TABLESPACE extension to the CREATE DATABASE statement. If SYSTEM is dictionary managed, and if a default temporary tablespace is not defined at database creation, then SYSTEM is the default temporary storage. However, the database writes a warning in the alert log saying that a default temporary tablespace is recommended.

-- SYSTEM 表空間是數據字典管理時,並且沒有定義默認的臨時表空間,那麼SYSTEM 表空間會作爲默認的temporary storage

            關於表空間的兩種類型: locally managed 和 dictionary managed的區別,參考我的Blog:

Oracle 自動段空間管理(ASSM:auto segment space management)

            http://blog.csdn.net/tianlesoftware/archive/2009/12/07/4958989.aspx

            A temporary tablespace contains schema objects only for the duration of a session. Locally managed temporary tablespaces have temporary files (temp files), which are special files designed to store data in hash, sort, and other operations. Temp files also store result set data when insufficient space exists in memory.

Temp files are similar to permanent data files, with the following exceptions:

(1)Permanent database objects such as tables are never stored in temp files.

(2)Temp files are always set to NOLOGGING mode, which means that they never have redo generated for them. Media recovery does not recognize temp files.

(3)You cannot make a temp file read-only.

(4)You cannot create a temp file with the ALTER DATABASE statement.

(5)When you create or resize temp files, they are not always guaranteed allocation of disk space for the file size specified. On file systems such as Linux and UNIX, temp files are created as sparse files. In this case, disk blocks are allocated not at file creation or resizing, but as the blocks are accessed for the first time.

Caution:

            Sparse files enable fast temp file creation and resizing; however, the disk could run out of space later when the temp files are accessed.

(6)Temp file information is shown in the data dictionary view DBA_TEMP_FILES and the dynamic performance view V$TEMPFILE, but not in DBA_DATA_FILES or the V$DATAFILE view.

            臨時表空間主要用途是在數據庫進行排序運算、管理索引、訪問視圖等操作時提供臨時的運算空間,當運算完成之後系統會自動清理。當oracle裏需要用到sort的時候,PGA中sort_area_size大小不夠時,將會把數據放入臨時表空間裏進行排序,同時如果有異常情況的話,也會被放入臨時表空間,正常來說,在完成Select語句、create index等一些使用TEMP表空間的排序操作後,Oracle是會自動釋放掉臨時段的。注意這裏的釋放,僅僅是將這些空間標記爲空閒,並可重用,真正佔用的磁盤空間並沒有釋放。所以Temp表空間可能會越來越大。

            排序是很耗資源的,Temp表空間滿了,關鍵是優化你的語句,儘量使排序減少纔是上策.

Temp 表空間的操作

           

            You cannot use the ALTER TABLESPACE statement, with the TEMPORARY keyword, to change a locally managed permanent tablespace into a locally managed temporary tablespace. You must use the CREATE TEMPORARY TABLESPACE statement to create a locally managed temporary tablespace.

            You can use ALTER TABLESPACE to add a tempfile, take a tempfile offline, or bring a tempfile online, as illustrated in the following examples:

ALTER TABLESPACE lmtemp

   ADD TEMPFILE '/u02/oracle/data/lmtemp02.dbf' SIZE 18M REUSE;

SQL>ALTER TABLESPACE lmtemp TEMPFILE OFFLINE;

SQL>ALTER TABLESPACE lmtemp TEMPFILE ONLINE;

Note:

            You cannot take a temporary tablespace offline. Instead, you take its tempfile offline. The view V$TEMPFILE displays online status for a tempfile.

-- 不可以將Temp 表空間offline,但是可以將tempfile offlineV$TEMPFILE 顯示了tempfile 的狀態。

The ALTER DATABASE statement can be used to alter tempfiles.

The following statements take offline and bring online tempfiles. They behave identically to the last two ALTER TABLESPACE statements in the previous example.

SQL>ALTER DATABASE TEMPFILE '/u02/oracle/data/lmtemp02.dbf' OFFLINE;

SQL>ALTER DATABASE TEMPFILE '/u02/oracle/data/lmtemp02.dbf' ONLINE;

The following statement resizes a tempfile: -- resize 表空間

SQL>ALTER DATABASE TEMPFILE '/u02/oracle/data/lmtemp02.dbf' RESIZE 18M;

The following statement drops a tempfile and deletes its operating system file:

SQL>ALTER DATABASE TEMPFILE '/u02/oracle/data/lmtemp02.dbf' DROP

    INCLUDING DATAFILES;

-- drop tempfile 和它的物理文件。

            The tablespace to which this tempfile belonged remains. A message is written to the alert log for the tempfile that was deleted. If an operating system error prevents the deletion of the file, the statement still succeeds, but a message describing the error is written to the alert log.

            -- 當我們把temp 表空間的數據文件文件刪除之後,表空間的信息還會存在,但是在alert log裏會有錯誤信息。

            It is also possible to use the ALTER DATABASE statement to enable or disable the automatic extension of an existing tempfile, and to rename a tempfile. See Oracle Database SQL Language Reference for the required syntax.

Note:

            To rename a tempfile, you take the tempfile offline, use operating system commands to rename or relocate the tempfile, and then use the ALTER DATABASE RENAME FILE command to update the database controlfiles.

tempfile 數據文件重命名的步驟:

            (1)將tempfile offline       

            (2)在操作系統上重命名tempfile

            (3)使用alter database rename file 更新控制文件。

. 臨時表空間滿時的處理方法

3.1 添加數據文件

            如果Temporary tablespace還不大,那麼我們可以增加一些數據文件。SQL 語句如下:

SQL>ALTERTABLESPACE TEMP ADDTEMPFILE'D:/ORADATA/NEWCCS/TEMP02.DBF'SIZE100MAUTOEXTENDOFF;

            一般來說,Temp tablespace 和 Undo Tablespace 是不建議設置爲自增長,設置自增長可能會把磁盤給撐滿。

3.2 修改數據文件大小

            可以將原來的數據文件改大一點,如:

SQL>ALTERDATABASETEMPFILE'D:/ ORADATA/NEWCCS/TEMP02.DBF'

RESIZE100M;

. Temp 表空間過大的處理方法

            Temp 表空間過大,會佔用很多的磁盤空間,這時候,我們可以用一下2中方法來縮小temp 表空間的大小。

4.1  替換Temp 表空間

4.1.1  查看目前Temp 表空間的信息

SQL> select name from v$tempfile;

NAME

———————————————————————

D:/ORACLE/ORADATA/TEST/TEMP01.DBF

SQL> select username,temporary_tablespace from dba_users;

USERNAME            TEMPORARY_TABLESPACE

------------------------------ ------------------------------

MGMT_VIEW                  TEMP

SYS                           TEMP

SYSTEM                       TEMP

DBSNMP                       TEMP

SYSMAN                       TEMP

            關於用戶這塊是要特別注意的,如果我們將默認的Temp 表空間指向其他的名稱,那麼這些用戶的信息就會失效,所以,我們特換時,要麼創建一個臨時的Temp 表空間中轉一下,這樣切換之後,我們的temp表空間名稱不變,要麼改變名稱,同時更新相關用戶的default temp 表空間。

這裏用中轉的方法來測試。

4.1.2 創建中轉臨時表空間

            UNIFORM specifies that the tablespace is managed with uniform extents of SIZE bytes.The default SIZE is 1 megabyte. All extents of temporary tablespaces are of uniform size, so this keyword is optional for a temporary tablespace. However, you must specify UNIFORM in order to specify SIZE. You cannot specify UNIFORM for an undo tablespace.

-- Temp 表空間必須是uniform 的,undo 必須是autoallocate的。默認情況下uniform 1M

            If you do not specify AUTOALLOCATE or UNIFORM, then the default is UNIFORM for temporary tablespaces and AUTOALLOCATE for all other types of tablespaces.

更多內容參考:

Oracle 表空間創建參數說明

            http://blog.csdn.net/tianlesoftware/archive/2011/01/27/6166928.aspx

創建SQL 如下:

SQL>CREATETEMPORARYTABLESPACE TEMP2 TEMPFILE

'D:/APP/ADMINISTRATOR/ORADATA/NEWCCS/TEMP03.DBF'SIZE10MAUTOEXTENDOFF;

EXTENTMANAGEMENTLOCALUNIFORMSIZE1M;

上面是默認情況,等於一下SQL:

SQL>CREATETEMPORARYTABLESPACE TEMP2 TEMPFILE

'D:/APP/ADMINISTRATOR/ORADATA/NEWCCS/TEMP03.DBF'SIZE10MAUTOEXTENDOFF

EXTENTMANAGEMENTLOCALUNIFORMSIZE1M;

4.1.3 修改Temp2爲默認臨時表空間

SQL>alter database default temporary tablespace temp2;

4.1.4. 刪除原來臨時表空間

SQL>drop tablespace temp including contents and datafiles;

4.1.5.  重新創建臨時表空間

SQL>CREATETEMPORARYTABLESPACE TEMP TEMPFILE

'D:/APP/ADMINISTRATOR/ORADATA/NEWCCS/TEMP03.DBF'SIZE10MAUTOEXTENDOFF;

EXTENTMANAGEMENTLOCALUNIFORMSIZE1M;

4.1.6. 重置缺省臨時表空間爲新建的temp表空間

SQL>alter database default temporary tablespace temp;

4.1.7. 刪除中轉用臨時表空間

SQL>drop tablespace temp2 including contents and datafiles;

4.1.8 如果有必要,重新指定用戶表空間爲重建的臨時表空間

SQL>alter user dave temporary tablespace temp; 

4.2 Shrinking a Locally Managed Temporary Tablespace

            Large sort operations performed by the database may result in a temporary tablespace growing and occupying a considerable amount of disk space. After the sort operation completes, the extra space is not released; it is just marked as free and available for reuse.

            -- 當排序操作完成,佔用的空間並沒有釋放,僅僅是將它標記爲空閒,並可重用。

            Therefore, a single large sort operation might result in a large amount of allocated temporary space that remains unused after the sort operation is complete. For this reason, the database enables you to shrink locally managed temporary tablespaces and release unused space.

--可以使用shrink 來釋放沒有使用的空間。

            (1)You use the SHRINK SPACE clause of the ALTER TABLESPACE statement to shrink a temporary tablespace, or 2the SHRINK TEMPFILE clause of the ALTER TABLESPACE statement to shrink a specific tempfile of a temporary tablespace. Shrinking frees as much space as possible while maintaining the other attributes of the tablespace or tempfile. The optional KEEP clause defines a minimum size for the tablespace or tempfile.

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

            -- shrink 是一個online 的操作,不影響其他的查詢。

示例一:

            The following example shrinks the locally managed temporary tablespace lmtmp1 to a size of 20M.

            SQL>ALTER TABLESPACE lmtemp1 SHRINK SPACE KEEP 20M;

示例二:

            The following example shrinks the tempfile lmtemp02.dbf of the locally managed temporary tablespace lmtmp2. Because the KEEP clause is omitted, the database attempts to shrink the tempfile to the minimum possible size.

            SQL>ALTER TABLESPACE lmtemp2 SHRINK TEMPFILE '/u02/oracle/data/lmtemp02.dbf';

 

附註:

1、查看臨時表空間 (dba_temp_files視圖)(v_$tempfile視圖)
select tablespace_name,file_name,bytes/1024/1024 file_size,autoextensible from dba_temp_files;
select status,enabled, name, bytes/1024/1024 file_size from v_$tempfile;--sys用戶查看

2、縮小臨時表空間大小
alter database tempfile 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\TELEMT\TEMP01.DBF' resize 100M;

3、擴展臨時表空間:
方法一、增大臨時文件大小:
SQL> alter database tempfile ‘/u01/app/oracle/oradata/orcl/temp01.dbf’ resize 100m;
方法二、將臨時數據文件設爲自動擴展:
SQL> alter database tempfile ‘/u01/app/oracle/oradata/orcl/temp01.dbf’ autoextend on next 5m maxsize unlimited;
方法三、向臨時表空間中添加數據文件:
SQL> alter tablespace temp add tempfile ‘/u01/app/oracle/oradata/orcl/temp02.dbf’ size 100m;

4、創建臨時表空間
SQL> create temporary tablespace temp1 tempfile ‘/u01/app/oracle/oradata/orcl/temp11.dbf’ size 10M;

5、更改系統的默認臨時表空間:
--查詢默認臨時表空間
select * from database_properties where property_name='DEFAULT_TEMP_TABLESPACE';
--修改默認臨時表空間
alter database default temporary tablespace temp1;
所有用戶的默認臨時表空間都將切換爲新的臨時表空間:
select username,temporary_tablespace,default_ from dba_users;
--更改某一用戶的臨時表空間:
alter user scott temporary tablespace temp;

6、刪除臨時表空間
刪除臨時表空間的一個數據文件:
SQL> alter database tempfile ‘/u01/app/oracle/oradata/orcl/temp02.dbf’ drop;
刪除臨時表空間(徹底刪除):
SQL> drop tablespace temp1 including contents and datafiles cascade constraints;

7、查看臨時表空間的使用情況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 BY tablespace_name) temp_used,
       (SELECT tablespace_name, SUM(bytes) / 1024 / 1024 total
          FROM dba_temp_files
         GROUP BY tablespace_name) temp_total
WHERE temp_used.tablespace_name = temp_total.tablespace_name

8、查找消耗資源比較的sql語句
Select se.username,
       se.sid,
       su.extents,
       su.blocks * to_number(rtrim(p.value)) as Space,
       su.tablespace,
       su.segtype,
       s.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

9、查看當前臨時表空間使用大小與正在佔用臨時表空間的sql語句
select sess.SID, sort.segtype, sort.blocks*8/1000 "MB", sql.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 sort.blocks desc;

10、臨時表空間組介紹
1)創建臨時表空間組:
create temporary tablespace tempts1 tempfile '/home/oracle/temp1_02.dbf' size 2M tablespace group group1;
create temporary tablespace tempts2 tempfile '/home/oracle/temp2_02.dbf' size 2M tablespace group group2;

2)查詢臨時表空間組dba_tablespace_groups視圖
select * from dba_tablespace_groups;
GROUP_NAME                     TABLESPACE_NAME
------------------------------ ------------------------------
GROUP1                         TEMPTS1
GROUP2                         TEMPTS2

3)將表空間從一個臨時表空間組移動到另外一個臨時表空間組:
alter tablespace tempts1 tablespace group GROUP2 ;
select * from dba_tablespace_groups;

GROUP_NAME                     TABLESPACE_NAME
------------------------------ ------------------------------
GROUP2                         TEMPTS1
GROUP2                         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

11、對臨時表空間進行shrink(11g新增的功能)
--將temp表空間收縮爲20M
alter tablespace temp shrink space keep 20M;
--自動將表空間的臨時文件縮小到最小可能的大小
ALTER TABLESPACE temp SHRINK TEMPFILE ’/u02/oracle/data/lmtemp02.dbf’;

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

臨時表空間的主要作用:
  索引create或rebuild;
  Order by 或 group by;
  Distinct 操作;
  Union 或 intersect 或 minus;
  Sort-merge joins;
  analyze。

v$sort_segment字典中,我們可能看到temp的詳細的使用情況,

SQL> desc v$sort_segment

v$sort_usage將會提供目前操作的會話.

SQL> desc v$sort_usage;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章