日期:2014-05-18  浏览次数:20685 次

怎样查看系统中正在运行的SQL语句已经执行了多久,最好是oracle sqlserver 都有
怎样查看系统中正在运行的SQL语句已经执行了多久,最好是oracle sqlserver 都有。谢谢

------解决方案--------------------
Oracle今天写的查看运行长时间等待查询语句脚本 
http://li2.itpub.net/post/72/3515

http://www.itpub.net/viewthread.php?tid=834588


v$session中的last_call_et列表示从STATUS(状态)改变到当前的时间(单位是秒)。
也就是说,如果session当前是inactive的,则表示inactive的时间
如果session当前是active的,则表示active的时间。
另外需要注意的是last_call_et表示的是last call的时间。
对于单条SQL而言,是指sql执行时间;对于plsql而言,则是指整个plsql的执行时间。
后台进程一直是active的,因此后台进程的last_call_et与实例运行时间相近。
查找方法:
先找出SID:select sid,username,last_call_et,status from v$session
where status='ACTIVE' and username is not null
order by last_call_et;
再根据SID找出执行的SQL或PL/SQL:
select sql_text from v$session a,v$sqltext_with_newlines b
where DECODE(a.sql_hash_value, 0, prev_hash_value, sql_hash_value)=b.hash_value
and a.sid=&1 order by piece;
附:查找空闲时间较长的连接:
select sid,serial#,username,machine,last_call_et,status from v$session 
where status='INACTIVE' and username is not null
order by last_call_et; 
如果为了减小资源占用,可以将长时间空闲的连接kill掉.
------解决方案--------------------
select sid,v$session.username 用户名,last_call_et 持续时间,status 状态,LOCKWAIT 等待锁,machine 用户电脑名,logon_time 开始登入时间,sql_text from v$session ,v$process ,v$sqlarea
where paddr=addr and sql_hash_value=hash_value 
and status='ACTIVE' and v$session.username is not null
order by last_call_et desc