当前位置: DBARoad > 基础知识 > 文章正文

Kill session的一些注意事项

                    作者 :OoNiceDream【转载时请务必以超链接形式标明文章原始出处和作者信息】
                    链接:http://www.dbaroad.me/archives/2008/11/kill-session-list.html

数据库中,常有一些状态为Killed的会话,直接查询V$PROCESS,其SPID已经查询不到。

SQL> select paddr from v$session where status ='KILLED';
 
PADDR
----------------
C000000821E89498
C000000821E89498
 
SQL> select spid from v$process a,v$session b where a.addr=b.paddr and b.status ='KILLED';
 
SPID
----------

虽然通过其它方法我们仍可以查询到其SPID,再到主机上进行KILL。
但是个人认为,杀会话,最好还是有个规范,像类似这种数小时之后查询,其状态仍是KILLED的会话,应及早处理,很有可能该会话持有资源仍没有释放。
1、对一般进程,查询其SPID并保存,先alter system kill ,再查看V$session中是否还存在;
若还是KILLED的状态,再行KILL -9。
2、对于大事物,建议先OS KILL -9,再行数据库中alter system kill。
3、OS KILL进程时需注意:先查询 ps -ef|grep spid,不要KILLED错进程了。
相关脚本:

set head off
select 'alter system kill session '||chr(39)||sid||','||a.serial#||chr(39)||';'
  FROM v$session a, v$process b
 WHERE a.paddr = b.addr AND a.sql_hash_value='1539424110';
 
set head on
 
SELECT SID, a.serial#,b.spid SPID,TYPE, a.username username, status, sql_hash_value, machine,osuser
  FROM v$session a, v$process b
 WHERE a.paddr = b.addr AND a.sql_hash_value='1539424110';
 
set head off
select 'kill -9 '||b.spid
 FROM v$session a, v$process b
 WHERE a.paddr = b.addr AND a.sql_hash_value='1539424110' ;
 
set head on

另外,像文中一开始提到的这种情况,会话已经KILLED,又如何查询其SPID呢?

SQL> select paddr from v$session where status ='KILLED';
 
PADDR
----------------
C000000821E89498
C000000821E89498
 
SQL> select spid from v$process a,v$session b where a.addr=b.paddr and b.status ='KILLED';
 
SPID
----------
 
SQL> select spid from v$process where addr IN
      2  (select addr from v$process where pid <> 1
      3  minus
      4  select paddr from v$session);
 
SPID
----------
27472
27474
27476
27478
27480
27484
27486
27489
27492
27494
27496
27500
3701
3705
 
14 rows selected
 
SQL> select server_name,spid from v$px_process;
 
SERVER_NAME SPID
----------- ----------
P000        27472
P001        27474
P002        27476
P003        27478
P004        27480
P005        27484
P006        27486
P007        27489
P008        27492
P009        27494
P010        27496
P011        27500
 
12 rows selected

这里我们看到,其实有12个进程,是后台的并行进程

[/app/oracle]$ ps -ef|grep 27472
  oracle  1290 29480  1 10:30:01 pts/td    0:00 grep 27472
  oracle 27472     1 162  Oct 29  ?        58:31 ora_p000_CMPR2
[/app/oracle]$ ps -ef|grep 27500
  oracle 27500     1  0  Oct 29  ?         0:13 ora_p011_CMPR2
  oracle  4161 29480  1 10:30:56 pts/td    0:00 grep 27500

修改查询SQL,如下:

SQL> select spid from v$process where addr IN
      2  (select addr from v$process where pid <> 1
      3  minus
      4  select paddr from v$session)
      5  minus
      6  select spid from v$px_process;
 
SPID
----------
3701
3705

在操作系统上查询相关进程,并KILL

[/app/oracle]$ ps -ef|grep 3701
  oracle   584 29480  2 10:29:48 pts/td    0:00 grep 3701
  oracle  3701     1  0  Oct 29  ?         0:00 oracleCMPR2 (LOCAL=NO)
[/app/oracle]$ ps -ef|grep 3705
  oracle  7187 29480  0 10:31:58 pts/td    0:00 grep 3705
  oracle  3705     1  0  Oct 29  ?         0:00 oracleCMPR2 (LOCAL=NO)
[/app/oracle]$ kill -9 3701
[/app/oracle]$ kill -9 3705

最后查询数据库,发现状态为KILLED的进程已消失:

SQL> select paddr from v$session where status ='KILLED';
 
PADDR
----------------
 
SQL>

— The End —

关键字:
喜欢DBARoad的文章,那就通过 RSS Feed 功能订阅阅读吧!

我要评论

*

* 绝不会泄露


  • 订阅


      抓虾
      google reader
      my yahoo
      bloglines
      鲜果
      有道

站内搜索



返回首页 | 关于我 | 联系我 | 广告合作 | 网站地图 | 友情链接 | 版权声明 |