SELECT *
FROM V$SESSION
WHERE USERNAME IS NOT NULL
AND STATUS = 'ACTIVE'
ORDER BY LOGON_TIME, SID;

1.查询会话

SELECT P.SPID,
S.SID,
S.OSUSER,
S.MACHINE,
S.PROGRAM,
S.LOGON_TIME,
Q.MODULE,
S.SERIAL#,
Q.SQL_ID,
Q.SQL_TEXT,
Q.SQL_FULLTEXT
FROM V$PROCESS P
INNER JOIN V$SESSION S
ON P.ADDR = S.PADDR
INNER JOIN V$SQLAREA Q
ON S.SQL_ID = Q.SQL_ID
WHERE S.AUDSID = USERENV('SESSIONID'); --查询当前会话

2.查询会话与对应的进程

3.Wait_time和Second_in_wait字段值与state相关:
  (1)如果state值为Waiting,那么wait_time值无用。Second_in_wait值是实际的等待时间(单位:秒)。
  (2)如果state值为Wait unknow time,那么wait_time值和Second_in_wait值都无用。
  (3)如果state值为Wait short time,那么wait_time值和Second_in_wait值都无用。
  (4)如果state值为Waiting known time,那么wait_time值就是实际等待时间(单位:秒),Second_in_wait值无用。

SELECT P.PID,
S.SID,
S.SERIAL#,
S.USERNAME,
Q.SQL_ID,
Q.SQL_TEXT,
Q.SQL_FULLTEXT,
W.EVENT,
W.WAIT_TIME,
W.STATE,
CASE WHEN W.STATE='WAITING' THEN W.SECONDS_IN_WAIT
WHEN W.STATE='WAITING KNOWN TIME' THEN W.WAIT_TIME
END AS SEC_IN_WAIT
FROM V$SESSION S, V$SESSION_WAIT W, V$SQLAREA Q, V$PROCESS P
WHERE S.SID = W.SID
AND S.SQL_ID = Q.SQL_ID
AND P.ADDR = S.PADDR
AND W.EVENT NOT LIKE 'SQL*Net%'
AND S.USERNAME IS NOT NULL
AND W.WAIT_TIME>=0
ORDER BY W.SECONDS_IN_WAIT DESC
;

3.查询会话等待与对应的SQL

SELECT P.PID,
S.SID,
S.SERIAL#,
S.USERNAME,
Q.SQL_ID,
Q.SQL_TEXT,
Q.SQL_FULLTEXT,
E.EVENT,
E.TOTAL_WAITS,
E.TIME_WAITED,
E.AVERAGE_WAIT
FROM V$SESSION S, V$SESSION_EVENT E, V$SQLAREA Q, V$PROCESS P
WHERE S.SID = E.SID
AND S.SQL_ID = Q.SQL_ID
AND P.ADDR = S.PADDR
AND E.EVENT NOT LIKE 'SQL*Net%'
AND S.STATUS = 'ACTIVE'
AND S.USERNAME IS NOT NULL
;

4.查询会话等待事件与对应SQL

SELECT S.SID,
OPNAME,
TRUNC(L.SOFAR / L.TOTALWORK * 100, 2) || '%' AS PCT_WORK,
L.ELAPSED_SECONDS ELAPSED,
ROUND(L.ELAPSED_SECONDS * (L.TOTALWORK - L.SOFAR) / L.SOFAR) REMAIN_TIME,
Q.SQL_TEXT
FROM V$SESSION_LONGOPS L, V$SQLAREA Q, V$SESSION S
WHERE L.SQL_HASH_VALUE = Q.HASH_VALUE
AND L.SID = S.SID
--AND L.SOFAR != L.TOTALWORK
AND L.ELAPSED_SECONDS>6
ORDER BY L.START_TIME
;

5.查询长时间运行的会话与对应的SQL

SELECT A.OWNER 方案名,
A.OBJECT_NAME 表名,
B.XIDUSN 回滚段号,
B.XIDSLOT 槽号,
B.XIDSQN 序列号,
B.SESSION_ID 锁表SESSION_ID,
B.ORACLE_USERNAME 锁表用户名,
decode(D.type,
'XR',
'NULL',
'RS',
'SS(Row-S)',
'CF',
'SS(Row-S)',
'TM',
'TABLE LOCK',
'PW',
'TABLE LOCK',
'TO',
'TABLE LOCK',
'TS',
'TABLE LOCK',
'RT',
'ROW LOCK',
'TX',
'ROW LOCK',
'MR',
'S(Share)',
NULL) 锁定方式,
C.MACHINE 用户组,
C.TERMINAL 机器名,
B.OS_USER_NAME 系统用户名,
B.PROCESS 系统进程id,
DECODE(C.STATUS, 'INACTIVE', '不活动', 'ACTIVE', '活动') 活动情况,
C.SERVER,
C.SID,
e.SQL_TEXT,
C.SERIAL#,
C.PROGRAM 连接方式,
C.LOGON_TIME
FROM ALL_OBJECTS A, V$LOCKED_OBJECT B, SYS.GV_$SESSION C, v$lock d,v$sqltext E
WHERE (A.OBJECT_ID = B.OBJECT_ID)
AND (B.PROCESS = C.PROCESS)
and C.sid = d.sid
and B.LOCKED_MODE = D.LMODE
and c.SQL_ID=e.sql_id (+)
ORDER BY 1, 2;

6.查询会话及锁与对应的SQL

select t2.username,
t2.sid,
t2.serial#,
t3.object_name,
t2.OSUSER,
t2.MACHINE,
t2.PROGRAM,
t2.LOGON_TIME,
t2.COMMAND,
t2.LOCKWAIT,
t2.SADDR,
t2.PADDR,
t2.TADDR,
t2.SQL_ADDRESS,
t1.LOCKED_MODE
from v$locked_object t1, v$session t2, dba_objects t3,v$sql t4
where t1.session_id = t2.sid
and t1.object_id = t3.object_id
--- and t2.SADDR=t4.ADDRESS(+) and t2.sql_id=t4.sql_id
order by t2.logon_time alter system kill session '139, 182'
139 : sid 182 : sertal#

6.1查询会话及锁与对应SQL

SELECT
s1.username "WAITING USER"
, s1.osuser "OS User" , s1.LOGON_TIME "logon time"
, w.session_id "Sid"
, p1.spid "PID"
, q1.SQL_TEXT "SQLTEXT"
, s2.username "HOLDING User"
, s2.osuser "OS User" , s2.LOGON_TIME "logon time"
, h.session_id "Sid"
, p2.spid "PID"
, q2.SQL_TEXT "SQLTEXT"
FROM
sys.v_$process p1
, sys.v_$process p2
, sys.v_$session s1
, sys.v_$session s2
, dba_locks w
, dba_locks h
, v$sql q1
, v$sql q2
WHERE
h.mode_held != 'None'
AND h.mode_held != 'Null'
AND w.mode_requested != 'None'
AND w.lock_type (+) = h.lock_type
AND w.lock_id1 (+) = h.lock_id1
AND w.lock_id2 (+) = h.lock_id2
AND w.session_id = s1.sid (+)
AND h.session_id = s2.sid (+)
AND s1.paddr = p1.addr (+)
AND s2.paddr = p2.addr (+)
AND s1.SQL_ID=q1.SQL_ID(+)
AND s2.SQL_ID=q2.SQL_ID(+)
order by h.session_id
;

6.2查询阻塞会话与被阻塞会话的对应SQL

DBA常用SQL之会话与等待事件的更多相关文章

  1. Oracle EBS DBA常用SQL - 安装/补丁【Z】

    Oracle EBS DBA常用SQL - 安装/补丁 检查应用补丁有没有安装:select bug_number,last_update_date from ad_bugs where bug_nu ...

  2. DB SQL Monitor 阻塞及等待事件监控工具

    SQL Monitor Designed By zhaoguan wang 说明------------------------------------------------------------ ...

  3. DBA_Oracle DBA常用SQL汇总(概念)

    2014-06-20 Created By BaoXinjian

  4. DB SQL Monitor 会话状态及等待事件监控工具

    DB SQL Monitor v1.7.6       Designed by Wang Zhaoguan   工具说明 --------------------------------------- ...

  5. DBA常用SQL之DDL生成语句-2

    ------数据迁移常用SQL SELECT 'DROP USER '||u.username ||' CASCADE;' AS dropstrs FROM DBA_USERS U where u.u ...

  6. postgresql dba常用sql查询语句

    查看帮助命令 DB=# help --总的帮助 DB=# \h --SQL commands级的帮助 DB=# \? --psql commands级的帮助 \dn[S+] [PATTERN],其中[ ...

  7. DBA常用SQL之DDL生成语句

    获取对象定义的包为:dbms_metadata,其中的get_ddl函数是获取对象的函数 GET_DDL函数返回创建对象的原数据的DDL语句,参数说明 .object_type ---需要返回原数据的 ...

  8. Oracle DBA常用SQL

    监控SQL 1.监控事例的等待: select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,c ...

  9. DBA常用SQL之数据库基础信息

    第一部分: 1. 查看oracle最大连接数 sql>show parameter processes #最大连接数 2. 修改最大连接数 sql>alter system set pro ...

随机推荐

  1. 第三周总结PSP日志文件

    项目计划 时间记录 缺陷记录 结对编程伙伴:李妍 20133099 博客地址:http://www.cnblogs.com/liyan-luckygirl/

  2. iOS-代理

    1.协议是一组通讯协议,一般用作两个类之间的通信. 2.协议声明了一组所有类对象都可以实现的接口. 3.协议不是类,用@protocol关键字声明一个协议. 4.与协议有关的两个对象,代理者和委托者. ...

  3. linux的ftp使用方法

    linux下常用FTP命令 1. 连接ftp服务器 以下是在服务器为Ubuntu环境下操作: (1)首先需要安装vsftp软件,已经安装的可跳过. 1.更新软件源 #apt-get  update 2 ...

  4. Why did Jimmy Wales invest in Quora? Is he afraid that it will take over Wikipedia?

    QUESTION: Why did Jimmy Wales invest in Quora? Is he afraid that it will take over Wikipedia? Answer ...

  5. PotPlayer播放器——最强大的播放器 - imsoft.cnblogs

    PotPlayer下载:链接 http://pan.baidu.com/s/17vgMM 密码: 8buc PotPlayer关联图标修改方法:打开安装目录替换目录下的PotIcons.dll文件即可 ...

  6. ZOJ 1002 Fire Net

    题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...

  7. activiti 任务授权,把流程中当前任务授权个其他用户代为办理

    / * 授权代办 * @param serDoc / @Transactional(readOnly = false) public void authAgent(SerDoc serDoc) { S ...

  8. oracle锁机制

    1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据 库执行请求,T2代表另一个请求,也可以理解为T1为 ...

  9. leetcode 132. Palindrome Partitioning II ----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  10. 20145220 实验五 Java网络编程

    20145220 实验五 Java网络编程 实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务 ...