Home Database Mysql Tutorial 六, 监控当前数据库的活动session

六, 监控当前数据库的活动session

Jun 07, 2016 pm 03:47 PM
session database Activity monitor

六 , 监控当前数据库的活动 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, 找出给定操作系统pidsession的执行sql

V$open_cursor视图列出session打开的所有cursor, 很多时候都将被用到, 比如: 你可以通过这个视图查看各个session打开的cursor.

当诊断系统资源占用时, v$open_cursor视图常被用来连接v$sqlareav$sql查询出特定SQL(高逻辑或物理IO). 然后, 下一步就是找出源头.

V$sqlarea中的统计项在语句完全执行后被更新(并且从v$session.sql_hash_value中消失). 因此, 我们无法通过v$sqlareav$session直接关联找到session, 除非语句被再次执行. 不过如果sessioncursor仍然打开着, 用户就可以通过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_TEXTSql语句

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),当前未在等待状态。
=0session正在等待当前的事件。
=-1:最后一次等待时间小于1个统计单位,当前未在等待状态。
=-2:时间统计状态未置为可用,当前未在等待状态。

3.Wait_timeSecond_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等待的原因。原因编码的范围从0300,下列为部分编码所代表的事项:
0
块被读入缓冲区。

100
我们想要NEW(创建)一个块,但这一块当前被另一session读入。

110
我们想将当前块设为共享,但这一块被另一session读入,所以我们必须等待read()结束。

120
我们想获得当前的块,但其他人已经将这一块读入缓冲区,所以我们只能等待他人的读入结束。

130
块被另一session读入,而且没有找到其它协调的块,所以我们必须等待读的结束。缓冲区死锁后这种情况也有可能产生。所以必须读入块的CR

200
我们想新创建一个block,但其他人在使用,所以我们只好等待他人使用结束。

210 Session
想读入SCURXCUR中的块,如果块交换或者session处于非连续的TX模式,所以等待可能需要很长的时间。

220
在缓冲区查询一个块的当前版本,但有人以不合法的模式使用这一块,所以我们只能等待。

230
CR/CRX方式获得一个块,但块中的更改开始并且没有结束。

231 CR/CRX
扫描找到当前块,但块中的更改开始并且没有结束。

6.4 跟踪长时间运行session10046事件

1, 使用sql_trace跟踪当前session10046事件

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跟踪当前session10046事件

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跟踪当前session10046事件

例如我们查看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.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

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

Return to Omaha Beach! World of Tanks launches Normandy commemoration event Return to Omaha Beach! World of Tanks launches Normandy commemoration event May 31, 2024 pm 10:25 PM

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 handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

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.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

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())

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

See all articles