Oracle 暫存資料表空間的增刪改查

來源:互聯網
上載者:User

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,
      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
 
9、查看當前暫存資料表空間使用大小與正在佔用暫存資料表空間的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;

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.