Tuesday, January 29, 2013

Finding the SQL that is chewing up all the Temp Tablespace

Here is a script that will do just that.  Enjoy.
Thx

***************
REM: tempfindsql.sql


create or replace view tempspfindsql_view
as
SELECT   S.sid,S.serial#,S.username,S.osuser,P.spid,S.module,S.program,
                 SUM (U.blocks) * T.block_size / 1024 / 1024 mb_used, U.tablespace,
                 COUNT(*) sort_ops
  FROM      sys.v$sort_usage U, sys.v$session S, sys.dba_tablespaces T, sys.v$process P
  WHERE    U.session_addr = S.saddr
  AND      S.paddr = P.addr
  AND      U.tablespace = T.tablespace_name
  GROUP BY S.sid, S.serial#, S.username, S.osuser, P.spid, S.module,
           S.program, T.block_size, U.tablespace
/

create or replace view tsql1_view
as
select a.username,a.osuser,a.sid,a.SERIAL#,a.sql_id,substr(sql_text,1,74) as sql_text
      ,decode(command ,1,  'CrTab'
                      ,2,  'Ins'
                      ,3,  'Sel'
                      ,4,  'CrClus'
                      ,5,  'AltClus'
                      ,6,  'Up'
                      ,7,  'Del'
                      ,8,  'Drop'
                      ,9,  'CrInd'
                      ,10, 'DrInd'
                      ,11, 'AltInd'
                      ,12, 'DrTab'
                      ,13, '---'
                      ,14, '---'
                      ,15, 'AltTab'
                      ,16, '---'
                      ,17, 'Grant'
                      ,18, 'Revoke'
                      ,19, 'CrSyn'
                      ,20, 'DrSyn'
                      ,21, 'CrVi'
                      ,22, 'DrVi'
                      ,23, '---'
                      ,24, '---'
                      ,25, '---'
                      ,26, 'LkTab'
                      ,27, 'NoOper'
                      ,28, 'Rena'
                      ,29, 'Com'
                      ,30, 'Aud'
                      ,31, 'Noaud'
                      ,32, 'CrExtDb'
                      ,33, 'DrExtDb'
                      ,34, 'CrDb'
                      ,35, 'AltDb'
                      ,36, 'CrRolbSeg'
                      ,37, 'AltRolbSeg'
                      ,38, 'DrRolbSeg'
                      ,39, 'CrTsp'
                      ,40, 'AltTsp'
                      ,41, 'DrTsp'
                      ,42, 'AltSes'
                      ,43, 'AltUsr'
                      ,44, 'Commit'
                      ,45, 'Rolbk'
                      ,46, 'Savpo'
                      ,62, 'AnaTab'
                      ,    'Unknown') as command
from sys.v$session a,sys.v$sqlarea b
where b.address = a.sql_address
/

create or replace view tempfind_view
as
select a.sql_id,sum(b.mb_used) as Tot_MBs_Used
from   tsql1_view a, tempspfindsql_view b
where a.sid = b.sid
and   a.serial#=b.serial#
group by a.sql_id
order by a.sql_id;

set linesize 132

column Tot_MBs_Used format 999,999,999,999
column sql_text format a82

spool Temp_Space_SQL.out

select distinct a.sql_id,a.Tot_MBs_Used,b.sql_text
from tempfind_view a, tsql1_view b
where  a.sql_id = b.sql_id
order by Tot_MBs_Used;

spool off

*******************

Here is a sample of the out put:

SQL_ID            TOT_MBS_USED SQL_TEXT
------------- ---------------- ----------------------------------------------------------------------------------
2b064ybzkwf1y                1 BEGIN EMD_NOTIFICATION.QUEUE_READY(:1, :2, :3); END;