CPU bound queries

May 16, 2011
This can be used to find queries that use CPU heavily.  It starts by determining a baseline and then identifies those queries that use more CPU than the baseline * an arbitrary multiplier.  If you want to use this in a balanced RAC environment change the references to v$sqlarea to gv$sqlarea.  Functions are used to return topCPU and user to the mainline.

SET SERVEROUTPUT ON SIZE 1000000

DECLARE

topCPUkount                 NUMBER;
multiplier                  NUMBER     := .60;

CURSOR cpuCursor (cKount  NUMBER) IS
  SELECT *
    FROM v$sqlarea
   WHERE cpu_time > cKount;

FUNCTION topCPU RETURN NUMBER IS
num NUMBER;
BEGIN
  SELECT MAX(cpu_time)
    INTO num
    FROM v$sqlarea;
  RETURN num;
EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line(SQLERRM);
END;

FUNCTION getUser(uid    NUMBER)  RETURN VARCHAR2 IS
str VARCHAR2(30);
BEGIN
  SELECT username
    INTO str
    FROM dba_users
   WHERE user_id = uid;
  RETURN str;
EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line(SQLERRM);
END;

BEGIN
  topCPUkount := topCPU;
  dbms_output.put_line('Top CPU Count = '||topCPUkount);
  FOR i IN cpuCursor (topCPUkount * multiplier)  LOOP
    dbms_output.put_line('--------');
    dbms_output.put_line('Last Active = '||TO_CHAR(i.last_active_time,'YYYY-MM-DD HH:MI'));
    dbms_output.put_line(getUser(i.parsing_user_id));
    dbms_output.put_line(i.sql_text);
  END LOOP;
 
EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line(SQLERRM);
END;
 

Check for Locked objects

May 16, 2011
A handy script to check for locked objects in a database.

SET SERVEROUTPUT ON SIZE 1000000

DECLARE

kount           NUMBER   := 0;
objectName dba_objects.object_name%TYPE;
objectType dba_objects.object_type%TYPE;
schemaOwner dba_objects.owner%TYPE;

CURSOR lock_cursor IS
  SELECT *
    FROM gv$locked_object;

BEGIN
  FOR i IN lock_cursor LOOP
    kount := kount + 1;
    SELECT owner,object_name,object_type
      INTO schemaOwner,objectName,objectType
      FROM dba_objects
     WHERE object_id = i.object_id;
   ...
Continue reading...
 

Check Oracle Tablespace Size

May 16, 2011
A useful script for checking the size of tablespaces

SET PAGESIZE 100

COL "Tablespace" FOR a22
COL "Used MB" FOR 99,999,999
COL "Free MB" FOR 99,999,999
COL "Total MB" FOR 99,999,999

SELECT df.tablespace_name "Tablespace",
       totalusedspace "Used MB",
       (df.totalspace - tu.totalusedspace) "Free MB",
        df.totalspace "Total MB",
        round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace)) "Pct. Free"
  FROM
    (SELECT tablespace_name,
            round(sum(bytes) / 1048576) To...
Continue reading...
 

Categories

Blog Archive

 

Make a free website with Yola