很多時候由於異常或程序錯誤會導致個別進程占用大量系統資源,需要結束這些進程,通常可以使用以下命令Kill進程:
alter system kill session 'sid,serial#';
但是此命令釋放資源極為緩慢,具體可以參考:Oracle中Kill session的研究.
為了更快速的釋放資源,通常我們使用如下步驟來Kill進程:
1.首先在操作系統級kill進程
2.在數據庫內部kill session
這樣通常可以快速中止進程,釋放資源。
今天就遇到這樣一個案例,其他朋友在數據庫裡kill session,可是長時間仍無效果:
[Oracle@danaly ~]$ sqlplus "/ as sysdba"
SQL*Plus: Release 10.2.0.1.0 - Production on Thu Oct 27 11:09:50 2005
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
SQL> select sid,username,status from v$session;
SID USERNAME STATUS
---------- ------------------------------ --------
....
154 SCOTT KILLED
...
30 rows selected.
那按照我前面提到的步驟,首先查詢得到該session對應的OS進程號:
SQL> select 'kill -9 '||spid from v$process where addr = (select paddr from v$session where sid=&sid);
Enter value for sid: 154
old 1: select 'kill -9 '||spid from v$process where addr = (select paddr from v$session where sid=&sid)
new 1: select 'kill -9 '||spid from v$process where addr = (select paddr from v$session where sid=154)
'KILL-9'||SPID
--------------------
kill -9 22702
SQL> !
在操作系統級kill該進程:
[Oracle@danaly ~]$ ps -ef|grep 22702
oracle 22702 1 0 Oct25 ? 00:00:02 Oracledanaly (LOCAL=NO)
Oracle 12082 12063 0 11:12 pts/1 00:00:00 grep 22702
[Oracle@danaly ~]$ kill -9 22702
[Oracle@danaly ~]$ ps -ef|grep 22702
Oracle 12088 12063 0 11:12 pts/1 00:00:00