ORACLE快速彻底Kill掉的会话,防止锁表

前端之家收集整理的这篇文章主要介绍了ORACLE快速彻底Kill掉的会话,防止锁表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

万能是sql重启服务,不然,以下之:

在ORACLE数据库当中,有时候会使用ALTER SYSTEM KILL SESSION 'sid,serial#'杀掉一个会话进程,但是使用这个sql语句杀掉会话后,数据库并不会立即释放掉相关的资源,有时候你会发现锁定的资源很长时间也不会释放,即使会话状态为“KILLED”,依然会阻塞其它会话。

下面根据Eygel的"Oracle中Kill session的研究",构造一个案例看看kill session到底做了什么。如下所示

会话1:

sql> conn etl/etl
Connected.
sql>  update test set status='invalid';
 
  
55944 rows updated.
sql> update test2 set dropped='Y';
3090 rows updated.

会话2:

sql> show user
USER is "SYS"
update etl.test2 3090  会话3

 
  
select saddr,sid,serial#,paddr,username,status from v$session where username =upper('etl') or username ='sys');
SADDR                   SID    SERIAL# PADDR            USERNAME                       STATUS
---------------- ---------- ---------- ---------------- ------------------------------ --------
000000025C233B00         27      33353 000000025F1D1FC8 ETL                            INACTIVE
000000025C23A608         37      11448 000000025F1D27B0 SYS                            ACTIVE
000000025C24BC50         63      54311 000000025F1D5F08 SYS                            ACTIVE
alter system kill '27,33353';
System altered.
SADDR                   SID    SERIAL# PADDR            USERNAME                       STATUS
--------------000000025C233B00         27      33353 000000025C21A0B0 ETL                            KILLED
000000025C23A608         37      11448 000000025F1D27B0 SYS                            ACTIVE
000000025C24BC50         63      54311 000000025F1D5F08 SYS                            INACTIVE

如下所示,我杀掉了其中两个会话后,这两个会话的地址都变为000000025C21A0B0了(请见PADDR列)。当在Oracle中kill session以后,Oracle只是简单的把相关session的paddr 指向同一个虚拟地址.此时v$process和v$session失去关联,进程就此中断。然后Oracle就等待PMON去清除这些Session.所以通常等待一个被标记为Killed的Session退出需要花费很长的时间.如果此时被Kill的process,重新尝试执行任务,那么马上会收到进程中断的提示,process退出,此时Oracle会立即启动PMON来清除该session.这被作为一次异常中断处理.

'63,54311';
000000025C233B00         27      33353 000000025C21A0B0 ETL                            KILLED
000000025C24BC50         63      54311 000000025C21A0B0 SYS                            KILLED

我们根据下面sql找到进程的地址,然后在v$process里面找到对应的spid,然后从操作系统中杀掉该进程。

select p.addr from v$process p where pid <> 1
  2  minus
  3  select s.paddr session s;
ADDR
--
000000025F1D1FC8
000000025F1D5F08
session 
    000000025C24BC50         63      54311 000000025C21A0B0 SYS                            KILLED
select addr,pid,spid,username from v$process where addr in ('000000025F1D1FC8','000000025F1D5F08');
ADDR                    PID SPID         USERNAME
-- ---------- ------------ ---------------
000000025F1D1FC8         22 12959        oracle
000000025F1D5F08         38 12971        oracle
sql> ! kill -9 12959
kill -9 12971
000000025C23A608         37      11448 000000025F1D27B0 SYS                            ACTIVE

在ORACLE数据库杀掉会话进程有三种方式:

1: ALTER SYSTEM KILL SESSION

关于KILL SESSION Clause ,如下官方文档描述所示,alter system kill session实际上不是真正的杀死会话,它只是将会话标记为终止。等待PMON进程来清除会话。

The KILL SESSION clause lets you mark a session as terminated,roll back ongoing transactions,release all session locks,and partially recover session resources. To use this clause,your instance must have the database open. Your session and the session to be terminated must be on the same instance unless you specify integer3.You must identify the session with the following values from the V$SESSION view:
For integer1,specify the value of the SID column.
For integer2,specify the value of the SERIAL# column.
For the optional integer3,specify the ID of the instance where the target session to be killed exists. You can find the instance ID by querying the GV$ tables.
If the session is performing some activity that must be completed,such as waiting for a reply from a remote database or rolling back a transaction,255)">then Oracle Database waits for this activity to complete,marks the session as terminated,and then returns control to you. If the waiting lasts a minute,255)">then Oracle Database marks the session to be terminated and returns control to you with a message that the session is marked to be terminated. The PMON background process then marks the session as terminated when the activity is complete.
Whether or not the session has an ongoing transaction,Oracle Database does not recover the entire session state until the session user issues a request to the session and receives a message that the session has been terminated.

可以使用ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE 来快速回滚事物、释放会话的相关锁、立即返回当前会话的控制权。

Specify IMMEDIATE to instruct Oracle Database to roll back ongoing transactions,recover the entire session state,and return control to you immediately.

2: ALTER SYSTEM DISCONNECT SESSION

ALTER SYSTEM DISCONNECT SESSION 杀掉专用服务器(DEDICATED SERVER)或共享服务器的连接会话,它等价于从操作系统杀掉进程。它有两个选项POST_TRANSACTION和IMMEDIATE, 其中POST_TRANSACTION表示等待事务完成后断开会话,IMMEDIATE表示中断会话,立即回滚事务。

sql> ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' POST_TRANSACTION;

3: KILL -9 SPID (Linux) 或 orakill ORACLE_SID spid (Windows)

可以使用下面sql语句找到对应的操作系统进程SPID,然后杀掉。当然杀掉操作系统进程是一件危险的事情,尤其不要误杀。所以在执行前,一定要谨慎确认。

SET LINESIZE 100
COLUMN spid FORMAT A10
COLUMN username FORMAT A10
COLUMN program FORMAT A45
SELECT s.inst_id,
       s.sid,244)">       s.serial#,monospace; width:878.046875px; padding:0px; direction:ltr; line-height:12pt; background-color:white">       p.spid,244)">       s.username,monospace; width:878.046875px; padding:0px; direction:ltr; line-height:12pt; background-color:white">       s.program
FROM   gv$session s
       JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
WHERE  s.type != 'BACKGROUND';

数据库如果要彻底杀掉一个会话,尤其是大事务会话,最好是使用ALTER SYSTEM DISCONNECT SESSIONIMMEDIATE或使用下面步骤:

1:首先在操作系统级别Kill掉进程。

2:在数据库内部KILL SESSION

或者反过来亦可。这样可以快速终止进程,释放资源。

参考资料:

http://www.eygle.com/faq/Kill_Session.htm

http://www.oracle-base.com/articles/misc/killing-oracle-sessions.php#unix

http://www.eygle.com/archives/2005/10/oracle_howto_kill_session.html

http://blog.sina.com.cn/s/blog_6d6e54f70100zfqp.html

http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_2013.htm#SQLRF53047

原文链接:https://www.f2er.com/oracle/213641.html

猜你在找的Oracle相关文章