表空间相关SQL
--查表空间使用率情况(含临时表空间)
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;