查找死锁进程
column sid format 999;
column b format 9;
column object_name format a30;
select v$lock.sid,
decode(v$lock.type,
'MR', 'Media Recovery',
'RT','Redo Thread',
'UN','User Name',
'TX', 'Transaction',
'TM', 'DML',
'UL', 'PL/SQL User Lock',
'DX', 'Distributed Xaction',
'CF', 'Control File',
'IS', 'Instance State',
'FS', 'File Set',
'IR', 'Instance Recovery',
'ST', 'Disk Space Transaction',
'TS', 'Temp Segment',
'IV', 'Library Cache Invalida-tion',
'LS', 'Log Start or Switch',
'RW', 'Row Wait',
'SQ', 'Sequence Number',
'TE', 'Extend Table',
'TT', 'Temp Table',
'Unknown') LockType,
rtrim(object_type) || ' ' || rtrim(owner) || '.' || object_name object_name,
decode(lmode, 0, 'None',
1, 'Null',
2, 'Row-S',
3, 'Row-X',
4, 'Share',
5, 'S/Row-X',
6, 'Exclusive', 'Unknown') LockMode,
decode(request, 0, 'None',
1, 'Null',
2, 'Row-S',
3, 'Row-X',
4, 'Share',
5, 'S/Row-X',
6, 'Exclusive', 'Unknown') RequestMode,
ctime, block b
from v$lock, all_objects
where sid > 6
and v$lock.id1 = all_objects.object_id;
这个SQL可用来生成杀死死锁进程的SQL
select 'alter system kill session '''||a.sid||','||b.serial#||''';'
from v$lock a,all_objects c,v$session b
where a.sid > 6 and a.id1 = c.object_id and a.sid = b.sid;
select 'alter system kill session '''||a.sid||','||b.serial#||''';'
from v$lock a,v$session b
where a.sid > 6 and a.sid = b.sid;
空闲之际,写了个sql语句,解决当前工作中经常遇到的锁定和被锁定表得信息,欢迎大家使用,指正。
如何快捷的显示当前锁定和被锁定表的用户,sql等信息
如果是dba用户那么可以使用
一、
阻塞和等待的用户及sql语句
select 'blocker('||wb.holding_session||':'||sb.username||')-sql:'||qb.sql_text blockers,sb.machine,sb.terminal,
'waiter ('||wb.waiting_session||':'||sw.username||')-sql:'||qw.sql_text waiters ,sw.machine,sb.terminal
from dba_waiters wb,
v$session sb,
v$session sw,
v$sqlarea qb,
v$sqlarea qw
where wb.holding_session=sb.sid
and wb.waiting_session=sw.sid
and sb.prev_sql_addr=qb.address
and sw.sql_address=qw.address
and wb.mode_held<>'None'
二、如果不是dba那么就不能使用dba_waiters表了,可以使用v$lock来替换
select 'blocker('||lb.sid||':'||sb.username||')-sql:'|| qb.sql_text blockers,
'waiter ('||lw.sid||':'||sw.username||')-sql:'|| qw.sql_text waiters
from v$lock lb,
v$lock lw,
v$session sb,
v$session sw,
v$sql qb,
v$sql qw
where lb.sid=sb.sid
and lw.sid=sw.sid
and sb.prev_sql_addr=qb.address
and sw.sql_address=qw.address
and lb.id1=lw.id1
and sw.lockwait is not null
and sb.lockwait is null
and lb.block=1
如果向更详细的显示username的信息可以加入
select 'blocker('||lb.sid||':'||sb.username||')-sql:'|| qb.sql_text blockers,sb.machine,sb.terminal,sb.logon_time,
'waiter ('||lw.sid||':'||sw.username||')-sql:'|| qw.sql_text waiters ,sw.machine,sw.terminal,sw.logon_time
from v$lock lb,
v$lock lw,
v$session sb,
v$session sw,
v$sql qb,
v$sql qw
where lb.sid=sb.sid
and lw.sid=sw.sid
and sb.prev_sql_addr=qb.address
and sw.sql_address=qw.address
and lb.id1=lw.id1
and sw.lockwait is not null
and sb.lockwait is null
and lb.block=1
三、--根据以上显示的信息可以知道当前产生锁和等待的用户信息
--那么就需要杀掉当前产生阻塞的用户,在执行杀掉用户进程前请咨询dba和该业务的同事,是否产生其他的不当影响
select sid,serial# from v$session sid in (...) --查询产生阻塞用户的 sid,serial#,然后
alter system kill session(sid,serial#); --杀掉产生阻塞的用户
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/640706/viewspace-1031379/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/640706/viewspace-1031379/