1、模拟一个会话阻塞的场景。
通过update 同一行数据达到模拟阻塞的效果 :
SQL> create table t1 (id number ,name varchar2(20)) ;
SQL> insert into t1 values(1,'huyi') ;
已创建 1 行。
SQL> insert into t1 values(2,'zhanges');
已创建 1 行。
SQL> select * from scott.t1 ;
ID NAME
---------- --------------------
1 huyi
2 zhanges
SQL> update scott.t1 set name='huyi2' where id=1;
已更新 1 行。
注意:未提交
新开一个窗口:
ora11g-> sqlplus "/ as sysdba"
SQL*Plus: Release 11.2.0.4.0 Production on 星期五 10月 19 08:16:26 2018
Copyright (c) 1982, 2013, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> update scott.t1 set name='huyi3' where id=1; 产生等待
33
1
SQL> create table t1 (id number ,name varchar2(20)) ;
2
SQL> insert into t1 values(1,'huyi') ;
3
已创建 1 行。
4
SQL> insert into t1 values(2,'zhanges');
5
已创建 1 行。
6
7
8
SQL> select * from scott.t1 ;
9
10
ID NAME
11
---------- --------------------
12
1 huyi
13
2 zhanges
14
SQL> update scott.t1 set name='huyi2' where id=1;
15
16
已更新 1 行。
17
18
注意:未提交
19
20
新开一个窗口:
21
ora11g-> sqlplus "/ as sysdba"
22
23
SQL*Plus: Release 11.2.0.4.0 Production on 星期五 10月 19 08:16:26 2018
24
25
Copyright (c) 1982, 2013, Oracle. All rights reserved.
26
27
连接到:
28
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
29
With the Partitioning, OLAP, Data Mining and Real Application Testing options
30
31
SQL> update scott.t1 set name='huyi3' where id=1; 产生等待
32
33
2、利用ASH视图分析,定位到阻塞源会话,并解除阻塞。
分思路:1.找到某个大至时间范围 即:什么时间
2.找到这个时间段的等待事件:产生原因
3.找到是谁导致了阻塞 : 阻塞源
4.源会话分析
5.阻塞关系分析
SQL> alter session set NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS' ;
会话已更改。
1.找到某个大至时间范围 即:什么时间
SQL> select trunc(sample_time,'mi'),count(1) from gv$active_session_history group by trunc(sample_time,'mi') order by 1 ;
TRUNC(SAMPLE_TIME COUNT(1)
----------------- ----------
20181019 07:56:00 1
20181019 07:57:00 3
20181019 07:58:00 1
20181019 08:00:00 3
20181019 08:01:00 1
20181019 08:03:00 1
20181019 08:06:00 2
20181019 08:09:00 3
20181019 08:11:00 2
20181019 08:13:00 1
20181019 08:15:00 2
20181019 08:17:00 59 #这里等待时事明显突然增多
20181019 08:18:00 64
20181019 08:19:00 63
20181019 08:20:00 60
20181019 08:21:00 62
20181019 08:22:00 60
20181019 08:23:00 60
20181019 08:24:00 61
20181019 08:25:00 60
20181019 08:26:00 61
20181019 08:27:00 62
2. 找出某个等待事件 :
SQL> select trunc(sample_time,'mi'),event,count(1) from gv$active_session_history
2 where
3 sample_time >=to_date('20181019 08:14:00','yyyymmdd hh24:mi:ss')
4 and sample_time <=to_date('20181019 08:18:00','yyyymmdd hh24:mi:ss')
5 group by trunc(sample_time,'mi') ,event order by 1 ;
TRUNC(SAMPLE_TIME EVENT COUNT(1)
----------------- ----------------------------------- ----------
20181019 08:15:00 1
20181019 08:15:00 log file parallel write 1
20181019 08:17:00 enq: TX - row lock contention 59 #根据时间范围找出等待事件
3. 找出会话源头:
col sample_time for a20
col final_block for 99999999
col sid_chain for a15
col event_chain for a35
with ash as (select instance_number,session_id,event,blocking_session,program,to_char(sample_time,'YYYYMMDD HH24MISS') SAMPLE_TIME,sample_id,blocking_inst_id
from dba_hist_active_sess_history
where sample_time >to_date('20181019 08:14:00','yyyymmdd hh24:mi:ss')
and sample_time <to_date('20181019 08:18:00','yyyymmdd hh24:mi:ss')
)
select * from (
select sample_time,blocking_session final_block,sys_connect_by_path(session_id,',') sid_chain,
sys_connect_by_path(event,',') event_chain
from ash start with session_id is not null
connect by prior blocking_session=session_id and
prior instance_number=blocking_inst_id and sample_id = prior sample_id) a
where instr(sid_chain,final_block)=0 and not exists
(select 1 from ash b where a.final_block=b.session_id and b.blocking_session is not null)
order by sample_time ;
SAMPLE_TIME FINAL_BLOCK SID_CHAIN EVENT_CHAIN
-------------------- ----------- --------------- -----------------------------------
20181019 081703 129 ,130 ,enq: TX - row lock contention #从这里可以看出是sid 129 阻赛了130
20181019 081713 129 ,130 ,enq: TX - row lock contention
20181019 081723 129 ,130 ,enq: TX - row lock contention
20181019 081733 129 ,130 ,enq: TX - row lock contention
20181019 081743 129 ,130 ,enq: TX - row lock contention
20181019 081753 129 ,130 ,enq: TX - row lock contention
已选择6行。
4.源会话分析
找出某个SQL引起的等待事件
select event,sql_id,count(1) from gv$active_session_history a where a.sample_time > sysdate -15/144 group by event,sql_id order by count(1) desc ;
EVENT SQL_ID COUNT(1)
----------------------------------- --------------- ----------
enq: TX - row lock contention gw47xzmm9h6qq 5921
control file parallel write 34
log file parallel write 5
os thread startup 2
db file sequential read fsk02m4793dqq 1
null event 3ktacv9r56b51 1
dayq182sk41ks 1
f318xdxdn0pdc 1
null event 1
5d2u345mmudn9 1
1np800rjbv1kn 1
5.查询会话状态
set linesize 2000
set pagesize 2000
col ins_sid for a15
col username for a10
col machine for a10
col program for a26
col module for a26
col action for a10
col sql_id for a15
col event for a25
col blocking_session for 999999999
col logon_time for a26
col prev_exec_start for a10
col client_info for a10
select inst_id||','||sid ins_sid,username,machine,program,module,action,sql_id,event,blocking_session,logon_time,prev_exec_start,client_info from gv$session where status='ACTIVE' and type<>'BACKGROUND' order by inst_id,sid;
INS_SID USERNAME MACHINE PROGRAM MODULE ACTION SQL_ID EVENT BLOCKING_SESSION LOGON_TIME PREV_EXEC_ CLIENT_INF
----------- ------ ---------- -------------------------- -------------------------- ------ --------------- ------------------------- ------------ -------------------- ---------- ----------
1,8 SYS ora11g sqlplus@ora11g (TNS V1-V3) sqlplus@ora11g (TNS V1-V3) c3wctdc71swjh SQL*Net message to client 20181019 08:24:18 20181019 0
9:49:55
1,70 ora11g oracle@ora11g (J000) jobq slave wait 20181019 09:49:46
1,130 SYS ora11g sqlplus@ora11g (TNS V1-V3) sqlplus@ora11g (TNS V1-V3) gw47xzmm9h6qq enq: TX - row lock conten 129 20181019 08:16:26 20181019 0
tion 8:16:26
1,131 ora11g oracle@ora11g (J001) jobq slave wait 20181019 09:49:46
查询被阻塞的SQL语句:
select SQL_ID,sql_text from v$sqltext where SQL_ID='&sql_id' order by piece asc;
SQL_ID SQL_TEXT
--------------- ----------------------------------------------------------------------------------------------------
gw47xzmm9h6qq update scott.t1 set name='huyi3' where id=1;
查询阻塞的SQL语句:
set linesize 1000
select /*+ ORDERED USE_NL(st) */ INST_ID,SQL_ID,sql_text from gv$sqltext a
where (a.hash_value, a.address) IN (
SELECT DECODE (sql_hash_value,0, prev_hash_value,sql_hash_value),
DECODE (sql_hash_value, 0,prev_sql_addr, sql_address)
from gv$session where sid=&sid and INST_ID=INST_ID ) order by piece asc;
输入 sid 的值: 129
原值 5: from gv$session where sid=&sid and INST_ID=INST_ID ) order by piece asc
新值 5: from gv$session where sid=129 and INST_ID=INST_ID ) order by piece asc
INST_ID SQL_ID SQL_TEXT
---------- --------------- ----------------------------------------------------------------------------------------------------
1 4f0caa4cy9x8v update scott.t1 set name='huyi2' where id=1
#可以看出:是由于会话129 更新了一条数据,未提交,导到了另一个会话更新相同数据时被阻塞。
# 总结:1.出现类似问题思路清晰,找到时间段,是什么原因,出现什么问题 ,理清问题关系
# 2.要深度了解gv$active_session_history 的数据源,以及各字段的含义,以方便我们能没在有工具的情况下,编写出我们需要的脚本。
历史等待事件状态:
set linesize 2000
set pagesize 2000
col event for a35
select event,count(1) from gv$active_session_history a where a.sample_time > sysdate -15/144 group by event order by count(1) desc ;
EVENT COUNT(1)
----------------------------------- ----------
enq: TX - row lock contention 5871
71
control file parallel write 34
log file parallel write 5
null event 2
os thread startup 2
db file sequential read 1
已选择7行。
173
1
SQL> alter session set NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS' ;
2
会话已更改。
3
4
1.找到某个大至时间范围 即:什么时间
5
SQL> select trunc(sample_time,'mi'),count(1) from gv$active_session_history group by trunc(sample_time,'mi') order by 1 ;
6
TRUNC(SAMPLE_TIME COUNT(1)
7
----------------- ----------
8
9
20181019 07:56:00 1
10
20181019 07:57:00 3
11
20181019 07:58:00 1
12
20181019 08:00:00 3
13
20181019 08:01:00 1
14
20181019 08:03:00 1
15
20181019 08:06:00 2
16
20181019 08:09:00 3
17
20181019 08:11:00 2
18
20181019 08:13:00 1
19
20181019 08:15:00 2
20
20181019 08:17:00 59 #这里等待时事明显突然增多
21
20181019 08:18:00 64
22
20181019 08:19:00 63
23
20181019 08:20:00 60
24
20181019 08:21:00 62
25
20181019 08:22:00 60
26
20181019 08:23:00 60
27
20181019 08:24:00 61
28
20181019 08:25:00 60
29
20181019 08:26:00 61
30
20181019 08:27:00 62
31
32
2. 找出某个等待事件 :
33
34
SQL> select trunc(sample_time,'mi'),event,count(1) from gv$active_session_history
35
2 where
36
3 sample_time >=to_date('20181019 08:14:00','yyyymmdd hh24:mi:ss')
37
4 and sample_time <=to_date('20181019 08:18:00','yyyymmdd hh24:mi:ss')
38
5 group by trunc(sample_time,'mi') ,event order by 1 ;
39
40
TRUNC(SAMPLE_TIME EVENT COUNT(1)
41
----------------- ----------------------------------- ----------
42
20181019 08:15:00 1
43
20181019 08:15:00 log file parallel write 1
分析思路: