Sql语句的跟踪查询
时间:2010-09-24 来源:sun5411
Sql语句的跟踪查询
一.跟踪流程图
·先在v$transcation中查询当前存在的事务和ses_addrà再查询v$session中对应的paddrà然后再查询v$process视图中对应的Spid,进而和系统pid对应起来。
·通过v$session中的sql_address,prev_sql_addr(上次执行语句的地址)和v$sql中的address对应关系查找到sql_text。
客户端上执行:
insert into empbak select * from employees where last_name='King';
插入一行数据大empbak表中
然后在oracle服务器端查询事务:
SQL> select addr,ses_addr from v$transaction
ADDR SES_ADDR
-------- --------
2FA0E8E4 30EF763C
从v$session中查询出事务对应的paddr
SQL> select saddr,sid,paddr,username,status from v$session where saddr='30EF763C';
SADDR SID PADDR USERNAME STATUS
-------- ---------- -------- ------------------------------ --------
30EF763C 142 30E1C370 HR INACTIVE
最后从v$process中查询出spid:
SQL> select addr,spid from v$process where addr='30E1C370';
ADDR SPID
-------- ------------
30E1C370 8413
在系统命令行里面查询系统进程
[root@sun ~]# netstat -nap |grep 8413
tcp 0 0 192.168.1.55:1521 192.168.1.101:1408 ESTABLISHED 8413/oraclecharge
[root@sun ~]# ps -ef|grep 8413
oracle 8413 1 0 01:19 ? 00:00:00 oraclecharge (LOCAL=NO)
root 9722 9664 0 02:22 pts/0 00:00:00 grep 8413
2,查询事务对应的sql语句
SQL>select saddr,sid,paddr,sql_address,prev_sql_addr,username,status from v$session where saddr='30EF763C'
SADDR SID PADDR SQL_ADDR PREV_SQL USERNAME STATUS
-------- ---------- -------- -------- -------- ------------------------------ --------
30EF763C 142 30E1C370 00 2AF9B7C4 HR INACTIVE
然后在v$sql中查询sql_text,既产生该事务的sql语句:
SQL> select sql_text,address from v$sql where address='2AF9B7C4';
SQL_TEXT ADDRESS
-------------------------------------------------------------------------------- --------
insert into empbak select * from employees where last_name='King' 2AF9B7C4
复合查询语句:
1. 查询客户端连接到服务器的进程和进程id:
SQL>select addr,sid,username,s.status,process,program from v$transaction t,v$session s where t.ses_addr=s.saddr;
ADDR SID USERNAME STATUS PROCESS PROGRAM
-------- ---------- ------------------------------ -------- ------------ ------------------------------------------------
2FA0E8E4 142 HR INACTIVE 4060 :2092 toad.exe