oracle 批量杀死 死锁进程

有时候死锁时需要杀死的进程有太多,一个个杀会很麻烦。

因此考虑用语句进行批量杀


这里要用到的是  单引号与变量拼接,我最初一直卡在这里。在网上搜到http://blog.csdn.net/firetaker/article/details/5666634后才解决


最初版:



declare cursor mycur is
select b.username,b.sid,b.serial#,logon_time
  from v$locked_object a,v$session b
  where a.session_id = b.sid order by b.logon_time;



begin
  for cur in mycur
    loop
     select count(1) into newcount from  v$locked_object a,v$session b
  where a.session_id = b.sid and b.sid=cur.sid and b.serial#=cur.SERIAL#  order by b.logon_time;

     execute immediate ( 'alter system  kill session  '''||cur.sid || ','|| cur.SERIAL# ||''' ');

     end loop;

end;


提示会话id不存在,刚开始想不通,后来想通了,应该是select语句查出来的sid,serial#存在重复的情况,前面已经kill掉了以后,后面再对这个sid,serial#进行kill就会提示不存在了,所以考虑每次实时进行查询是否还存在,在execute前加一个判断。


改版:




declare cursor mycur is
select b.username,b.sid,b.serial#,logon_time
  from v$locked_object a,v$session b
  where a.session_id = b.sid order by b.logon_time;

newcount number;


begin
  for cur in mycur
    loop


     select count(1) into newcount from  v$locked_object a,v$session b
  where a.session_id = b.sid and b.sid=cur.sid and b.serial#=cur.SERIAL#  order by b.logon_time;


     if newcount>=1 then

     execute immediate ( 'alter system  kill session  '''||cur.sid || ','|| cur.SERIAL# ||''' ');
     end if;


     end loop;

end;


执行后发现有效果,但是很慢。


所以想到直接避免循环中每次再去查询:



[sql]  view plain  copy
  1. declare cursor mycur is  
  2. select b.sid,b.serial#  
  3.   from v$locked_object a,v$session b  
  4.   where a.session_id = b.sid group by b.sid,b.serial#;  
  5.   
  6.   
  7. begin  
  8.   for cur in mycur  
  9.     loop    
  10.      execute immediate ( 'alter system  kill session  '''||cur.sid || ','|| cur.SERIAL# ||''' ');  
  11.      end loop;  
  12.   
  13. end;  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值