【问题标题】:SQL - lock select checkSQL - 锁定选择检查
【发布时间】:2015-02-12 12:27:20
【问题描述】:

我在数据库记录-TX 表的 3 个会话上创建了锁。

接下来我写了一个查询来检查数据库Oracle上的锁,但有些地方不对:

select 
    round(s1.seconds_in_wait/60,1) as TIME, s1.event,
    s1.blocking_session as SID_A, s1.username as USER_A, 
    s2.sid as SID_B, s2.username as USER_B  
from 
    v$session  s1, v$session s2   
where
     s1.blocking_session is not null 
     and s1.seconds_in_wait > 1  
     and s1.sid = s2.sid  
order by 
     s1.seconds_in_wait desc;

结果:

| Time | Evetn              |SID_A| USER_A  |SID_B| USER_B  |
-------------------------------------------------------------
|10.1 | enq: TX row lock.. |  45 | Schema1 |  54 | Schema1 |
|15.5 | enq: TX row lock.. |  45 | Schema2 |  95 | Schema2 |

应该是这样的:

| Time | Evetn              |SID_A| USER_A  |SID_B| USER_B  |
-------------------------------------------------------------
|10.1 | enq: TX row lock.. |  45 | Schema1 |  54 | Schema2 |
|15.5 | enq: TX row lock.. |  45 | Schema1 |  95 | Schema3 |

哪里有问题,请帮忙。

【问题讨论】:

标签: sql oracle select locking


【解决方案1】:

您加入s1.sid=s2.sid - 这将与自己加入行。我认为您想使用阻塞 sid 加入:

select 
    round(s1.seconds_in_wait/60,1) as TIME, s1.event,
    s1.sid as SID_A, s1.username as USER_A, 
    s2.sid as SID_B, s2.username as USER_B  
from 
    v$session  s1, v$session s2   
where
     s1.blocking_session is not null 
     and s1.seconds_in_wait > 1  
     and s1.blocking_session = s2.sid  
order by 
     s1.seconds_in_wait desc;

或者(使用 ansi 连接):

select 
    round(s1.seconds_in_wait/60,1) as TIME, s1.event,
    s1.sid as SID_A, s1.username as USER_A, 
    s2.sid as SID_B, s2.username as USER_B  
from 
    v$session  s1
join
    v$session s2   
on
    s1.blocking_session = s2.sid  
where
     s1.blocking_session is not null 
     and s1.seconds_in_wait > 1  
order by 
     s1.seconds_in_wait desc;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多