Skip to Main Content
  • Questions
  • Is there any SQL dictionary view records the SQL per execution time, rather than total elapsed time?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Joe.

Asked: January 18, 2016 - 10:38 am UTC

Last updated: January 20, 2016 - 4:11 pm UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

Hi, Tom
Today, I was trying to collect the current running SQL statements which elapsed more than 10 seconds per execution. To caculate the SQL Elapsed time, I have tried some SQL dictionary views (such as gv$sql, gv$sqlarea, gv$transaction, gv$session), but the "Elapsed_time" column on these views just present the total Elapsed time for a SQL statment, rather than per executions. SO I had to divide the "Elpased time" into "Executions", like the SQL Statments shown below:

SELECT A.SID,A.SERIAL#,S.ELAPSED_TIME/1000000,executions,A.PROGRAM,S.SQL_TEXT,S.SQL_ID FROM GV$SESSION A,GV$SQL S
WHERE A.SQL_HASH_VALUE = S.HASH_VALUE
AND ROUND(ELAPSED_TIME / 1000000 / (CASE
WHEN (EXECUTIONS = 0 OR NVL(EXECUTIONS, 1 ) = 1) THEN
1
ELSE
EXECUTIONS
END),
2)> 10
AND UPPER(SQL_TEXT) LIKE 'SELECT%'
AND A.STATUS = 'ACTIVE';


Is there any SQL dictionary view records the SQL per execution time, rather than total elapsed time?

and Connor said...

Not directly, but you could use V$ACTIVE_SESSION_HISTORY to get a good idea of it. Something like

select sql_id, sql_exec_id, count(*)
from V$ACTIVE_SESSION_HISTORY
group by sql_id, sql_exec_id
having count(*) > 10

Hope this helps.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

get all the current running SQL Statement that last for more than 10 sec

Joe Huang, January 18, 2016 - 12:18 pm UTC

Thanks Connor,
This idea is good, but the V$ACTIVE_SESSION_HISTORY just stores history information, not current running SQL. What if I wanna get all the current running SQL Statement that last for more than 10 sec?
Connor McDonald
January 18, 2016 - 1:02 pm UTC

V$ACTIVE_SESSION_HISTORY *does* have current running SQL. It has active sessions right up to "now".

A SQL running for 7 seconds will have 7 rows (the most recent 7)...every second, another row will appear.

use V$ACTIVE_SESSION_HISTORY to caculate run time of current running SQL statments

Joe Huang, January 19, 2016 - 1:38 am UTC

Hi, Connor
It works! V$ACTIVE_SESSION_HISTORY does fullfill our request and solve our problem.
Thanks for your help.
Connor McDonald
January 19, 2016 - 3:24 am UTC

Glad we could help.

Just make sure that you are *licensed* for v$active_session_history :-)

what is sql_exec_id ?

Rajeshwaran Jeyabal, January 20, 2016 - 3:11 pm UTC

Connor,

What is sql_exec_id ? looked into the docs, doesn't help much.

given a sql, if it executed more than once (either from same session or from different session) does the sql_exec_id differs?

http://docs.oracle.com/database/121/REFRN/GUID-69CEA3A1-6C5E-43D6-982C-F353CD4B984C.htm#REFRN30299
Chris Saxon
January 20, 2016 - 4:11 pm UTC

Chris here. Yep, it's a counter. Tanel Poder has written more about it at:

http://blog.tanelpoder.com/2011/10/24/what-the-heck-is-the-sql-execution-id-sql_exec_id/

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.