Oracle資料庫查看一個進程是如何執行相關的實際SQL語句
複製代碼 代碼如下:SELECT b.sql_text, sid, serial#, osuser, machine
FROM v$session a, v$sqlarea b
WHERE a.sql_address = b.address;
查詢前台發出的SQL語句.
複製代碼 代碼如下:select user_name,sql_text
from v$open_cursor
where sid in
(select sid from (select sid,serial#,username,program from v$session where status='ACTIVE'));
根據SPID查詢session
複製代碼 代碼如下:SELECT * FROM v$session WHERE paddr IN
(SELECT addr FROM v$process WHERE spid=&spid);
根據SID查詢process
複製代碼 代碼如下:SELECT * FROM v$process WHERE addr IN
(SELECT paddr FROM v$session WHERE sid=&sid);
DBA如何查詢其他使用者所進行的操作
複製代碼 代碼如下:SELECT sql_text
FROM v$sql t1, v$session t2
WHERE t1.address = t2.sql_address
AND t2.sid = &sid;
根據process查詢sql語句
複製代碼 代碼如下:SELECT sql_text
FROM v$sqltext a
WHERE (a.hash_value, a.address) IN (
SELECT DECODE (sql_hash_value,0, prev_hash_value,sql_hash_value ),
DECODE (sql_hash_value, 0, prev_sql_addr, sql_address)
FROM v$session b
WHERE b.paddr = (SELECT addr
FROM v$process c
WHERE c.spid = '$processID'))
ORDER BY piece ASC;
其他
執行下列命令語句複製代碼 代碼如下:sqlplus / as sysdba <<EOF
create tablespace test datafile '/data/test01.dbf' size 10240M;
quit;
EOF
通過ps -ef|grep sqlplus命令得到上面所執行的命令的進程id為:12345
(1)關於v$process
執行下面的SQL是查不到相關的資訊:
select * from v$process where spid='12345';
因為這個spid欄位對應的並不是我們用ps命令從系統中查詢到的進程id,而是這個進程執行的當前SQL的進程id,
也就是上面命令中的“create tablespace test datafile '/data/test01.dbf' size 10240M;”所對應的進程id,如果想
通過用ps命令從系統中查詢到的進程id查看對應的資訊,那麼必須使用下面語句:
select spid,sid,process,sql_address from v$session where process='12345'
上面sql中的process就是通過ps查看的進程id,而spid就是裡面的sql語句所對應的進程id。
還可以通過上面的sql_address 查看正在執行的SQL語句內容:
select sql_text from v$sqlarea s,v$session ses where s.address=ses.sql_address and ses.process='12345';
(2)關於v$session
在查詢 v$session 視圖的時候,我們根據command欄位內部表示解碼每一個欄位,當我們需要快速找出他們的 Oracle 系統的內部情況時非常有用。複製代碼 代碼如下:select
substr(s.username,1,18) username,substr(s.program,1,15) program,p.spid,s.process,
decode(s.command,
0,'No Command',
1,'Create Table',
2,'Insert',
3,'Select',
6,'Update',
7,'Delete',
9,'Create Index',
15,'Alter Table',
21,'Create View',
23,'Validate Index',
35,'Alter Database',
39,'Create Tablespace',
41,'Drop Tablespace',
40,'Alter Tablespace',
53,'Drop User',
62,'Analyze Table',
63,'Analyze Index',
s.command||': Other') command
from
v$session s,
v$process p,
v$transaction t,
v$rollstat r,
v$rollname n
where s.paddr = p.addr
and s.taddr = t.addr (+)
and t.xidusn = r.usn (+)
and r.usn = n.usn (+)
order by username
(3)幾個相關的SQL
--查看系統進程對應的資訊
複製代碼 代碼如下:select se.saddr,se.sid,se.serial#,p.pid,se.paddr,s.sql_id,s.sql_text
from v$session se ,v$process p, v$sqlarea s
where se.paddr=p.addr and se.sql_address=s.address and se.process='&1'
and se.username is not null
--查看所有的會話
複製代碼 代碼如下:select se.username,se.saddr,se.sid,se.serial#,se.process,s.sql_id
from v$session se,v$sqlarea s
where se.sql_address=s.address
--查看會話對應的sql內容
複製代碼 代碼如下:select se.username,se.process,s.sql_text
from v$session se,v$sqlarea s
where se.sql_address=s.address and s.sql_id='&1'