分享

Oracle 临时表空间 SQL语句

 LuciferLiu 2021-12-10

以下总结了关于 Oracle 数据库临时表空间的相关 SQL 语句:

Oracle 临时表空间创建和添加数据文件:

--创建临时表空间 tempdatacreate temporary tablespace tempdata tempfile '/oradata/orcl/tempdata01.dbf' size 30g autoextend off;--新增临时表空间数据文件alter tablespace tempdata add tempfile '/oradata/orcl/tempdata02.dbf' size 30g autoextend off;--删除临时表空间数据文件alter tablespace tempdata drop tempfile '/oradata/orcl/tempdata02.dbf' including datafiles;--调整临时表空间数据文件大小alter database tempfile '/oradata/orcl/tempdata01.dbf' resize 2G;--设置自动扩展alter database tempfile '/oradata/orcl/tempdata01.dbf' autoextend on;--切换默认临时表空间alter database default temporary tablespace tempdata;--删除临时表空间drop tablespace temp including contents and datafiles cascade constraints;--收缩临时表空间alter tablespace temp shrink space keep 8G;alter tablespace temp shrink tempfile '/oradata/orcl/tempdata01.dbf';

查看当前默认临时表空间:

SELECT PROPERTY_NAME, PROPERTY_VALUE FROM DATABASE_PROPERTIES WHERE PROPERTY_NAME='DEFAULT_TEMP_TABLESPACE';

查询temp表空间使用率:

select  df.tablespace_name "Tablespace",   df.totalspace "Total(MB)",   nvl(FS.UsedSpace, 0)  "Used(MB)",   (df.totalspace - nvl(FS.UsedSpace, 0)) "Free(MB)",   round(100 * (1-( nvl(fs.UsedSpace, 0) / df.totalspace)), 2) "Pct. Free(%)"FROM  (SELECT tablespace_name, round(SUM(bytes) / 1048576) TotalSpaceFROM   dba_TEMP_filesGROUP  BY tablespace_name) df,   (SELECT tablespace_name, ROUND(SUM(bytes_used) / 1024 / 1024)  UsedSpaceFROM   gV$temp_extent_poolGROUP  BY tablespace_name) fs  WHERE  df.tablespace_name = fs.tablespace_name(+)

查看临时表空间对应的临时文件的使用情况:

SELECT TABLESPACE_NAME         AS TABLESPACE_NAME    ,BYTES_USED/1024/1024/1024    AS TABLESAPCE_USED  ,BYTES_FREE/1024/1024/1024  AS TABLESAPCE_FREEFROM V$TEMP_SPACE_HEADERORDER BY 1 DESC;

查询实时使用temp表空间的sql_id和sid:

set linesize 260 pagesize 1000col machine for a40
col program for a40SELECT se.username,   sid,   serial#,   se.sql_id
       machine,   program,   tablespace,   segtype,   (su.BLOCKS*8/1024/1024) GB  FROM v$session se, v$sort_usage su WHERE se.saddr = su.session_addr order by su.BLOCKS desc;/*需要注意的是这里查询sql_id要用v$session视图的sql_id,而不要用v$sort_usage视图的sql_id,v$sort_usage视图里面的sql_id是不准确的*/

查询历史的temp表空间的使用的SQL_ID:

select a.SQL_ID,   a.SAMPLE_TIME,   a.program,   sum(trunc(a.TEMP_SPACE_ALLOCATED / 1024 / 1024)) MB  from v$active_session_history a where TEMP_SPACE_ALLOCATED is not null 
 and sample_time between
 to_date('&date1', 'yyyy-mm-dd hh24:mi:ss') and
 to_date('&date2', 'yyyy-mm-dd hh24:mi:ss')
 group by a.sql_id,a.SAMPLE_TIME,a.PROGRAM order by 2 asc,4 desc;

本次分享到此结束啦~

如果觉得文章对你有帮助,点赞、收藏、关注、评论,一键四连支持,你的支持就是我创作最大的动力。

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多