在Oracle數據庫中,咱們可否找到未提交事務(uncommit transactin)的SQL語句或其餘相關信息呢? 關於這個問題,咱們先來看看實驗測試吧。實踐出真知。算法
首先,咱們在會話1(SID=63)中構造一個未提交的事務,以下所:sql
SQL> create table test
2 as
3 select * from dba_objects;
Table created.
SQL> select userenv('sid') from dual;
USERENV('SID')
--------------
63
SQL> delete from test where object_id=12;
1 row deleted.
SQL>
而後咱們在會話2(SID=70)中,咱們使用下面SQL查詢未提交的SQL語句。以下所示:數據庫
SQL> select userenv('sid') from dual;
USERENV('SID')
--------------
70
SQL>
SQL> SET SERVEROUTPUT ON SIZE 99999;
SQL> EXECUTE PRINT_TABLE('SELECT SQL_TEXT FROM V$SQL S,V$TRANSACTION T WHERE S.LAST_ACTIVE_TIME=T.START_DATE');
SQL_TEXT : delete from test where object_id=12
-----------------
SQL_TEXT : select
grantee#,privilege#,nvl(col#,0),max(mod(nvl(option$,0),2))from objauth$ where
obj#=:1 group by grantee#,privilege#,nvl(col#,0) order by grantee#
-----------------
SQL_TEXT : SELECT /* OPT_DYN_SAMP */ /*+ ALL_ROWS
IGNORE_WHERE_CLAUSE NO_PARALLEL(SAMPLESUB)
opt_param('parallel_execution_enabled', 'false') NO_PARALLEL_INDEX(SAMPLESUB)
NO_SQL_TUNE */ NVL(SUM(C1),0), NVL(SUM(C2),0) FROM (SELECT /*+
IGNORE_WHERE_CLAUSE NO_PARALLEL("TEST") FULL("TEST") NO_PARALLEL_INDEX("TEST")
*/ 1 AS C1, CASE WHEN "TEST"."OBJECT_ID"=12 THEN 1 ELSE 0 END AS C2 FROM "TEST"
SAMPLE BLOCK (6.134372 , 1) SEED (1) "TEST") SAMPLESUB
-----------------
SQL_TEXT : select col#, grantee#,
privilege#,max(mod(nvl(option$,0),2)) from objauth$ where obj#=:1 and col# is
not null group by privilege#, col#, grantee# order by col#, grantee#
-----------------
SQL_TEXT : select
type#,blocks,extents,minexts,maxexts,extsize,extpct,user#,iniexts,NVL(lists,6553
5),NVL(groups,65535),cachehint,hwmincr,
NVL(spare1,0),NVL(scanhint,0),NVL(bitmapranges,0) from seg$ where ts#=:1 and
file#=:2 and block#=:3
-----------------
PL/SQL procedure successfully completed.
如上所示,這個SQL咱們會查出不少不相關的SQL語句,接下來咱們能夠用下面的SQL查詢(改用SQL Developer展現,由於SQL*Plus,不方便展現),以下所示,這個SQL倒不會查出不相關的SQL。可是這個SQL能勝任任何場景嗎? 答案是否認的。緩存
SELECT S.SID
,S.SERIAL#
,S.USERNAME
,S.OSUSER
,S.PROGRAM
,S.EVENT
,TO_CHAR(S.LOGON_TIME,'YYYY-MM-DD HH24:MI:SS')
,TO_CHAR(T.START_DATE,'YYYY-MM-DD HH24:MI:SS')
,S.LAST_CALL_ET
,S.BLOCKING_SESSION
,S.STATUS
,(
SELECT Q.SQL_TEXT
FROM V$SQL Q
WHERE Q.LAST_ACTIVE_TIME=T.START_DATE
AND ROWNUM<=1) AS SQL_TEXT
FROM V$SESSION S,
V$TRANSACTION T
WHERE S.SADDR = T.SES_ADDR;
咱們知道,在ORACLE裏第一次執行一條SQL語句後,該SQL語句會被硬解析,並且執行計劃和解析樹會被緩存到Shared Pool裏。方便之後再次執行這條SQL語句時不須要再作硬解析。可是Shared Pool的大小也是有限制的,不可能無限制的緩存全部SQL的執行計劃,它使用LRU算法管理庫高速緩存區。因此有可能你要找的SQL語句已經不在Shared Pool裏面了,它從Shared Pool被移除出去了。以下所示,咱們使用sys.dbms_shared_pool.purge人爲構造SQL被移除出Shared Pool的狀況。以下所示:oracle
SQL> col sql_text for a80;
SQL> select sql_text
2 ,sql_id
3 ,version_count
4 ,executions
5 ,address
6 ,hash_value
7 from v$sqlarea where sql_text
8 like 'delete from test%';
SQL_TEXT SQL_ID VERSION_COUNT EXECUTIONS ADDRESS HASH_VALUE
------------------------------------ ------------- ------------- ---------- ---------------- ----------
delete from test where object_id=12 5xaqyzz8p863u 1 1 0000000097FAE648 3511949434
SQL> exec sys.dbms_shared_pool.purge('0000000097FAE648,3511949434','C');
PL/SQL procedure successfully completed.
SQL>
此時咱們查詢到的SQL語句,是一個不相關的SQL或者其值爲Null。app
接下來咱們回滾SQL語句,而後繼續新的實驗測試,以下所示,在會話1(SID=63)裏面執行了兩個DML操做語句,都未提交事務。測試
SQL> delete from test where object_id=12;
1 row deleted.
SQL> update test set object_name='kkk' where object_id=14;
1 row updated.
SQL>
接下來,咱們使用SQL語句去查找未提交的SQL,發現只能捕獲最開始執行的DELETE語句,不能捕獲到後面執行的UPDATE語句。這個實驗也從側面印證了,咱們不必定能準確的找出未提交事務的SQL語句。spa
因此結合上面實驗,咱們基本上能夠給出結論,咱們不必定能準確找出未提交事務的SQL語句,這個要視狀況或場景而定。存在這不肯定性。code
參考資料:orm
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:9523503800346688981