监控当前数据库的活动session
6.1 监控session的执行语句
6.1.1 通过动态性能视图查找活动session的执行语句
select a.SID,a.USERNAME,a.machine,a.TERMINAL,b.PIECE,b.SQL_TEXT
from v$session a,
v$sqltext b
where b.ADDRESS = decode(a.SQL_HASH_VALUE,0,a.PREV_SQL_ADDR,a.SQL_ADDRESS)
and a.status = 'ACTIVE'
and user# >0
order by a.SQL_ADDRESS,b.PIECE;
6.1.2通过动态性能视图查找所有session的执行语句
select a.SID,b.ADDRESS,b.HASH_VALUE,a.USERNAME,a.machine,b.SADDR,c.PIECE,c.SQL_TEXT
from v$session a,
v$open_cursor b,
v$sqltext c
where a.SID = b.SID
and b.ADDRESS = c.ADDRESS
and b.HASH_VALUE = c.HASH_VALUE
and a.status = 'ACTIVE'
and user# >0
order by a.SID,b.ADDRESS,b.HASH_VALUE,a.USERNAME,a.machine,b.SADDR,c.PIECE;
6.1.3 通过操作系统查找相关session信息
1, 找出最消耗cpu的操作系统进程
# ps aux| grep -v grep | grep ora| head -10
oracle 876648 1.9 1.0 57832 82156 - A 16:22:35 7:59 oracleSISDB2 (LO
oracle 594138 1.9 1.0 58808 83132 - A 15:22:46 16:48 oracleSISDB2 (LO
oracle 495712 0.9 1.0 56628 80952 - A 17:04:47 0:43 oracleSISDB2 (LO
oracle 712946 0.5 1.0 55716 80040 - A 17:11:33 0:07 oracleSISDB2 (LO
oracle 966862 0.1 1.0 55144 79468 - A Jul 08 153:01 oracleSISDB2 (LO
oracle 442494 0.1 1.0 58984 83308 - A Feb 16 1751:47 ora_lms1_SISDB2
oracle 581808 0.1 1.0 59140 83464 - A Feb 16 1747:01 ora_lms0_SISDB2
oracle 811254 0.1 1.0 55228 79552 - A 15:51:29 0:31 oracleSISDB2 (LO
oracle 573582 0.0 1.0 57680 82004 - A Feb 16 149:17 ora_lmon_SISDB2
oracle 651300 0.0 1.0 57204 81528 - A Feb 16 125:13 ora_diag_SISDB2
2, 找出给定操作系统pid的session的执行sql
V$open_cursor视图列出session打开的所有cursor, 很多时候都将被用到, 比如: 你可以通过这个视图查看各个session打开的cursor数.
当诊断系统资源占用时, v$open_cursor视图常被用来连接v$sqlarea和v$sql查询出特定SQL(高逻辑或物理IO). 然后, 下一步就是找出源头.
V$sqlarea中的统计项在语句完全执行后被更新(并且从v$session.sql_hash_value中消失). 因此, 我们无法通过v$sqlarea跟v$session直接关联找到session, 除非语句被再次执行. 不过如果session的cursor仍然打开着, 用户就可以通过v$open_cursor来找出执行这个语句的session.
SELECT /*+ ORDERED */
address,piece,sql_text
FROM v$sqltext a
WHERE (a.hash_value, a.address) IN (
SELECT d.HASH_VALUE,d.ADDRESS
FROM v$session b,v$open_cursor d
where b.SID = d.SID
and b.paddr = (SELECT addr
FROM v$process c
WHERE c.spid = '&pid'))
ORDER BY address,piece;