Oracle数据库查找持有锁的SQL语句,而不是请求锁的SQL语句
- 查找活动的事务以及活动事务关联的会话信息
select s.sid 会话ID,
s.serial# 会话序列号,
s.username 用户名,
s.machine 主机名,
s.module 程序名,
t.addr 事务ID,
t.status 事务状态,
t.start_time 事务开启时间
from v$session s, v$transaction t
where t.addr = s.taddr
order by t.start_time;
- 查找具体的锁表SQL根据事务包含的SQL数量可以分为以下两种方式
- 事务中只有一个SQL语句,可以通过指定精确时间点来查找锁表SQL,准确度高
注意:SQL代码中的事务开始时间字段数据来源是来自事务视图(v$ transaction)中的事务开始时间
select s.sql_text sql代码,
s.sql_id sql_ID,
s.last_active_time SQL最后执行时间,
s.module 程序名,
s.parsing_schema_name 用户,
s.service 服务,
s.action 操作
from v$sql s
where s.last_active_time = to_date('2018/4/1 1:09:50', 'yyyy-mm-dd HH24:mi:ss') --事务开启时间至今所有的SQL语句
and s.parsing_schema_name = 'SYSTEM' --执行SQL代码的用户
and s.module = 'PL/SQL Developer' --执行SQL代码的程序
and trim(s.sql_text) not like 's%' --过滤select语句
and trim(s.sql_text) not like 'S%' --过滤select语句
and s.service not like 'SYS$%' --过滤数据库内部SQL
order by s.last_active_time asc; --根据执行时间排序
- 事务中包含多个SQL语句:可以通过指定时间范围,用户名,程序名等进行范围查找,准确度较低,
范围查找的弊端:因事务ID无法和SQL_ID没有关联字段,因此范围查会包括不同事务所发起SQL语句,需要根据需求进行二次过滤
select s.sql_text sql代码,
s.sql_id sql_ID,
s.last_active_time 最后执行时间,
s.module 程序名,
s.parsing_schema_name 用户名,
s.service 服务,
s.action 操作
from v$sql s
where (s.last_active_time between
to_date('2018/4/1 1:09:50', 'yyyy-mm-dd HH24:mi:ss') and sysdate)
and s.parsing_schema_name = 'SYSTEM' --执行SQL代码的用户
and s.module = 'PL/SQL Developer' --执行SQL代码的程序
and trim(s.sql_text) not like 's%' --过滤select语句
and trim(s.sql_text) not like 'S%' --过滤select语句
and s.service not like 'SYS$%' --过滤数据库内部SQL
order by s.last_active_time asc; --根据执行时间排序