登上数据库服务器后,第一个就是通过系统命令确认下CPU、内存、I/O是否异常,每个系统的命令不一样,常见的有top、topas、vmstat、iostat。
第二步就是连到数据库查看活动的等待事件,这是监控、巡检、诊断数据库最基本的手段,通常81%的问题都可以通过等待事件初步定为原因,它是数据库运行情况最直接的体现,如下脚本是查看每个等待事件的个数、等待时长,并排除了一些常见的IDLE等待事件。
--wait_event
col event for a45
SELECT inst_id,EVENT, SUM(DECODE(WAIT_TIME, 0, 0, 1)) "Prev", SUM(DECODE(WAIT_TIME, 0, 1, 0)) "Curr", COUNT(*) "Tot" , sum(SECONDS_IN_WAIT) SECONDS_IN_WAIT
FROM GV$SESSION_WAIT
WHERE event NOT
IN ('smon timer','pmon timer','rdbms ipc message','SQL*Net message from client','gcs remote message')
AND event NOT LIKE '%idle%'
AND event NOT LIKE '%Idle%'
AND event NOT LIKE '%Streams AQ%'
GROUP BY inst_id,EVENT
ORDER BY 1,5 desc
这里就需要掌握一些常见异常等待事件的原因,并形成条件反射,比如library cache lock、read by other session、row cache lock、buffer busy waits、latch:shared pool、gc buffer busy、cursor: pin S on X、direct path read、log file sync、enq: TX - index contention、PX Deq Credit: send blkd、latch free、enq: TX - row lock contention等等,如果异常等待事件的个数和等待时间很长,那么排查原因的入口就在这里。
得到异常等待事件之后,我们就根据等待事件去查会话详情,也就是查看哪些会话执行哪些SQL在等待,另外还查出来用户名和机器名称,以及是否被阻塞。另外如下脚本可改写成根据用户查会话、根据SQL_ID查会话等等。
-- wait_event
col event for a45
SELECT inst_id,EVENT, SUM(DECODE(WAIT_TIME, 0, 0, 1)) "Prev", SUM(DECODE(WAIT_TIME, 0, 1, 0)) "Curr", COUNT(*) "Tot" , sum(SECONDS_IN_WAIT) SECONDS_IN_WAIT
FROM GV$SESSION_WAIT
WHERE event NOT
IN ('smon timer','pmon timer','rdbms ipc message','SQL*Net message from client','gcs remote message')
AND event NOT LIKE '%idle%'
AND event NOT LIKE '%Idle%'
AND event NOT LIKE '%Streams AQ%'
GROUP BY inst_id,EVENT
ORDER BY 1,5 desc
得到会话列表之后,可以根据如下SQL查询某个会话的详细信息,如上次个执行的SQL_ID,登录时间等,该SQL也可改写成多个。
-- session_by_sid
SELECT s.sid, s.serial#, spid, event, sql_id, PREV_SQL_ID, seconds_in_wait ws, row_wait_obj# obj,
s.username, s.machine, module,blocking_session b_sess,logon_time
FROM v$session s, v$process p
WHERE sid = '&sid' AND s.paddr = p.addr
从前面两个SQL都可以看到会话等待的对象ID,可以通过如下SQL查询对象的详细信息。
-- session_by_sid
SELECT s.sid, s.serial#, spid, event, sql_id, PREV_SQL_ID, seconds_in_wait ws, row_wait_obj# obj,
s.username, s.machine, module,blocking_session b_sess,logon_time
FROM v$session s, v$process p
WHERE sid = '&sid' AND s.paddr = p.addr
根据SQL_ID、HASH_VALUE查询SQL语句。如果v$sqlarea中查不到,可以尝试DBA_HIST_SQLTEXT视图中查询。
-- sql_text
select sql_id,SQL_fullTEXT
from v$sqlarea
where (sql_id='&sqlid' or hash_value=to_number('&hashvale') )
and rownum<2
关于SQL语句的执行计划、对象的统计信息、性能诊断、跟踪SQL等这里就不展开,后面计划出一个类似的系列,敬请关注。
通过如下SQL查询某个会话阻塞了多少个会话。
-- blocking_sess
select count(*),blocking_session
from v$session
where blocking_session is not null
group by blocking_session;
通过如下SQL查询某个会话的锁,有哪些TM、TX锁,以及会话和锁关联查询的SQL,注意这里指定了ctime大于100秒,30%的情况是人为误操作锁表,导致应用SQL被阻塞,无法运行。
-- lock
set linesize 180
col username for a15
col owner for a15
col OBJECT_NAME for a30
col SPID for a10
--查询某个会话的锁
select /*+rule*/SESSION_ID,OBJECT_ID,ORACLE_USERNAME,OS_USER_NAME,PROCESS,LOCKED_MODE
from gv$locked_object where session_id=&sid;
--查询TM、TX锁
select /*+rule*/* from v$lock
where ctime >100 and type in ('TX','TM') order by 3,9;
--查询数据库中的锁
select /*+rule*/s.sid,p.spid,l.type,round(max(l.ctime)/60,0) lock_min,s.sql_id,s.USERNAME,b.owner,b.object_type,b.object_name
from v$session s, v$process p,v$lock l,v$locked_object o,dba_objects b
where o.SESSION_ID=s.sid and s.sid=l.sid and o.OBJECT_ID=b.OBJECT_ID
and s.paddr = p.addr and l.ctime >100 and l.type in ('TX','TM','FB')
group by s.sid,p.spid,l.type,s.sql_id,s.USERNAME,b.owner,b.object_type,b.object_name
order by 9,1,3
有的问题可能需要分析很长时间,或者是需要外部人员协助分析,那么保留现场证据就非常重要了,下面脚本是systemstate dump和hanganalyze步骤,如果有sqlplus无法登陆的情况,可以加-prelim参数。
--systemstate dump
sqlplus -prelim / as sysdba
oradebug setmypid
oradebug unlimit;
oradebug dump systemstate 266;
--wait for 1 min
oradebug dump systemstate 266;
--wait for 1 min
oradebug dump systemstate 266;
oradebug tracefile_name;
--hanganalyze
oradebug setmypid
oradebug unlimit;
oradebug dump hanganalyze 3
--wait for 1 min
oradebug dump hanganalyze 3
--wait for 1 min
oradebug dump hanganalyze 3
oradebug tracefile_name
通常情况下,初步定为问题后为了快速恢复业务,需要去杀掉某些会话,特别是批量杀会话,有时还会直接kill所有LOCAL=NO的进程,再杀会话时一定要检查确认,更不能在别的节点或者别的服务器上执行。
-- kill_sess
set line 199
col event format a35
--杀某个SID会话
SELECT /*+ rule */ sid, s.serial#, 'kill -9 '||spid, event, blocking_session b_sess
FROM v$session s, v$process p WHERE sid='&sid' AND s.paddr = p.addr order by 1;
--根据SQL_ID杀会话
SELECT /*+ rule */ sid, s.serial#, 'kill -9 '||spid, event, blocking_session b_sess
FROM v$session s, v$process p WHERE sql_id='&sql_id' AND s.paddr = p.addr order by 1;
--根据等待事件杀会话
SELECT /*+ rule */ sid, s.serial#, 'kill -9 '||spid, event, blocking_session b_sess
FROM v$session s, v$process p WHERE event='&event' AND s.paddr = p.addr order by 1;
--根据用户杀会话
SELECT /*+ rule */ sid, s.serial#, 'kill -9 '||spid, event, blocking_session b_sess
FROM v$session s, v$process p WHERE username='&username' AND s.paddr = p.addr order by 1;
--kill所有LOCAL=NO进程
ps -ef|grep LOCAL=NO|grep $ORACLE_SID|grep -v grep|awk '{print $2}' |xargs kill -9
如需要修改静态参数、内存等问题,需要重启数据库,(不要觉得重启很LOW,在很多情况下为了快速恢复业务经常使用这个从网吧里传出来的绝招),记住千万不要在这个时候死磕问题原因、当作课题研究,我们的首要任务是恢复业务。
tail -f alert_.log
alter system checkpoint;
alter system switch logfile;
shutdown immediate;
startup
转载来源:https://mp.weixin.qq.com/s/T0V7rgd-BGL2fhzKd-nD5A