Home > Ask the Oracle Experts > (Archive) App Dev: PL/SQL and XML Questions & Answers > Log execution time for a procedure
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Log execution time for a procedure

Lewis Cunningham EXPERT RESPONSE FROM: Lewis Cunningham

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 13 March 2006
Hi. I need to somehow log the execution time for two procedures every time they are called and save that data (in a table?). Do you know of a way to isolate tracing to a specific procedure in order to do this? Thanks!

>
EXPERT RESPONSE

It really depends on what you are trying to do. You can use DBMS_APPLICATION_INFO if you are running a long procedure and just want to see what it's doing. If you really need to log run times, you can write a simple program to do that.

First, create a log table:

CREATE TABLE PROC_LOG
(
  PROC_INSTANCE  NUMBER                         NOT NULL,
  PROC_NAME      VARCHAR2(100)                  NOT NULL,
  IN_TIME        TIMESTAMP(6)                   NOT NULL,
  OUT_TIME       TIMESTAMP(6),
  COMMENTS       VARCHAR2(1000),
  PRIMARY KEY (PROC_INSTANCE)
)
/
And a sequence:
CREATE SEQUENCE PROC_INSTANCE
/

In the table, proc_instance is a sequence generated primary key so that you can identify unique runs of your procedure. Proc name can be the actual procedure you are running or a made up process name. In_time is the date and time that the run started and out_time is the date and time it ended. Comments are an optional clear text field.

And now a log package:

CREATE OR REPLACE PACKAGE proclog_pkg AS
  FUNCTION init_log( p_proc_name IN VARCHAR2, 
                     p_comments IN VARCHAR2 DEFAULT NULL )
      RETURN proc_log.proc_instance%TYPE;
  
  PROCEDURE end_log( p_proc_instance IN proc_log.proc_instance%TYPE );
  
END;
/

Init_log starts a record and end_log finishes it. See the package body:

CREATE OR REPLACE PACKAGE BODY proclog_pkg AS
  FUNCTION init_log( p_proc_name IN VARCHAR2, 
                     p_comments IN VARCHAR2 DEFAULT NULL )
     RETURN proc_log.proc_instance%TYPE AS
     
    v_proc_instance proc_log.proc_instance%TYPE;
  BEGIN
    INSERT INTO proc_log
   (proc_instance, proc_name, in_time, comments)
      VALUES ( proc_instance.nextval, 
               p_proc_name,
               localtimestamp,
               p_comments )
      RETURNING proc_instance INTO v_proc_instance;
      
    RETURN v_proc_instance;
      
  END;
               
  PROCEDURE end_log( p_proc_instance IN proc_log.proc_instance%TYPE ) AS
  BEGIN
    UPDATE proc_log
      SET out_time = localtimestamp
      WHERE proc_instance = p_proc_instance;
  END;
  
END;
/

You run it like this:

DECLARE
  v_proc_instance proc_log.proc_instance%TYPE;
  
  i NUMBER := 0;
  i_am_done BOOLEAN := FALSE;
BEGIN

  -- Start logging
  v_proc_instance := proclog_pkg.init_log('My Package Name', 'This is a comment' );
  
  -- Run some random code
  LOOP
    EXIT WHEN i_am_done;
    i := i + 1;
    i_am_done := i > 10000;
  END LOOP;  
  
  -- End logging
  proclog_pkg.end_log( v_proc_instance );
END;
/

Now you can select from the proc_log table and see your values.

If you want it to always commit, you can modify the procedures by using pragma autonomous_transaction and always commit in this package.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
(Archive) App Dev: PL/SQL and XML
Detect if a column is NUMBER or VARCHAR2 in PL/SQL
Loading data into Oracle
Hiding objects in a schema
Query for five most recent dates
Connecting with Visual Basic
SQL ANSI standards and compliance
Developing a parser for recognizing HTML tags
Oracle and SOAP
Concatenating XML fragments
Two schemas on a single database

Oracle PL/SQL
Oracle's free SQL Developer adds database migration tool
Confused about Oracle certification exams
ORA-01422 error when procedure returns more than one row
Calling procedure inside another procedure in anonymous block
How to import comma-delimited text file to Oracle table
Oracle updates Microsoft developer tools
PLS-00103 errors
PL/SQL do's and don't's: Five questions with Steven Feuerstein
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers

Oracle stored procedures
Stored procedure to autotransfer data between Oracle servers
Insufficient privileges error when creating stored procedure
ORA-01422 error when procedure returns more than one row
Calling procedure inside another procedure in anonymous block
Can I make a second connection to Oracle without losing the first?
How to create an index using a procedure in Oracle
Oracle updates Microsoft developer tools
Procedure compiles but does not execute
Tool in Oracle 10g to keep track of execution of queries
ORA-04082: NEW or OLD references not allowed in table level triggers

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
PL/SQL  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts