Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

measure time of an sql statement in a procedure in plsql

I must write a procedure which save the execute time of any sql-statement in a table.

The procedure is calling by exec measuresqltime('sql statement as string');

My idea is like this:

  --declarations 
  timestart NUMBER;
  BEGIN 
    dbms_output.enable; 
    timestart:=dbms_utility.get_time(); 
    EXECUTE IMMEDIATE sql
    COMMIT; 
    dbms_output.put_line(dbms_utility.get_time()-timestart); 
    -- save time

But it didn't work for me for a SELECT *... clause. (I think sql need a INTO-order)

Is there a way to execute any sql-atatements in a procedure?

like image 458
sheepy Avatar asked Jan 20 '12 10:01

sheepy


People also ask

What is timestamp in Plsql?

For example, SYSDATE + 1 points to tomorrow. TIMESTAMP. The timestamp data type is an extension of the DATE data type. It is used to hold the year, month, hour, and second. The default timestamp format is determined by the Oracle initialization parameter NLS_TIMESTAMP_FORMAT.

How do you find the time taken to execute a query in Oracle?

Answer: For seeing the elapsed time for an individual query, you can see individual query response time in SQL*Plus with the "set timing on" command. For DBA's, Oracle has several tools for measuring system-wide response time using v$sysmetric, v$active_session_history, v$sqlarea and v$sysmetric_summary.

How does PL SQL calculate time?

The PLSQL SYSDATE function will returns current system date and time on your database. There is no any parameter or argument for the SYSDATE function. The SYSDATE function returns a date value. Note that the SYSDATE function return date and time as “YYYY-MM-DD HH:MM:SS” (string) or as YYYYMMDDHHMMSS (numeric).

How do you check when a stored procedure was last called executed in Oracle?

SELECT last_ddl_time, timestamp FROM user_objects WHERE object_type = 'PROCEDURE' AND object_name = 'OBJECT_NAME'; SQL below will tell you what day it is & it too does NOT answer OP's question.


2 Answers

If your SQL statement is a SELECT, you need to fetch from the cursor to have a meaningful measure of its execution time.

If you don't fetch from the cursor, you only measure the time spent in "parse" and "execution" phases, whereas much of the work is usually done in the "fetch" phase for SELECT statements.

You won't be able to fetch with EXECUTE IMMEDIATE or OPEN cursor FOR 'string' if you don't know the number of columns the actual statement will have. You will have to use the dynamic SQL package DBMS_SQL if the number/type of columns of the SELECT is unknown.

Here's an example:

SQL> CREATE OR REPLACE PROCEDURE demo(p_sql IN VARCHAR2) AS
  2     l_cursor  INTEGER;
  3     l_dummy   NUMBER;
  4     timestart NUMBER;
  5  BEGIN
  6     dbms_output.enable;
  7     timestart := dbms_utility.get_time();
  8     l_cursor  := dbms_sql.open_cursor;
  9     dbms_sql.parse(l_cursor, p_sql, dbms_sql.native);
 10     l_dummy := dbms_sql.execute(l_cursor);
 11     LOOP
 12        EXIT WHEN dbms_sql.fetch_rows(l_cursor) <= 0;
 13     END LOOP;
 14     dbms_sql.close_cursor(l_cursor);
 15     dbms_output.put_line(dbms_utility.get_time() - timestart);
 16  END;
 17  /

Procedure created.

SQL> exec demo('SELECT * FROM dual CONNECT BY LEVEL <= 1e6');
744

PL/SQL procedure successfully completed.

Note that this will measure the time needed to fetch to the last row of the SELECT.

like image 127
Vincent Malgrat Avatar answered Dec 04 '22 18:12

Vincent Malgrat


completing devosJava answered... avoid using it at dawn ;P

PROCEDURE MY_PROCEDURE IS
  timeStart  TIMESTAMP;
  timeEnd    TIMESTAMP;
  timeSecond NUMBER
BEGIN
  timeStart  := SYSTIMESTAMP;

  -- YOUR CODE HERE

  timeEnd    := SYSTIMESTAMP;
  timeSecond :=((extract(hour from timeEnd)*3600)+(extract(minute from timeEnd)*60)+extract(second from timeEnd))-((extract(hour from timeStart)*3600)+(extract(minute from timeStart)*60)+extract(second from timeStart));
  dbms_output.put_line('finished: '||timeSecond||' seconds');
END MY_PROC;
like image 34
Tiago Oliveira Avatar answered Dec 04 '22 18:12

Tiago Oliveira