六, 监控当前数据库的活动session
六 , 监控当前数据库的活动 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_HA
六, 监控当前数据库的活动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'
anduser# >0
orderby 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'
anduser# >0
orderby 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'))
ORDERBY address,piece;
6.2 session的资源占用
6.2.1 通过动态性能视图查找相关session信息
利用V_$SQLAREA视图提供了执行的细节。(执行、读取磁盘和读取缓冲区的次数)
• 数据列
EXECUTIONS:执行次数
DISK_READS:读盘次数
COMMAND_TYPE:命令类型(3:select,2:insert;6:update;7delete;47:pl/sql程序单元)
OPTIMIZER_MODE:优化方式
SQL_TEXT:Sql语句
SHARABLE_MEM:占用shared pool的内存多少
BUFFER_GETS:读取缓冲区的次数
• 用途
1、帮忙找出性能较差的SQL语句
2、帮忙找出最高频率的SQL
3、帮忙分析是否需要索引或改善联接
求DISK READ较多的SQL
select st.ADDRESS,st.PIECE,st.sql_text
from v$sql s, v$sqltext st
where s.address = st.address
and s.hash_value = st.hash_value
and s.disk_reads > 300
orderby st.address, st.piece ;
求DISK SORT严重的SQL
select sess.username, sql.sql_text, sort1.blocks
from v$session sess, v$sqlarea sql, v$sort_usage sort1
where sess.serial# = sort1.session_num
and sort1.sqladdr = sql.address
and sort1.sqlhash = sql.hash_value
and sort1.blocks > 200;
查看语句占用的内存情况
select username, sum(sharable_mem), sum(persistent_mem), sum(runtime_mem)
fromsys.v_$sqlarea a, dba_users b
where a.parsing_user_id = b.user_id
groupby username;
6.2.2 通过操作系统查找相关session信息
# ps aux|head -1; ps aux|sort -nr +2 |head -10
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
root 73764 6.1 0.0 384 384 - A Jan 10 130144:34 wait
root 57372 6.1 0.0 384 384 - A Jan 10 132116:52 wait
root 65568 6.0 0.0 384 384 - A Jan 10 129411:36 wait
# ps aux |head -1; ps aux |sort -nr +3 | head -10
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
oracle 974978 2.2 1.0 57992 82316 - A 14:05:06 2:41 oracleSISDB2 (LO
oracle 966862 0.1 1.0 55144 79468 - A Jul 08 80:49 oracleSISDB2 (LO
oracle 942332 0.0 1.0 59112 83436 - A Feb 16 2:24 ora_arc0_SISDB2
oracle 909346 1.4 1.0 58364 82688 - A 13:49:28 3:22 oracleSISDB2 (LO
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'))
ORDERBY address,piece;
6.3 session的等待事件
V$session_event, v$session_wait两个视图中记录的是session级别的等待事件, 通过查询这两个视图用户可以得到当前数据库的一些操作到底在等待什么, 是磁盘IO, 缓冲区忙还是插锁等.
V$SESSION_WAIT中的常用列
SID: session标识
EVENT: session当前等待的事件,或者最后一次等待事件。
WAIT_TIME: session等待事件的时间(单位,百分之一秒)如果本列为0,说明session当前session还未有任何等待。
SEQ#: session等待事件将触发其值自增长
P1, P2, P3: 等待事件中等待的详细资料
P1TEXT, P2TEXT, P3TEXT: 解释说明p1,p2,p3事件
附注:
1.State字段有四种含义﹕
Waiting:SESSION正等待这个事件。
Waited unknown time:由于设置了timed_statistics值为false,导致不能得到时间信息。表示发生了等待,但时间很短。
Wait short time:表示发生了等待,但由于时间非常短不超过一个时间单位,所以没有记录。
Waited knnow time:如果session等待然后得到了所需资源,那么将从waiting进入本状态。
Wait_time值也有四种含义:
值>0:最后一次等待时间(单位:10ms),当前未在等待状态。
值=0:session正在等待当前的事件。
值=-1:最后一次等待时间小于1个统计单位,当前未在等待状态。
值=-2:时间统计状态未置为可用,当前未在等待状态。
3.Wait_time和Second_in_wait字段值与state相关:
如果state值为Waiting,那么wait_time值无用。Second_in_wait值是实际的等待时间(单位:秒)。
如果state值为Wait unknow time,那么wait_time值和Second_in_wait值都无用。
如果state值为Wait short time,那么wait_time值和Second_in_wait值都无用。
如果state值为Waiting known time,那么wait_time值就是实际等待时间(单位:秒),Second_in_wait值无用。
Select s.SID,
s.username,
s.program,
s.status,
se.event,
se.total_waits,
se.total_timeouts,
se.time_waited,
se.average_wait
from v$session s, v$session_event se
Where s.sid = se.sid
And se.event not like 'SQl*Net%'
And s.status = 'ACTIVE'
And s.username is not null;
Select s.SID,
s.username,
s.program,
s.status,
sw.EVENT,
sw.STATE,
casewhen sw.STATE = 'WAITING'then'正在等待...'
when sw.state = 'WAITED UNKNOWN TIME'then'等待完成, 但时间很短'
when sw.state = 'WAITED SHORT TIME'THEN'等待完成, 但时间更短'
when sw.state = 'WAITED KNOWN TIME'then'等待完成,等待时间(单位10ms)'||sw.wait_time end state_memo,
casewhen sw.STATE = 'WAITING'then sw.SECONDS_IN_WAIT else0end seconds_in_wait,
sw.WAIT_TIME,
casewhen sw.WAIT_TIME = -1then'等待完成, 最后一次等待时间小于10ms...'
when sw.WAIT_TIME = -2then'等待完成, 统计时间未置为可用'
when sw.WAIT_TIME > 0then'等待完成, 最后一次等待时间(单位10ms)'||sw.WAIT_TIME
when sw.WAIT_TIME = 0then'正在等待'end wait_time_memo,
st.PIECE,
st.SQL_TEXT,
sw.P1TEXT,sw.p1, sw.P2TEXT,sw.p2, sw.P3TEXT, sw.P3
from v$session s, v$session_wait sw, v$sqltext st
Where s.sid = sw.sid
and s.sql_address = st.address(+)
And sw.event notlike'SQl*Net%'
And s.status = 'ACTIVE'
And s.username isnotnull
orderby sw.state,s.sid,st.PIECE;
v$session_wait视图的列代表的缓冲区忙等待事件如下:
P1—与等待相关的数据文件的全部文件数量。
P2—P1中的数据文件的块数量。
P3—描述等待产生原因的代码。
例:select p1 "File #", p2 "Block #", p3 "Reason Code"
from v$session_wait
where event = 'buffer busy waits';
如果以上查询的结果显示一个块在忙等待,以下的查询将显示这一块的名称和类型:
select owner, segment_name, segment_type
from dba_extents
where file_id = &P1 and &P2 between block_id and block_id + blocks -1;
我们也可以查询dba_data_files以确定等待的文件的file_name,方法是使用v$session_wait中的P1。
从v$session_wait中查询P3(原因编码)的值可以知道session等待的原因。原因编码的范围从0到300,下列为部分编码所代表的事项:
0 块被读入缓冲区。
100 我们想要NEW(创建)一个块,但这一块当前被另一session读入。
110 我们想将当前块设为共享,但这一块被另一session读入,所以我们必须等待read()结束。
120 我们想获得当前的块,但其他人已经将这一块读入缓冲区,所以我们只能等待他人的读入结束。
130 块被另一session读入,而且没有找到其它协调的块,所以我们必须等待读的结束。缓冲区死锁后这种情况也有可能产生。所以必须读入块的CR。
200 我们想新创建一个block,但其他人在使用,所以我们只好等待他人使用结束。
210 Session想读入SCUR或XCUR中的块,如果块交换或者session处于非连续的TX模式,所以等待可能需要很长的时间。
220 在缓冲区查询一个块的当前版本,但有人以不合法的模式使用这一块,所以我们只能等待。
230 以CR/CRX方式获得一个块,但块中的更改开始并且没有结束。
231 CR/CRX扫描找到当前块,但块中的更改开始并且没有结束。
6.4 跟踪长时间运行session的10046事件
1, 使用sql_trace跟踪当前session的10046事件
SQL> alter session set sql_trace = true;
Session altered
SQL> select 1 from dual;
1
SQL> alter session set sql_trace = false;
Session altered
2, 使用set events跟踪当前session的10046事件
SQL> alter session set events '10046 trace name context forever,level 12';
Session altered
SQL> select 2 from dual;
2
----------
2
SQL> alter session set events '10046 trace name context off';
Session altered
3, 使用oradebug跟踪当前session的10046事件
例如我们查看PID = 487432的进程, 可以使用下面的方法.
# su - oracle
[YOU HAVE NEW MAIL]
$ sqlplus /nolog
SQL*Plus: Release 10.2.0.3.0 - Production on Tue Jul 14 17:24:42 2009
Copyright (c) 1982, 2006, Oracle. All Rights Reserved.
SQL> conn / as sysdba
Connected.
SQL> oradebug setospid 487432
Oracle pid: 12, Unix process pid: 487432, image: oracle@i2db (MMNL)
SQL> oradebug event 10046 trace name context forever,level 8
Statement processed.
SQL> oradebug tracefile_name
/oracle/admin/arpdb/bdump/arpdb_mmnl_487432.trc
SQL> oradebug event 10046 trace name context off
Statement processed.
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
$ tkprof /oracle/admin/arpdb/bdump/arpdb_mmnl_487432.trc
output = arpdb_mm1.txt
TKPROF: Release 10.2.0.3.0 - Production on Tue Jul 14 17:31:29 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

As the D-Day invasion approaches its 80th anniversary, a whole month of World of Tanks events and specials will be centered around Operation Overlord - a new PvE mode, a themed battle pass, the release of a new Frontline mode, and a month-long The Operation Normandy token store is about to open. OPERATION MAP From June 3 to June 30, explore the beaches of Normandy and collect up to 90 Operation Normandy Tokens: 36 from this map and another 54 by completing daily tasks. Check out the interactive map and see the start dates for each event, then start earning tokens now, or unlock special token missions. Use the map to learn more about Operation Normandy related activities. Once you have obtained enough Operation Normandy tokens, you can go to the Operation Normandy token dealer

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

"Wenwen" multi-platform joint transport classic Wushuang server "Sheng Wushuang" is launched today. The multi-platform joint transport is very popular, and a valuable cumulative recharge lottery is in progress for a limited time! Many re-server activities such as Wushuang Monkey King, Spirit Box Treasure Hunt, Lucky Star, Achievement Master, and Ranking Competition are launched simultaneously today. Come and get the full benefits! Event time: 12:00 on May 7 to 23:59 on June 17. Open the Holy Spirit Box and look for rare treasures. Complete the corresponding activity tasks to receive lottery opportunities, and each lottery consumes 1 opportunity. The prize pool includes iPhone 15 Pro 256G (random color), Bao Bao customized gold and silver banknote night light set, 1,000 silver ingots and other rewards. Bind the new server "Sheng Wushuang" character on the "Spirit Box Treasure Hunt" activity page to directly obtain the "Dragon" which is limited to 30 days.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

On April 18th, a magical adventure feast in "The Land of Gods and Demons 2" is about to kick off! Are you ready for the launch of a new server? Explore the continent of Kelanmondo and make like-minded partners, waiting to write your own adventure chapter with you! In order to make the adventure more exciting and interesting, invite your friends to return to the Kelanmondo continent and join the new server immediately, and you will have the opportunity to receive generous rewards! Invite friends to join the new server starting from 12:00 on April 11th. After the server is opened, you can earn points as your level increases. The points can be redeemed for the "Calling Friends Gift Pack"! At 12:00 on April 16th, Warriors can pre-create characters in the new server. Not only can they preemptively register the character name of their choice, but after successfully pre-creating the character, they can also receive a "new server reservation gift package". 1. Reservation & Reward Instructions

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.
