- 最后登录
- 2018-9-14
- 在线时间
- 47 小时
- 威望
- 205
- 金钱
- 2327
- 注册时间
- 2011-10-13
- 阅读权限
- 150
- 帖子
- 90
- 精华
- 0
- 积分
- 205
- UID
- 26
|
5#
发表于 2017-8-16 16:15:08
16.4 确认哪里存在锁等待
以下查询确认了持有的锁和其最长持有时间,以及相应受到阻塞的会话:- SELECT
- lr.sid blocker_sid,
- ld.sid blocked_sid,
- lr.type, -- Type will typically be TM, TX, UL. Otherwise it is system type.
- lt.name,
- lr.id1, -- The value contained in these varies. For TM ID1 contains the object_id, for TX it is the Rollback entry
- lr.id2, -- The value contained in these varies. For TX it is the Transaction entry.
- decode(lr.lmode,
- 0,'none(0)',
- 1,'null(NULL)(1)',
- 2,'row-S(SS)(2)',
- 3,'row-S(SX)(3)',
- 4,'Share(S)(4)',
- 5,'S/Row-X(SSX)(5)',
- 6,'exclusive(X)(6)',
- LTRIM(TO_CHAR(lr.lmode,'990'))) lock_hold_mode,
- decode(lr.request,
- 0,'none(0)',
- 1,'null(NULL)(1)',
- 2,'row-S(SS)(2)',
- 3,'row-S(SX)(3)',
- 4,'Share(S)(4)',
- 5,'S/Row-X(SSX)(5)',
- 6,'exclusive(X)(6)',
- LTRIM(TO_CHAR(lr.request,'990'))) lock_request_mode,
- lr.ctime time_blocker_held,
- ld.ctime time_blocked_waiting
- FROM v$lock lr,
- v$lock ld,
- v$lock_type lt
- WHERE lt.type (+) = lr.type
- AND ld.id1 = lr.id1 -- rollback entries match
- AND ld.id2 = lr.id2 -- transaction entries match
- AND lr.block = 1 -- blocker
- AND ld.block = 0 -- blocked
- ORDER BY lr.ctime DESC;
复制代码 下面这个查询则提供了锁链情况 - 以链的长度排序,最长的在最前面:
主要的受阻会话可以通过这个查询来确认。
注意:如果存在持有锁的会话阻塞了多个其它会话的情况,那么部分锁链在报告中会被报告多次的情况(多行相同,每行对应一个链组合的起始和结束)。- SELECT *
- FROM
- (SELECT
- level chainlevel,
- blocked_sid,
- CONNECT_BY_ROOT blocker_sid ultimate_blocker_sid,
- sys_connect_by_path(blocker_sid,'/')|| '/' || blocked_sid blocking_path,
- time_blocked_waiting
- FROM (SELECT
- lr.sid blocker_sid,
- ld.sid blocked_sid,
- lr.id1,
- lr.id2,
- ld.ctime time_blocked_waiting
- FROM v$lock lr,
- v$lock ld
- WHERE ld.id1 = lr.id1
- AND ld.id2 = lr.id2
- AND lr.block = 1
- AND ld.block = 0) linked_locks
- CONNECT BY NOCYCLE PRIOR blocked_sid = blocker_sid) chains
- -- blocked end of chain is not a blocker session
- WHERE NOT EXISTS (SELECT 'exists'
- FROM v$lock lr,
- v$lock ld
- WHERE ld.id1 = lr.id1
- AND ld.id2 = lr.id2
- AND lr.block = 1
- AND ld.block = 0
- AND lr.sid = chains.blocked_sid)
- -- blocker end of chain is not a blocked session
- AND NOT EXISTS (SELECT 'exists'
- FROM v$lock lr,
- v$lock ld
- WHERE ld.id1 = lr.id1
- AND ld.id2 = lr.id2
- AND lr.block = 1
- AND ld.block = 0
- AND ld.sid = chains.ultimate_blocker_sid)
- ORDER BY chainlevel DESC;
复制代码 下面的查询则显示了死循环/死锁问题:- SELECT *
- FROM (SELECT
- DECODE(CONNECT_BY_ISCYCLE,1,'YES','NO') is_a_cycle,
- level,
- blocked_sid,
- CONNECT_BY_ROOT blocker_sid ultimate_blocker_sid,
- sys_connect_by_path(blocker_sid,'/')|| '/' || blocked_sid blocking_path,
- time_blocked_waiting
- FROM (SELECT
- lr.sid blocker_sid,
- ld.sid blocked_sid,
- lr.id1,
- lr.id2,
- ld.ctime time_blocked_waiting
- FROM v$lock lr,
- v$lock ld
- WHERE ld.id1 = lr.id1
- AND ld.id2 = lr.id2
- AND lr.block = 1
- AND ld.block = 0) linked_locks
- CONNECT BY NOCYCLE PRIOR blocked_sid = blocker_sid)
- WHERE is_a_cycle = 'YES';
复制代码 更多会话信息,按锁时间长短排序:- SELECT
- l.sid,
- l.block,
- DECODE(l.block,1,'Blocker','Blocked'),
- s.blocking_session,
- l.type,
- lt.name,
- l.id1,
- l.id2,
- decode(l.lmode,
- 0,'none(0)',
- 1,'null(NULL)(1)',
- 2,'row-S(SS)(2)',
- 3,'row-S(SX)(3)',
- 4,'Share(S)(4)',
- 5,'S/Row-X(SSX)(5)',
- 6,'exclusive(X)(6)',
- LTRIM(TO_CHAR(l.lmode,'990')) ) lock_hold_mode,
- decode(l.request,
- 0,'none(0)',
- 1,'null(NULL)(1)',
- 2,'row-S(SS)(2)',
- 3,'row-S(SX)(3)',
- 4,'Share(S)(4)',
- 5,'S/Row-X(SSX)(5)',
- 6,'exclusive(X)(6)',
- LTRIM(TO_CHAR(l.request, '990')) ) lock_request_mode,
- l.ctime time_held_s,
- s.seconds_in_wait,
- s.wait_class,
- s.event,
- s.p1text,
- s.p1,
- s.p1raw,
- s.p2text,
- s.p2,
- s.p2raw,
- s.p3text,
- s.p3,
- s.p3raw,
- s.serial#,
- s.program,
- s.client_identifier,
- s.terminal,
- s.command,
- ct.command_name,
- s.service_name,
- s.module,
- s.action,
- s.username,
- s.machine,
- DECODE(l.type, 'TM', o.object_name) object_name,
- s.sql_id,
- st.sql_text, -- just the first 64 characters of the SQL text
- st.hash_value,
- s.row_wait_obj#,
- s.row_wait_file#,
- s.row_wait_block#,
- s.row_wait_row#
- FROM v$session s,
- v$lock l,
- v$sqltext st,
- v$lock_type lt,
- v$sqlcommand ct,
- dba_objects o
- WHERE s.sid (+) = l.sid
- AND ( (l.request != 0) -- either requesting a lock
- OR
- (l.lmode != 0 -- or holding a lock
- AND
- EXISTS (SELECT 'exists'
- FROM v$lock ld
- WHERE ld.request != 0
- AND ld.id1 = l.id1
- AND ld.id2 = l.id2) -- and blocking a blocked session
- )
- )
- AND st.sql_id (+) = s.sql_id
- AND st.piece (+) = 0
- AND o.object_id (+) = l.id1
- AND lt.type (+) = l.type
- AND ct.command_type (+) = s.command
- -- AND l.sid = <session id>
- ORDER BY l.ctime DESC;
复制代码 针对某个被堵塞的会话所涉及对象及rowid,我们可以使用以下SQL查看:
这主要是针对行锁(TX),但也会显示TM锁的一些相关信息,这取决于表上的锁的模式以及被锁会话正在进行的活动。- SELECT
- o.object_name,
- s.row_wait_obj#,
- s.row_wait_file#,
- s.row_wait_block#,
- s.row_wait_row#,
- dbms_rowid.rowid_create( 0, s.row_wait_obj#, s.row_wait_file#, s.row_wait_block#, s.row_wait_row#) restricted_rowid,
- dbms_rowid.rowid_create( 1, s.row_wait_obj#, s.row_wait_file#, s.row_wait_block#, s.row_wait_row#) extended_rowid
- FROM v$session s,
- dba_objects o
- WHERE s.row_wait_obj# = o.object_id (+)
- AND s.sid = <sid>;
复制代码 rowid可以被用于指出表上被锁住的行。
以下SQL可用于通过sql id来得到SQL具体信息:
The rowid could be used to query the contents of the locked row on the object (table).
The following SQL can be run for particular sql ids to get more SQL text:- SELECT
- sql_text -- First 1000 characters
- FROM v$sql
- WHERE sql_id = <sql_id>;
- or:
- SELECT
- sql_text
- FROM v$sqltext
- WHERE sql_id = <sql_id>
- ORDER BY piece;
复制代码 |
|