首先,应用反映数据库有问题的时候,需要先查一下数据库当前活动会话都在干什么。
然后根据最大的等待事件,判断下一步,比如top 事件是db file sequential read、db file scattered read、direct path read等io事件,则要查一查系统io情况。
比如是一些enq tx row lock contention、enq index contention等并发事件,则该紧急杀锁源就杀锁源,再研究具体应用问题。
----v$session
select *from (select a.event, count(*) zs
from gv$session a
where a.STATUS = 'ACTIVE'
and a.WAIT_CLASS# <> 6
group by a.EVENT) b
order by b.zs desc
如果要查看历史一段时间,数据库都在干什么,则要用到ash了,sql如下:
---ashselect *
from (select event,machine,sql_id, count(*) zs
from dba_hist_active_sess_history a
where sample_time between
to_date('20170913 09:10:00', 'yyyymmdd hh24:mi:ss') and
to_date('20170913 09:30:00', 'yyyymmdd hh24:mi:ss')
and a.wait_class <> 'idle'
group by event,machine,sql_id) b
order by b.zs desc
紧急处理的时候,有时候往往是个别应用代码存在问题,杀会话解决了不了问题,因为问题sql还会源源不断连上来,这就需要找到问题的代码源。
------v$session,machineselect *
from (select a.event,machine, count(*) zs
from v$session a
where a.STATUS = 'ACTIVE'
and a.WAIT_CLASS# <> 6
group by a.EVENT,machine) b
order by b.zs desc
当系统真的遇到资源达到瓶颈,比如cpu、io等,需要紧急停不重要的应用,则需要根据源进行批量kill了。
-----kill machineselect 'alter system kill session '||''''||sid||','||serial#||''''||';'
from v$session
where machine = 'cs_app;
遇到数据库层面kill 不掉了,就需要操作系统kill -9了。
------os killseletopct 'kill -9 ' || b.SPID
from v$session a, v$process b
where a.PADDR = b.ADDR
and a.machine = 'cs_app'