Oracle : Important Queries for Checking Temp Usage.

Share via:

Checking Default_Temp_Tablespace : 

col property_name for a35
col property_value for a20
col description for a45

set linesize 300

SELECT * FROM DATABASE_PROPERTIES where PROPERTY_NAME=’DEFAULT_TEMP_TABLESPACE’;

 

Temp tablespace usage :

set linesize 300

SELECT A.tablespace_name tablespace, D.mb_total,
SUM (A.used_blocks * D.block_size) / 1024 / 1024 mb_used,
D.mb_total – SUM (A.used_blocks * D.block_size) / 1024 / 1024 mb_free
FROM v$sort_segment A,
(
SELECT B.name, C.block_size, SUM (C.bytes) / 1024 / 1024 mb_total
FROM v$tablespace B, v$tempfile C
WHERE B.ts#= C.ts#
GROUP BY B.name, C.block_size
) D
WHERE A.tablespace_name = D.name
GROUP by A.tablespace_name, D.mb_total;

Checking Temp Usage for Sessions :

set lines 232 pages 1000

column username format a15
column osuser format a10
column module format a35
column program format a20
column Tablespace format a10

select * from (SELECT S.sid ,S.serial# , S.username, S.osuser, S.module,s.SQL_ID,
P.program, SUM (T.blocks) * TBS.block_size / 1024 / 1024 mb_used, T.tablespace,
COUNT(*) statements
FROM v$sort_usage T, v$session S, dba_tablespaces TBS, v$process P
WHERE T.session_addr = S.saddr
AND S.paddr = P.addr
AND T.tablespace = TBS.tablespace_name
GROUP BY S.sid, S.serial#, S.username, S.osuser,s.sql_id,P.spid, S.module,
P.program, TBS.block_size, T.tablespace
ORDER BY username) where mb_used > 20; — Here it is more than 20 MB

 

Checking Temp tablespace usage with SQL ID + Status of Sessions : (Please Check below for example) 

SET TERMOUT OFF;
COLUMN current_instance NEW_VALUE current_instance NOPRINT;
SELECT rpad(instance_name, 17) current_instance FROM v$instance;
SET TERMOUT ON;

PROMPT
PROMPT +————————————————————————+
PROMPT | Report : Temporary Sort Users |
PROMPT | Instance : &current_instance |
PROMPT +————————————————————————+

SET ECHO OFF
SET FEEDBACK 6
SET HEADING ON
SET LINESIZE 180
SET PAGESIZE 50000
SET TERMOUT ON
SET TIMING OFF
SET TRIMOUT ON
SET TRIMSPOOL ON
SET VERIFY OFF

CLEAR COLUMNS
CLEAR BREAKS
CLEAR COMPUTES

COLUMN instance_name FORMAT a9 HEADING ‘Instance’
COLUMN tablespace_name FORMAT a15 HEADING ‘Tablespace Name’
COLUMN sid FORMAT 99999 HEADING ‘SID’
COLUMN serial_id FORMAT 99999999 HEADING ‘Serial ID’
COLUMN session_status FORMAT a9 HEADING ‘Status’
COLUMN oracle_username FORMAT a18 HEADING ‘Oracle User’
COLUMN os_username FORMAT a18 HEADING ‘O/S User’
COLUMN os_pid FORMAT a8 HEADING ‘O/S PID’
COLUMN session_terminal FORMAT a10 HEADING ‘Terminal’ TRUNC
COLUMN session_machine FORMAT a30 HEADING ‘Machine’ TRUNC
COLUMN session_program FORMAT a20 HEADING ‘Session Program’ TRUNC
COLUMN contents FORMAT a9 HEADING ‘Contents’
COLUMN extents FORMAT 999,999,999 HEADING ‘Extents’
COLUMN blocks FORMAT 999,999,999 HEADING ‘Blocks’
COLUMN bytes FORMAT 999,999,999,999 HEADING ‘Bytes’
COLUMN segtype FORMAT a12 HEADING ‘Segment Type’

BREAK ON instance_name SKIP PAGE

SELECT
i.instance_name instance_name
, t.tablespace tablespace_name
, s.sid sid
, s.serial# serial_id
, s.status session_status
, s.username oracle_username
, s.osuser os_username
, p.spid os_pid
, s.program session_program
, t.contents contents
, t.segtype segtype
, (t.blocks * c.value) bytes
FROM
gv$instance i
, gv$session s
, gv$process p
, gv$sort_usage t
, (select value from v$parameter
where name = ‘db_block_size’) c
WHERE
s.inst_id = p.inst_id
AND p.inst_id = i.inst_id
AND t.inst_id = i.inst_id
AND s.inst_id = i.inst_id
AND s.saddr = t.session_addr
AND s.paddr = p.addr
ORDER BY
i.instance_name
, s.sid;

 

 

Adding Temp file to Temporary tablespace : (Analyze before adding space to temp tablespace)

alter tablespace  temp add tempfile ‘+DATA012344’ size 3g(semicolon)

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (17 votes, average: 5.00 out of 5)
Loading...

3 thoughts on “Oracle : Important Queries for Checking Temp Usage.

Add Comment