-- 查询Oracle正在执行的sql语句及执行该语句的用户
select s.sid AS session_id,
s.serial# AS serial_number,
sql_text, -- 正在执行的sql
s.username AS login_user, -- 登录oracle用户名
s.machine, -- 计算机名
spid, -- 操作系统id
paddr
from v$session s, v$process p, v$sqlarea a
where p.addr = s.paddr
and s.sql_hash_value = a.hash_value;
-- 查看正在执行sql的发起者的发放程序
select osuser, -- 电脑登录身份
program, -- 发起请求的程序
username, -- 登录系统的用户名
schemaname,
b.cpu_time, -- 花费cpu的时间
status,
b.sql_text -- 执行的sql
from v$session a
left join v$sql b
on a.sql_address = b.address
and a.sql_hash_value = b.hash_value
order by b.cpu_time desc;
-- 查出oracle当前的被锁对象
select l.session_id sid,
s.serial#,
l.locked_mode, -- 锁模式
l.oracle_username, -- 登录用户
l.os_user_name, -- 登录机器用户名
s.machine, -- 机器名
s.terminal, -- 终端用户名
o.object_name, -- 被锁对象名
s.logon_time -- 登录数据库时间
from v$locked_object l, all_objects o, v$session s
where l.object_id = o.object_id
and l.session_id = s.sid
order by sid, s.serial#;
-- kill掉当前的锁对象
-- alter system kill session 'sid, s.serial#';
alter system kill session '877, 27944';
-- 查询当前正在执行的事务
select s.sid,
s.serial#,
s.event,
a.sql_text,
a.sql_fulltext,
s.username,
s.status,
s.machine,
s.terminal,
s.program,
a.executions,
s.sql_id,
p.spid,
a.direct_writes
from (select * from v$session where status = 'active') s
left join v$sqlarea a
on s.sql_id = a.sql_id
inner join v$process p
on s.paddr = p.addr;
-- 查看Oracle 数据库中的长事务
set linesize 200
set pagesize 5000
col transaction_duration format a45
with transaction_details as
(select inst_id, ses_addr, sysdate - start_date as diff from gv$transaction)
select s.username,
to_char(trunc(t.diff)) || ' days, ' ||
to_char(trunc(mod(t.diff * 24, 24))) || ' hours, ' ||
to_char(trunc(mod(t.diff * 24 * 60, 24))) || ' minutes, ' ||
to_char(trunc(mod(t.diff * 24 * 60 * 60, 60))) || ' seconds' as transaction_duration,
s.program,
s.terminal,
s.status,
s.sid,
s.serial#
from gv$session s, transaction_details t
where s.inst_id = t.inst_id
and s.saddr = t.ses_addr
order by t.diff desc
/
-- 查询长事务SQL
with ltr as
(select to_char(sysdate, 'YYYYMMDDHH24MISS') TM,
s.sid,
s.sql_id,
s.sql_child_number,
s.prev_sql_id,
xid,
to_char(t.start_date, 'YYYYMMDDHH24MISS') start_time,
e.TYPE,
e.block,
e.ctime,
decode(e.CTIME, 0, (sysdate - t.start_date) * 3600 * 24, e.ctime) el_second
-- q.sql_text
from v$transaction t, v$session s, v$transaction_enqueue e
where t.start_date <= sysdate - interval '100' second /*查询开始多少秒的事务*/
and t.addr = s.taddr
--and s.sql_child_number = q.CHILD_NUMBER(+)
--and s.sql_id = q.sql_id(+) and s.prev_sql_id = q.sql_id(+)
and t.addr = e.addr(+))
select ltr.*,
(select q1.sql_text
from v$sql q1
where ltr.prev_sql_id = q1.sql_id(+)
and rownum = 1) prev_sql_text,
(select q1.sql_text
from v$sql q1
where ltr.sql_id = q1.sql_id(+)
and ltr.sql_child_number = q1.CHILD_NUMBER(+)) sql_text
from ltr ltr;