[20201204]为什么返回2行记录.txt
--//在itpub问的问题,http://www.itpub.net/thread-2140116-1-1.html。花了一点点时间,大概知道问题在哪里,
--//问题出在一致性读取上,参考链接http://blog.itpub.net/267265/viewspace-2213824/ =>[20180907]访问v$视图与一致性读取.txt
1.问题提出:
SCOTT@book> @ ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
SCOTT@book> select sid from v$mystat where rownum=1;
SID
----------
32
SCOTT@book> select sid ,osuser from v$session where sid = 32;
SID OSUSER
---------- ------------------------------
32 oracle
--//返回1行.如果加入提示。
SCOTT@book> alter session set statistics_level = all;
Session altered.
SCOTT@book> select /*+ leading(s e w) */ sid ,osuser from v$session where sid = 32;
SID OSUSER
---------- ------------------------------
32 oracle
32 oracle
--//查看执行计划:
SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 41djy6556s87h, child number 1
-------------------------------------
select /*+ leading(s e w) */ sid ,osuser from v$session where sid = 32
Plan hash value: 1247622051
---------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| A-Rows | A-Time | OMem | 1Mem | Used-Mem |
---------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 1 (100)| 2 |00:00:00.01 | | | |
| 1 | NESTED LOOPS | | 1 | 1 | 108 | 0 (0)| 2 |00:00:00.01 | | | |
| 2 | MERGE JOIN CARTESIAN | | 1 | 1 | 82 | 0 (0)| 1367 |00:00:00.01 | | | |
|* 3 | FIXED TABLE FIXED INDEX| X$KSUSE (ind:1) | 1 | 1 | 69 | 0 (0)| 1 |00:00:00.01 | | | |
| 4 | BUFFER SORT | | 1 | 100 | 1300 | 0 (0)| 1367 |00:00:00.01 | 50176 | 50176 |45056 (0)|
| 5 | FIXED TABLE FULL | X$KSLED | 1 | 100 | 1300 | 0 (0)| 1367 |00:00:00.01 | | | |
|* 6 | FIXED TABLE FIXED INDEX | X$KSLWT (ind:1) | 1367 | 1 | 26 | 0 (0)| 2 |00:00:00.01 | | | |
---------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$88122447
3 - SEL$88122447 / S@SEL$4
5 - SEL$88122447 / E@SEL$4
6 - SEL$88122447 / W@SEL$4
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter(("S"."INDX"=32 AND "S"."INST_ID"=USERENV('INSTANCE') AND BITAND("S"."KSSPAFLG",1)<>0 AND BITAND("S"."KSUSEFLG",1)<>0))
6 - filter(("W"."KSLWTSID"=32 AND "W"."KSLWTEVT"="E"."INDX"))
--//你可以发现id=6,A-Row确实返回2行,为什么呢?
2.继续探究:
--//打开新的会话,原来会话不退出。
SCOTT@book> select /*+ leading(s e w) */ sid ,osuser from v$session where sid = 32;
SID OSUSER
---------- ------------------------------
32 oracle
--//相同的提示仅仅返回一行。再问为什么呢?
--//先猜测一下X$KSLWT,从名字上可以猜测是等待事件相关的视图。显示多加入几个字段:
SCOTT@book> select /*+ leading(s e w) */ sid ,osuser,EVENT#,event from v$session where sid = 32;
SID OSUSER EVENT# EVENT
---------- ------------------------------ ---------- ----------------------------------------
32 oracle 354 SQL*Net message from client
--//回到原来的会话,重复执行:
SCOTT@book> select /*+ leading(s e w) */ sid ,osuser,EVENT#,event from v$session where sid = 32;
SID OSUSER EVENT# EVENT
---------- ------------------------------ ---------- ----------------------------------------
32 oracle 350 SQL*Net message to client
32 oracle 354 SQL*Net message from client
--//这样就很好解析为什么返回2行了。视图不能保证一致性。
--//该语句执行时先出现SQL*Net message to client等待,然后是SQL*Net message from client等待。
--//由于视图一致性无法保证,就出现返回2行的情况。而另外的会话在不会出现这样的情况。