Friday, July 12, 2013

SQL currently using the most resources

While investigating performance issue, we may need to find out the sql which is most resource intensive. Below is the script, this will produce the list on resource intensive sqls currently running in you database.

Here goes script as;

select active_session_history.user_id,
dba_users.username,
sqlarea.sql_text,
sqlarea.sql_id,
sum(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
from v$active_session_history active_session_history,
v$sqlarea sqlarea,
dba_users
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
and active_session_history.sql_id = sqlarea.sql_id
and active_session_history.user_id = dba_users.user_id
group by active_session_history.user_id,sqlarea.sql_text, dba_users.username,sqlarea.sql_id
order by ttl_wait_time desc;

Example,

The output you will be getting as

USER_IDUSERNAMESQL_TEXTSQL_IDTTL_WAIT_TIME
0SYS"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"gm9t6yefb1yu62,802,675
331ASHISH"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"cb7vqpgadycjp492,902
322OTHERS"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"8tyhx4x2x47ga485,157
322OTHERS"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"4d3dhvjvuf880454,847
322OTHERS"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"4fq3fkixrsp9d432,484
322OTHERS"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"3t3umn6pod3ds142,767
322OTHERS"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"rs0tel46zczna98,390


The above output will provide you the sql which is most resource extensive. Also you get sql_id for your further investigation.

No comments:

Post a Comment