-- sort.sql: who is using TEMP right now: sort segments and per-session usage -- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/) -- https://hashjoin.com/kb/oracle/temp-tablespace-usage/ -- -- Usage: @sort.sql set echo off prompt CURRENT ACTIVE SORTS prompt ===================== prompt from (v$sort_segment) prompt (v$session) prompt ===================== col tablespace_name format a10 set lines 132 set trims on select vsg.tablespace_name , vsg.current_users , vsg.total_extents , vsg.used_extents , vsg.extent_hits , vsg.max_used_blocks , vsg.max_sort_blocks from v$sort_segment vsg; prompt CURRENT SORT SEGMENTS prompt ===================== prompt from (v$sort_segment) prompt ===================== set pages 0 set feedback off set verify off col block_size noprint new_value x select value/1024 block_size, 'Getting DB_BLOCK_SIZE ...' from v$parameter where name='db_block_size'; set lines 155 set feedback on set pages 60 set trims on SELECT tablespace_name , extent_size*&&x ext_size_KB , total_extents*extent_size*&&x tot_tmp_KB , used_extents*extent_size*&&x used_KB , free_extents*extent_size*&&x free_KB , max_used_size*extent_size*&&x max_used_KB FROM v$sort_segment; prompt SORT usage prompt =================== prompt from (v$sort_usage) prompt =================== col sid_serial format a10 Heading "Sid,Serial" col machine format a20 trunc col program format a15 trunc col kb format 99999999999 heading "KB" col tablespace format a7 trunc heading "TS name" col extents format 99999 Heading "Ext" col idle format a8 heading "Idle" break on status skip 2 compute sum of kb on status SELECT s.username, s.sid||','||s.serial# sid_serial, TO_CHAR(s.logon_time,'mon-dd hh24:mi') logon_time, floor(last_call_et/3600)||':'|| floor(mod(last_call_et,3600)/60)||':'|| mod(mod(last_call_et,3600),60) IDLE, s.machine, s.program, s.status, u.tablespace, u.contents, u.extents, u.blocks*&&x KB FROM v$session s, v$sort_usage u WHERE s.saddr=u.session_addr; set lines 132 set pages 1000 col osuser format a10 col sid format 99999 col machine format a30 trunc col module format a20 trunc col username format a10 col serial# format 9999999 col tablespace format a4 heading "TBSP" col EXTENTS format 999999999 break on tablespace skip 2 compute sum of EXTENTS on tablespace select a.sid,a.serial#,a.osuser,a.username,a.machine,a.module,c.sql_id,c.SEGTYPE,c.EXTENTS, tablespace from gv$session a, gv$tempseg_usage c where a.SADDR=c.session_addr and a.inst_id=c.inst_id order by 1; set echo on