Search notes:

Oracle SQL Statement Execution: Diff V$SQL Statistics

The following statement gets some execution stats from a (preferably running) SQL statement from v$sql and and then gets the same performance indicators after an interval of time (time_diff) again and then reports the stats and their differences.
with
   function sleep(secs number) return number is
   begin
      dbms_session.sleep(secs);
      return 0;
   end sleep;
   param as (
       select
         'b63vag62z3h9x' as sqlid,
          0              as chnr,
          10             as time_diff
      from
         dual
   ),  
   slowdown as (
     select
        level lvl,
        dbms_utility.get_time tm
     from
        param connect by sleep(time_diff) = 0 and level <= 2
  ),
  stats as ( 
select
   stat.*
from
   param, slowdown, lateral (
        select lvl, executions, fetches, parse_calls, buffer_gets, rows_processed, cpu_time, elapsed_time from v$sql where sql_id = param.sqlid and child_number = param.chnr and lvl = 1 union all
        select lvl, executions, fetches, parse_calls, buffer_gets, rows_processed, cpu_time, elapsed_time from v$sql where sql_id = param.sqlid and child_number = param.chnr and lvl = 2
   ) stat
)
select * from stats
union all
select
   3,
   max(case when stats.lvl = 2 then executions     end) - max(case when stats.lvl = 1 then executions     end),
   max(case when stats.lvl = 2 then fetches        end) - max(case when stats.lvl = 1 then fetches        end),
   max(case when stats.lvl = 2 then parse_calls    end) - max(case when stats.lvl = 1 then parse_calls    end),
   max(case when stats.lvl = 2 then buffer_gets    end) - max(case when stats.lvl = 1 then buffer_gets    end),
   max(case when stats.lvl = 2 then rows_processed end) - max(case when stats.lvl = 1 then rows_processed end),
   max(case when stats.lvl = 2 then cpu_time       end) - max(case when stats.lvl = 1 then cpu_time       end),
   max(case when stats.lvl = 2 then elapsed_time   end) - max(case when stats.lvl = 1 then elapsed_time   end)
from
   stats
/

See also

Tracing SQL statement execution

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758198433, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/SQL/statement/execution/diff-stats(80): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78