Oracle臨時表空間檢視、新增臨時表空間資料檔案、修改預設臨時表空間 方法!

kakaxi9521發表於2018-03-22

--查表空間使用率情況(含臨時表空間)
SELECT d.tablespace_name "Name", d.status "Status", 
       TO_CHAR (NVL (a.BYTES / 1024 / 1024, 0), '99,999,990.90') "Size (M)",
          TO_CHAR (NVL (a.BYTES - NVL (f.BYTES, 0), 0) / 1024 / 1024,
                   '99999999.99'
                  ) USE,
       TO_CHAR (NVL ((a.BYTES - NVL (f.BYTES, 0)) / a.BYTES * 100, 0),
                '990.00'
               ) "Used %"
  FROM SYS.dba_tablespaces d,
       (SELECT   tablespace_name, SUM (BYTES) BYTES
            FROM dba_data_files
        GROUP BY tablespace_name) a,
       (SELECT   tablespace_name, SUM (BYTES) BYTES
            FROM dba_free_space
        GROUP BY tablespace_name) f
 WHERE d.tablespace_name = a.tablespace_name(+)
   AND d.tablespace_name = f.tablespace_name(+)
   AND NOT (d.extent_management LIKE 'LOCAL' AND d.CONTENTS LIKE 'TEMPORARY')
UNION ALL
SELECT d.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) t
 WHERE d.tablespace_name = a.tablespace_name(+)
   AND d.tablespace_name = t.tablespace_name(+)
   AND d.extent_management LIKE 'LOCAL'
   AND d.CONTENTS LIKE 'TEMPORARY';

1. 查詢表空間剩餘位元組大小
SELECT TABLESPACE_NAME, SUM(BYTES)/1024/1024 AS "FREE SPACE(M)"
  FROM DBA_FREE_SPACE
 WHERE TABLESPACE_NAME = '&tablespace_name'
 GROUP BY TABLESPACE_NAME;
注:如果是臨時表空間,請查詢DBA_TEMP_FREE_SPACE
SELECT TABLESPACE_NAME, FREE_SPACE/1024/1024 AS "FREE SPACE(M)"
  FROM DBA_TEMP_FREE_SPACE
 WHERE TABLESPACE_NAME = '&tablespace_name';

2. 查詢表空間所有資料檔案路徑
SELECT TABLESPACE_NAME, FILE_ID, FILE_NAME, BYTES/1024/1024 AS "BYTES(M)"
  FROM DBA_DATA_FILES
 WHERE TABLESPACE_NAME = '&tablespace_name';

注:如果是臨時表空間,請查詢DBA_TEMP_FILES
SELECT TABLESPACE_NAME, FILE_ID, FILE_NAME, BYTES/1024/1024 AS "SPACE(M)"
  FROM DBA_TEMP_FILES
 WHERE TABLESPACE_NAME = '&tablespace_name';

3. 為空間不足的表空間增加資料檔案
ALTER TABLESPACE &tablespace_name ADD DATAFILE '&datafile_name' SIZE 2G;
注:如果要為臨時表空間擴容,使用下面的語句
ALTER TABLESPACE &tablespace_name ADD TEMPFILE '&datafile_name' SIZE 2G;

4.檢視臨時表空間的大小 和 資料檔案路徑
SELECT TABLESPACE_NAME, FILE_ID, FILE_NAME, BYTES/1024/1024 AS "SPACE(M)"
  FROM DBA_TEMP_FILES
 WHERE TABLESPACE_NAME = 'TEMP';
或者
select name, bytes/1024/1024 as "大小(M)" from v$tempfile order by bytes;

5.重建並修改預設臨時表空間辦法:

--查詢當前資料庫預設臨時表空間名
select * from database_properties where property_name='DEFAULT_TEMP_TABLESPACE';
--建立新的臨時表空間
create temporary tablespace temp02   tempfile 'E:\oracle\oradata\lims\TEMP02.DBF' size 1024M autoextend on;
--修改預設表空間為剛剛建立的臨時表空間
alter database default temporary tablespace temp02;
--檢視使用者所用臨時表空間的情況
SELECT USERNAME,TEMPORARY_TABLESPACE FROM DBA_USERS;
--刪除原來的臨時表空間
drop tablespace temp including contents and datafiles;
--檢視所有表空間名確認臨時表空間是否已刪除
select tablespace_name from dba_tablespaces; 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/21374452/viewspace-2152100/,如需轉載,請註明出處,否則將追究法律責任。

相關文章