使用ASH监控历史会话,找出坏SQL
2015-01-18 20:37
239 查看
使用如下脚本可以监控历史会话经历过哪些等待事件,并且按照等待次数降序排列
select session_id,event,count(*),sum(time_waited) from v$active_session_history where session_state='WAITING'
and time_waited>0 and sample_time>=(sysdate-&howlongago/(24*60))
group by session_id,event order by 3 desc;
例子:
SQL> select session_id,event,count(*),sum(time_waited) from v$active_session_history where session_state='WAITING'
2 and time_waited>0 and sample_time>=(sysdate-&howlongago/(24*60))
3 group by session_id,event order by 3 desc;
输入 howlongago 的值: 5
原值 2: and time_waited>0 and sample_time>=(sysdate-&howlongago/(24*60))
新值 2: and time_waited>0 and sample_time>=(sysdate-5/(24*60))
SESSION_ID EVENT COUNT(*) SUM(TIME_WAITED)
---------- ------------------------------ ---------- ----------------
150 direct path write temp 3 20825
165 control file sequential read 2 21520
150 direct path read temp 2 38545
然后通过如下语句查询出引起该等待事件的sql_id
select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-&howlongago/(24*60)) and session_id=&sid;
SQL> select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-&howlongago/(24*60)) and session_id=&sid;
输入 howlongago 的值: 8
输入 sid 的值: 150
原值 1: select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-&howlongago/(24*60)) and session_id=&sid
新值 1: select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-8/(24*60)) and session_id=150
EVENT SESSION_ID SQL_ID P1 P2
------------------------------ ---------- ------------- ---------- ----------
P3
----------
direct path read temp 150 fqu3bkz2q6j26 201 58665
1
150 fqu3bkz2q6j26 1111838976 1
0
direct path read temp 150 fqu3bkz2q6j26 201 45897
1
EVENT SESSION_ID SQL_ID P1 P2
------------------------------ ---------- ------------- ---------- ----------
P3
----------
direct path read temp 150 fqu3bkz2q6j26 201 47753
1
再通过如下语句找到引起该等待事件的SQL
select sql_text from v$sqlarea where sql_id='&sql_id';
SQL> select sql_text from v$sqlarea where sql_id='&sql_id';
输入 sql_id 的值: fqu3bkz2q6j26
原值 1: select sql_text from v$sqlarea where sql_id='&sql_id'
新值 1: select sql_text from v$sqlarea where sql_id='fqu3bkz2q6j26'
SQL_TEXT
--------------------------------------------------------------------------------
select a.* from dict a,dict b order by 1
select session_id,event,count(*),sum(time_waited) from v$active_session_history where session_state='WAITING'
and time_waited>0 and sample_time>=(sysdate-&howlongago/(24*60))
group by session_id,event order by 3 desc;
例子:
SQL> select session_id,event,count(*),sum(time_waited) from v$active_session_history where session_state='WAITING'
2 and time_waited>0 and sample_time>=(sysdate-&howlongago/(24*60))
3 group by session_id,event order by 3 desc;
输入 howlongago 的值: 5
原值 2: and time_waited>0 and sample_time>=(sysdate-&howlongago/(24*60))
新值 2: and time_waited>0 and sample_time>=(sysdate-5/(24*60))
SESSION_ID EVENT COUNT(*) SUM(TIME_WAITED)
---------- ------------------------------ ---------- ----------------
150 direct path write temp 3 20825
165 control file sequential read 2 21520
150 direct path read temp 2 38545
然后通过如下语句查询出引起该等待事件的sql_id
select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-&howlongago/(24*60)) and session_id=&sid;
SQL> select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-&howlongago/(24*60)) and session_id=&sid;
输入 howlongago 的值: 8
输入 sid 的值: 150
原值 1: select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-&howlongago/(24*60)) and session_id=&sid
新值 1: select event, session_id,sql_id, p1,p2,p3 from v$active_session_history where sample_time>=(sysdate-8/(24*60)) and session_id=150
EVENT SESSION_ID SQL_ID P1 P2
------------------------------ ---------- ------------- ---------- ----------
P3
----------
direct path read temp 150 fqu3bkz2q6j26 201 58665
1
150 fqu3bkz2q6j26 1111838976 1
0
direct path read temp 150 fqu3bkz2q6j26 201 45897
1
EVENT SESSION_ID SQL_ID P1 P2
------------------------------ ---------- ------------- ---------- ----------
P3
----------
direct path read temp 150 fqu3bkz2q6j26 201 47753
1
再通过如下语句找到引起该等待事件的SQL
select sql_text from v$sqlarea where sql_id='&sql_id';
SQL> select sql_text from v$sqlarea where sql_id='&sql_id';
输入 sql_id 的值: fqu3bkz2q6j26
原值 1: select sql_text from v$sqlarea where sql_id='&sql_id'
新值 1: select sql_text from v$sqlarea where sql_id='fqu3bkz2q6j26'
SQL_TEXT
--------------------------------------------------------------------------------
select a.* from dict a,dict b order by 1
相关文章推荐
- 使用ASH监控历史会话,找出坏SQL
- 使用ASH监控历史会话,找出坏SQL
- 使用Oracle自带的系统包和过程监控其它会话SQL语句的执行计划等信息
- 如何监控并找出系统中存在的大量的未使用绑定变量的SQL
- 使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话
- 监控和剖析数据库操作 -- P6Spy、SQL Profiler、IronTrack SQL 使用简介
- ASH(Active Session History)活动会话历史说明
- 监控和剖析数据库操作 -- P6Spy、SQL Profiler、IronTrack SQL 使用简介
- 演示使用sql_trace和10046事件对其他会话进行跟踪,并给出trace结果
- 使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话
- MySQLSniffer第三方SQL语句监控工具的安装使用
- SQL脚本:监控当前重做日志文件使用情况
- 使用rlwrap上下翻sqlplus历史命令
- 使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话
- SqlServer中使用T-sql找出identity列
- 使用 Red Gate SQL Response监控SQL SERVER
- SQL*plus中,使用上下键,调出历史命令
- 使用T-SQL查看SQL Server代理作业的历史记录
- 监控和剖析数据库操作 -- P6Spy、SQL Profiler、IronTrack SQL 使用简介
- linux下使用rlwrap调用sqlplus中历史命令