欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

查询Oracle中正在执行和执行过的SQL语句

程序员文章站 2023-02-22 13:57:33
查询oracle正在执行的sql语句及执行该语句的用户 select b.sid oracleid, b.username 登录oracle用户名,...

查询oracle正在执行的sql语句及执行该语句的用户

select b.sid oracleid,
    b.username 登录oracle用户名,
    b.serial#,
    spid 操作系统id,
    paddr,
    sql_text 正在执行的sql,
    b.machine 计算机名
from v$process a, v$session b, v$sqlarea c
where a.addr = b.paddr
  and b.sql_hash_value = c.hash_value

其它网友给出的正在执行的sql语句代码

select a.username, a.sid,b.sql_text, b.sql_fulltext
 from v$session a, v$sqlarea b 
where a.sql_address = b.address 
---执行过的
select b.sql_text,b.first_load_time,b.sql_fulltext
 from v$sqlarea b
where b.first_load_time between '2016-10-1/09:24:47' and
    '2016-10-1/09:24:47' order by b.first_load_time

(此方法好处可以查看某一时间段执行过的sql,并且 sql_fulltext 包含了完整的 sql 语句)

其他

select osuser,program,username,schemaname,b.cpu_time,status,b.sql_text 
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 

select address, sql_text, piece 
 from v$session, v$sqltext 
where address = sql_address 
 -- and machine = < you machine name > 
order by address, piece 

查看正在执行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#‘;

查找前十条性能差的sql.

select * from (select parsing_user_id,executions,sorts, 
command_type,disk_reads,sql_text from v$sqlarea 
order by disk_reads desc )where rownum<10 ; 

查看占io较大的正在运行的session

select se.sid,se.serial#,pr.spid,se.username,se.status, 
se.terminal,se.program,se.module,、se.sql_address,st.event,st. 
p1text,si.physical_reads, 
si.block_changes from v$session se,v$session_wait st, 
v$sess_io si,v$process pr where st.sid=se.sid and st. 
sid=si.sid and se.paddr=pr.addr and se.sid>6 and st. 
wait_time=0 and st.event not like '%sql%' order by physical_reads desc

以上就是小编整理的关于查询oracle中正在执行和执行过的sql语句的相关内容,如果你有更好的方法,请评论留言,谢谢