Search This Blog

Wednesday, 5 April 2017

A quick query to find top TEMP abusers in case of a query within a package

I was looking into my query library the other day and the existing query for historical temp tablespace usage I had was not satisfying my needs in case of search for a top temp abuser. I turned out to be a bug caused by DBMS_FEATURE_USAGE_INTERNAL. 



SELECT h.sql_id,
  h.TOP_LEVEL_SQL_ID,
  MAX(round(temp_space_allocated/power(1024,3),2)) MAX_USER_TEMP_GB ,
  NVL(TO_CHAR(SUBSTR(s.sql_Text, 0,3900)),'Check the TOP_LEVEL_SQL_ID: select * from SYS.DBA_HIST_SQLTEXT where sql_id='''||TOP_LEVEL_SQL_ID||''';  -- OR --  no sql_text was captured')  
FROM DBA_HIST_ACTIVE_SESS_HISTORY h
LEFT JOIN SYS.DBA_HIST_SQLTEXT s
ON h.sql_id=s.sql_id
WHERE h.sample_time > sysdate-1
  and TEMP_SPACE_ALLOCATED > (10*power(1024,3))
GROUP BY h.sql_id,
  h.TOP_LEVEL_SQL_ID,
  TO_CHAR(SUBSTR(s.sql_Text, 0,3900))
ORDER BY 3 DESC nulls last;