oracle表空间查询维护命令大全之三(暂时表空间)史上最全
--UNDO表空间汇总
--查看全部的表空间名字
SELECT NAME FROM V$TABLESPACE;
--创建新的UNDO表空间,并设置自己主动扩展參数;
CREATE UNDO TABLESPACE UNDOTBS2 DATAFILE 'D:\ORACLE\PRODUCT\10.1.0\ORADATA\ORCL\UNDOTBS02.DBF' SIZE 10M REUSE AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED;
--注意:在OPEN状态下某些时刻仅仅能用一个UNDO表空间。假设要用新建的表空间,必须切换到该表空间:
ALTER SYSTEM SET UNDO_TABLESPACE = UNDO2;
--改动为自己主动管理
ALTER SYSTEM SET UNDO_MANAGEMENT = AUTO SCOPE = SPFILE;
改动UNDO的表空间管理方式为MANUAL ALTER SYSTEM SET UNDO_MANAGEMENT = MANUAL SCOPE = SPFILE;
--改动
--等待原UNDO表空间全部UNDO SEGMENT OFFLINE;
SELECT USN,
XACTS,
STATUS,
RSSIZE / 1024 / 1024 / 1024,
HWMSIZE / 1024 / 1024 / 1024,
SHRINKS
FROM V$ROLLSTAT
ORDER BY RSSIZE;
--删除表空间
DROP TABLESPACE UNDO1 INCLUDING CONTENTS AND DATAFILES;
--更改UODO表空间的大小
ALTER DATABASE DATAFILE '/U2/ORADATA/SPRING/UNDOTBS01.DBF' RESIZE 1024M;
SELECT TABLESPACE_NAME, SEGMENT_TYPE, OWNER, SEGMENT_NAME
FROM DBA_EXTENTS
WHERE FILE_ID = 8
AND 565129 BETWEEN BLOCK_ID AND BLOCK_ID + BLOCKS - 1;
1, 暂时表空间的主要作用: 索引CREATE或REBUILD; ORDER BY 或 GROUP BY; DISTINCT 操作; UNION 或 INTERSECT 或 MINUS; SORT - MERGE JOINS; ANALYZE.
SELECT USERNAME, TEMPORARY_TABLESPACE FROM DBA_USERS;
查看默认暂时表空间
SELECT *
FROM DATABASE_PROPERTIES
WHERE PROPERTY_NAME = 'DEFAULT_TEMP_TABLESPACE';
--建立暂时表空间
CREATE TEMPORARY TABLESPACE TEMP_DATA TEMPFILE '/ORACLE/ORADATA/DB/TEMP_DATA.DBF' SIZE 50M
CREATE TEMPORARY TABLESPACE TEMP TEMPFILE '/U02/ORADATA/ORCL/ORCL/TEMP01.DBF' SIZE 6144M, '/U02/ORADATA/ORCL/ORCL/TEMP02.DBF' SIZE 6144M;
--改动暂时表空间的大小
ALTER DATABASE TEMPFILE '/U2/ORADATA/SPRING/TEMP_DATA.DBF' RESIZE 1024M;
CREATE TEMPORARY TABLESPACE TEMP1 TEMPFILE '/U02/ORADATA/ORCL/ORCL/TEMP101.DBF' SIZE 4056M;
--改动数据库的默认暂时表空间
ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEMP1;
--删除暂时表空间
DROP TABLESPACE TEMP INCLUDING CONTENTS AND DATAFILES;
--清理暂时表空间
ALTER TABLESPACE TEMP SHRINK SPACE KEEP 20M;
--自己主动将表空间的暂时文件缩小到最小可能的大小
ALTER TABLESPACE TEMP SHRINK TEMPFILE ’ / U02 / ORACLE / DATA / LMTEMP02.DBF’;
2, 暂时表空间过大,又一次暂时表空间的详细步骤汇总:
1.创建中转暂时表空间 CREATETEMPORARYTABLESPACETEMP1 TEMPFILE '/ORACLE/ORADATA/SECOOLER/TEMP02.DBF' SIZE 512M REUSE AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED;
2.改变缺省暂时表空间为刚刚创建的新暂时表空间TEMP1 ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEMP1;
验证用户的暂时表空间为TEMP1
SELECT USERNAME, TEMPORARY_TABLESPACE FROM DBA_USERS;
3.删除原暂时表空间 DROP TABLESPACE TEMP INCLUDING CONTENTS AND DATAFILES;
4.重建暂时表空间 CREATE TEMPORARY TABLESPACE TEMP TEMPFILE '/ORACLE/ORADATA/SECOOLER/TEMP01.DBF' SIZE 512M REUSE AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED;
5.重置缺省暂时表空间为新建的TEMP表空间 ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEMP;
验证用户的暂时表空间为TEMP
SELECT USERNAME, TEMPORARY_TABLESPACE FROM DBA_USERS;
3、查看谁在用暂时表空间
SELECT SE.USERNAME,
SE.SID,
SE.SERIAL#,
SE.SQL_ADDRESS,
SE.MACHINE,
SE.PROGRAM,
SU.TABLESPACE,
SU.SEGTYPE,
SU.CONTENTS FROM V$SESSION SE,
V$SORT_USAGE SU WHERE SE.SADDR = SU.SESSION_ADDR;
4、查看暂时表空间TEMP空暇情况
SELECT TABLESPACE_NAME,
FILE_ID,
BYTES_USED / 1024 / 1024,
BYTES_FREE / 1024 / 1024
FROM V$TEMP_SPACE_HEADER;
5, 详细到某个SID暂时表空间使用情况
SELECT B.TABLESPACE,
B.SEGFILE#,
B.SEGBLK#,
B.BLOCKS,
B.BLOCKS * 32 / 1024 / 1024,
A.SID,
A.SERIAL#,
A.USERNAME,
A.OSUSER,
A.STATUS,
C.SQL_TEXT,
B.CONTENTS
FROM V$SESSION A, V$SORT_USAGE B, V$SQL C
WHERE A.SADDR = B.SESSION_ADDR
AND A.SQL_ADDRESS = C.ADDRESS(+)
ORDER BY B.BLOCKS DESC
在创建用户时,
有一个默认的表空间的參数. 能够通过查看视图DATABASE_PROPERTIES能够看到对应的信息.
SELECT A.PROPERTY_NAME, A.PROPERTY_VALUE
FROM DATABASE_PROPERTIES A
WHERE A.PROPERTY_NAME LIKE '%DEFAULT%';
6, /*查看暂时表空间整体使用情况*/
SELECT TMP_TBS.TABLESPACE_NAME,
SUM(TMP_TBS.TOTAL_MB) TOTAL_MB,
SUM(USED_TOT.USED_MB) USED_MB,
SUM(USED_TOT.USED_MB) / SUM(TMP_TBS.TOTAL_MB) * 100 USED_PERSENT
FROM (SELECT TABLESPACE_NAME, SUM(BYTES) / 1024 / 1024 TOTAL_MB
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) TMP_TBS,
(SELECT TMP_USED.TABLESPACE,
SUM(TMP_USED.BLOCKS * PARA.DB_BLOCK_SIZE) / 1024 / 1024 USED_MB
FROM V$SORT_USAGE TMP_USED,
(SELECT VALUE DB_BLOCK_SIZE
FROM V$PARAMETER
WHERE NAME = 'DB_BLOCK_SIZE') PARA
GROUP BY TMP_USED.TABLESPACE) USED_TOT
WHERE TMP_TBS.TABLESPACE_NAME = USED_TOT.TABLESPACE(+)
GROUP BY TMP_TBS.TABLESPACE_NAME;
7, /*查看暂时表空间中排序段和数据段的使用情况*/
SELECT TMP_TBS.TABLESPACE_NAME,
USED_TOT.SEGTYPE TEMP_SEG_TYPE,
SUM(TMP_TBS.TOTAL_MB) TOTAL_MB,
SUM(USED_TOT.USED_MB) USED_MB,
SUM(USED_TOT.USED_MB) / SUM(TMP_TBS.TOTAL_MB) * 100 USED_PERSENT
FROM (SELECT TABLESPACE_NAME, SUM(BYTES) / 1024 / 1024 TOTAL_MB
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) TMP_TBS,
(SELECT TMP_USED.TABLESPACE,
TMP_USED.SEGTYPE,
SUM(TMP_USED.BLOCKS * PARA.DB_BLOCK_SIZE) / 1024 / 1024 USED_MB
FROM V$SORT_USAGE TMP_USED,
(SELECT VALUE DB_BLOCK_SIZE
FROM V$PARAMETER
WHERE NAME = 'DB_BLOCK_SIZE') PARA
GROUP BY TMP_USED.TABLESPACE, TMP_USED.SEGTYPE) USED_TOT
WHERE TMP_TBS.TABLESPACE_NAME = USED_TOT.TABLESPACE(+)
GROUP BY TMP_TBS.TABLESPACE_NAME, USED_TOT.SEGTYPE;
--查看全部的表空间名字
SELECT NAME FROM V$TABLESPACE;
--创建新的UNDO表空间,并设置自己主动扩展參数;
CREATE UNDO TABLESPACE UNDOTBS2 DATAFILE 'D:\ORACLE\PRODUCT\10.1.0\ORADATA\ORCL\UNDOTBS02.DBF' SIZE 10M REUSE AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED;
--注意:在OPEN状态下某些时刻仅仅能用一个UNDO表空间。假设要用新建的表空间,必须切换到该表空间:
ALTER SYSTEM SET UNDO_TABLESPACE = UNDO2;
--改动为自己主动管理
ALTER SYSTEM SET UNDO_MANAGEMENT = AUTO SCOPE = SPFILE;
改动UNDO的表空间管理方式为MANUAL ALTER SYSTEM SET UNDO_MANAGEMENT = MANUAL SCOPE = SPFILE;
--改动
--等待原UNDO表空间全部UNDO SEGMENT OFFLINE;
SELECT USN,
XACTS,
STATUS,
RSSIZE / 1024 / 1024 / 1024,
HWMSIZE / 1024 / 1024 / 1024,
SHRINKS
FROM V$ROLLSTAT
ORDER BY RSSIZE;
--删除表空间
DROP TABLESPACE UNDO1 INCLUDING CONTENTS AND DATAFILES;
--更改UODO表空间的大小
ALTER DATABASE DATAFILE '/U2/ORADATA/SPRING/UNDOTBS01.DBF' RESIZE 1024M;
SELECT TABLESPACE_NAME, SEGMENT_TYPE, OWNER, SEGMENT_NAME
FROM DBA_EXTENTS
WHERE FILE_ID = 8
AND 565129 BETWEEN BLOCK_ID AND BLOCK_ID + BLOCKS - 1;
1, 暂时表空间的主要作用: 索引CREATE或REBUILD; ORDER BY 或 GROUP BY; DISTINCT 操作; UNION 或 INTERSECT 或 MINUS; SORT - MERGE JOINS; ANALYZE.
SELECT USERNAME, TEMPORARY_TABLESPACE FROM DBA_USERS;
查看默认暂时表空间
SELECT *
FROM DATABASE_PROPERTIES
WHERE PROPERTY_NAME = 'DEFAULT_TEMP_TABLESPACE';
--建立暂时表空间
CREATE TEMPORARY TABLESPACE TEMP_DATA TEMPFILE '/ORACLE/ORADATA/DB/TEMP_DATA.DBF' SIZE 50M
CREATE TEMPORARY TABLESPACE TEMP TEMPFILE '/U02/ORADATA/ORCL/ORCL/TEMP01.DBF' SIZE 6144M, '/U02/ORADATA/ORCL/ORCL/TEMP02.DBF' SIZE 6144M;
--改动暂时表空间的大小
ALTER DATABASE TEMPFILE '/U2/ORADATA/SPRING/TEMP_DATA.DBF' RESIZE 1024M;
CREATE TEMPORARY TABLESPACE TEMP1 TEMPFILE '/U02/ORADATA/ORCL/ORCL/TEMP101.DBF' SIZE 4056M;
--改动数据库的默认暂时表空间
ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEMP1;
--删除暂时表空间
DROP TABLESPACE TEMP INCLUDING CONTENTS AND DATAFILES;
--清理暂时表空间
ALTER TABLESPACE TEMP SHRINK SPACE KEEP 20M;
--自己主动将表空间的暂时文件缩小到最小可能的大小
ALTER TABLESPACE TEMP SHRINK TEMPFILE ’ / U02 / ORACLE / DATA / LMTEMP02.DBF’;
2, 暂时表空间过大,又一次暂时表空间的详细步骤汇总:
1.创建中转暂时表空间 CREATETEMPORARYTABLESPACETEMP1 TEMPFILE '/ORACLE/ORADATA/SECOOLER/TEMP02.DBF' SIZE 512M REUSE AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED;
2.改变缺省暂时表空间为刚刚创建的新暂时表空间TEMP1 ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEMP1;
验证用户的暂时表空间为TEMP1
SELECT USERNAME, TEMPORARY_TABLESPACE FROM DBA_USERS;
3.删除原暂时表空间 DROP TABLESPACE TEMP INCLUDING CONTENTS AND DATAFILES;
4.重建暂时表空间 CREATE TEMPORARY TABLESPACE TEMP TEMPFILE '/ORACLE/ORADATA/SECOOLER/TEMP01.DBF' SIZE 512M REUSE AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED;
5.重置缺省暂时表空间为新建的TEMP表空间 ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEMP;
验证用户的暂时表空间为TEMP
SELECT USERNAME, TEMPORARY_TABLESPACE FROM DBA_USERS;
3、查看谁在用暂时表空间
SELECT SE.USERNAME,
SE.SID,
SE.SERIAL#,
SE.SQL_ADDRESS,
SE.MACHINE,
SE.PROGRAM,
SU.TABLESPACE,
SU.SEGTYPE,
SU.CONTENTS FROM V$SESSION SE,
V$SORT_USAGE SU WHERE SE.SADDR = SU.SESSION_ADDR;
4、查看暂时表空间TEMP空暇情况
SELECT TABLESPACE_NAME,
FILE_ID,
BYTES_USED / 1024 / 1024,
BYTES_FREE / 1024 / 1024
FROM V$TEMP_SPACE_HEADER;
5, 详细到某个SID暂时表空间使用情况
SELECT B.TABLESPACE,
B.SEGFILE#,
B.SEGBLK#,
B.BLOCKS,
B.BLOCKS * 32 / 1024 / 1024,
A.SID,
A.SERIAL#,
A.USERNAME,
A.OSUSER,
A.STATUS,
C.SQL_TEXT,
B.CONTENTS
FROM V$SESSION A, V$SORT_USAGE B, V$SQL C
WHERE A.SADDR = B.SESSION_ADDR
AND A.SQL_ADDRESS = C.ADDRESS(+)
ORDER BY B.BLOCKS DESC
在创建用户时,
有一个默认的表空间的參数. 能够通过查看视图DATABASE_PROPERTIES能够看到对应的信息.
SELECT A.PROPERTY_NAME, A.PROPERTY_VALUE
FROM DATABASE_PROPERTIES A
WHERE A.PROPERTY_NAME LIKE '%DEFAULT%';
6, /*查看暂时表空间整体使用情况*/
SELECT TMP_TBS.TABLESPACE_NAME,
SUM(TMP_TBS.TOTAL_MB) TOTAL_MB,
SUM(USED_TOT.USED_MB) USED_MB,
SUM(USED_TOT.USED_MB) / SUM(TMP_TBS.TOTAL_MB) * 100 USED_PERSENT
FROM (SELECT TABLESPACE_NAME, SUM(BYTES) / 1024 / 1024 TOTAL_MB
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) TMP_TBS,
(SELECT TMP_USED.TABLESPACE,
SUM(TMP_USED.BLOCKS * PARA.DB_BLOCK_SIZE) / 1024 / 1024 USED_MB
FROM V$SORT_USAGE TMP_USED,
(SELECT VALUE DB_BLOCK_SIZE
FROM V$PARAMETER
WHERE NAME = 'DB_BLOCK_SIZE') PARA
GROUP BY TMP_USED.TABLESPACE) USED_TOT
WHERE TMP_TBS.TABLESPACE_NAME = USED_TOT.TABLESPACE(+)
GROUP BY TMP_TBS.TABLESPACE_NAME;
7, /*查看暂时表空间中排序段和数据段的使用情况*/
SELECT TMP_TBS.TABLESPACE_NAME,
USED_TOT.SEGTYPE TEMP_SEG_TYPE,
SUM(TMP_TBS.TOTAL_MB) TOTAL_MB,
SUM(USED_TOT.USED_MB) USED_MB,
SUM(USED_TOT.USED_MB) / SUM(TMP_TBS.TOTAL_MB) * 100 USED_PERSENT
FROM (SELECT TABLESPACE_NAME, SUM(BYTES) / 1024 / 1024 TOTAL_MB
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) TMP_TBS,
(SELECT TMP_USED.TABLESPACE,
TMP_USED.SEGTYPE,
SUM(TMP_USED.BLOCKS * PARA.DB_BLOCK_SIZE) / 1024 / 1024 USED_MB
FROM V$SORT_USAGE TMP_USED,
(SELECT VALUE DB_BLOCK_SIZE
FROM V$PARAMETER
WHERE NAME = 'DB_BLOCK_SIZE') PARA
GROUP BY TMP_USED.TABLESPACE, TMP_USED.SEGTYPE) USED_TOT
WHERE TMP_TBS.TABLESPACE_NAME = USED_TOT.TABLESPACE(+)
GROUP BY TMP_TBS.TABLESPACE_NAME, USED_TOT.SEGTYPE;