使用ASH监控历史会话,找出坏SQL

使用如下脚本可以监控历史会话经历过哪些等待事件,并且按照等待次数降序排列

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

你可能感兴趣的:(sql,session,脚本,File,Path)