DBA常用SQL之会话与等待事件

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

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

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.查询会话等待与对应的SQL

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

4.查询会话等待事件与对应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

5.查询长时间运行的会话与对应的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

6.查询会话及锁与对应的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‘
: sid  182 : sertal#

6.1查询会话及锁与对应SQL

6.1查询会话及锁与对应SQL

6.2查询阻塞会话与被阻塞会话的对应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

原文地址:https://www.cnblogs.com/vmsysjack/p/12150670.html

时间: 2024-11-09 00:45:54

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

DBA_Oracle DBA常用SQL汇总(概念)

2014-06-20 BaoXinjian DBA常用的SQL汇总 1.监控索引是否使用 alter index &index_name monitoring usage; alter index &index_name nomonitoring usage; select * from v$object_usage where index_name = &index_name; 2.求数据文件的I/O分布 select df.name,phyrds,phywrts,phyblkr

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

DB SQL Monitor v1.5 by zhaoguan wang 说明 ------------------------------------------------------------------------------------       从SQL阻塞和等待事件的角度,了解数据库运行情况,供DBA和开发人员分析优化做参考 要求 --------------------------------------------------------------------------

Oracle DBA常用SQL

监控SQL 1.监控事例的等待: select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,count(*) from v$session_wait group by event order by 4; 2.回滚段的争用情况: select name,waits,gets,waits/gets ratio from v$rollstat a,v$rollname b where a.usn=

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

第一部分: 1. 查看oracle最大连接数 sql>show parameter processes #最大连接数 2. 修改最大连接数 sql>alter system set processes=value scope=spfile 重启数据库 sql>shutdown force  sql>start force 3. 查看当前连接数 sql>select * from v$session where username is not null 4. 查看不同用户的连接

DBA常用SQL之表空间与数据文件

select t.tablespace_name, round(sum(bytes / (1024 * 1024)), 0) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name; 1.查看表空间的名称及大小 select a.tablespace_name, a.bytes / 1024 / 1024 "Sum

dba常用SQL语句

查询rman备份集 SELECT TRIM(START_TIME||'#'), TRIM(END_TIME||'#'), TRIM(CASE OUTPUT_DEVICE_TYPE WHEN 'DISK' THEN 'DISK' WHEN 'SBT_TAPE' THEN 'SBT_TAPE' ELSE 'Others' END||'#') AS BACKUP_DEVICE, TRIM(STATUS||'#'), TRIM(CASE INPUT_TYPE WHEN 'DB FULL' THEN 'D

Oracle Bug 某脚本一直处于执行状态,等待事件为:asynch descriptor resize

问题描述:       项目反馈数据库服务器的CPU持续100%的情况,跟踪发现很多活动会话的等待事件是"asynch descriptor resize",并且这些会话一直处于Active状态,而这些会话执行的查询并不复杂,感觉很是奇怪.       查阅了一下资料,对应Oracle的Bug 9829397,Excessive CPU and many "asynch descriptor resize" waits for SQL using Async IO,

等待事件监测性能瓶颈

1. 会话等待事件性能视图:v$system_event, v$session_event, v$session_wait v$system_event: 查看整个系统级或者说最高级别的整体系统性能情况,这里面没有每个会话的相关信息,而是对每个事件总计了从系统启动以来在所有会话中发生过的情况.这个视图有5个字段: event: 列出所有发生的事件的名称. total_waits: 从数据库启动到现在这个等待事件总的等待次数. total_timeouts:总的等待超时次数. time_waite

DBA数据库信息查询常用SQL

常用DBA脚本1.查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name; 2.查看表空间物理文件的名称及大小 select tablespace_name, file_id,