这两个概念很容易混淆。[@more@]概念: OPEN_CURSOR,定义每个Session最大能够打开的游标数量。在init.ora文件中定义,可以通过select * from v$parameter where name = 'open_cursors'查询。 V$OPEN_CURSOR,当前Session缓存的游标,而不是曾经打开的游标。 V$SESSION_CACHED_CURSOR,当前Session已经关闭并被缓存的游标。 V$OPEN_CURSOR中显示的当前Session游标缓存中游标,如果要精确查询当前Session打开的游标总数,需要从V$ SESSTAT中查询。
select a.value, s.username, s.sid, s.serial# from v$sesstat a, v$statname b, v$session s where a.statistic# = b.statistic# and s.sid=a.sid and b.name = 'opened cursors current';
Session Test: SQL> show parameter session_cached_cursors;
NAME TYPE VALUE ------------------------------ session_cached_cursors integer 0 SQL> select sid from v$mystat where rownum=1; SID ---------- 9 SQL> select sid from v$mystat where rownum=1; SID ---------- 9
Session Monitor: SQL> SELECT SID, n.NAME para_name, s.VALUE used 2 FROM SYS.v_$statname n, SYS.v_$sesstat s 3 WHERE n.NAME IN ('opened cursors current', 'session cursor cache count') 4 AND s.statistic# = n.statistic# 5 AND SID = 9; SID PARA_NAME USED --- ------------------------------ ---------- 9 opened cursors current 1 9 session cursor cache count 0 SQL> select SID, USER_NAME,SQL_TEXT from v$open_cursor where sid=9; SID USER_NAME SQL_TEXT --- ---------- ----------------------------------------------------------- 9 SCOTT select sid from v$mystat where rownum=1
Session Test: SQL> alter session set session_cached_cursors = 1; Session altered. SQL> show parameter session_cached_cursors; NAME TYPE VALUE ------------------------------------ ----------- ------------------------- session_cached_cursors integer 1
Session Test: SQL> select sid from v$mystat where rownum =1; SID ---------- 9 SQL> select sid from v$mystat where rownum =1; SID ---------- 9 SQL> select sid from v$mystat where rownum =1; SID ---------- 9
Session Monitor: SQL> select SID, USER_NAME,SQL_TEXT from v$open_cursor where sid=9; SID USER_NAME SQL_TEXT --- ---------- ----------------------------------------------------------- 9 SCOTT select sid from v$mystat where rownum =1 SQL> SELECT SID, n.NAME para_name, s.VALUE used 2 FROM SYS.v_$statname n, SYS.v_$sesstat s 3 WHERE n.NAME IN ('opened cursors current', 'session cursor cache count') 4 AND s.statistic# = n.statistic# 5 AND SID = 9; SID PARA_NAME USED --- ------------------------------ ---------- 9 opened cursors current 1 9 session cursor cache count 0
v$open_cursor dooes not show all open cursors. it shows more than that, the best option to find the number of open cursors is from v$sysstat. 通过如上的执行结果可以知道,即使同一个游标被打开3次,在SESSION_CACHED_CURSOR的数量仍然为0。
下面,将会在Session Test中关闭游标(通过执行一条其他的语句)。
Session Test: SQL> select * from t where rownum!=7; no rows selected
Session Monitor: SQL> SELECT SID, n.NAME para_name, s.VALUE used 2 FROM SYS.v_$statname n, SYS.v_$sesstat s 3 WHERE n.NAME IN ('opened cursors current', 'session cursor cache count') 4 AND s.statistic# = n.statistic# 5 AND SID = 9; SID PARA_NAME USED --- ------------------------------ ---------- 9 opened cursors current 1 9 session cursor cache count 1 SQL> select SID, USER_NAME,SQL_TEXT from v$open_cursor where sid=9; SID USER_NAME SQL_TEXT --- ---------- ----------------------------------------------------------- 9 SCOTT select sid from v$mystat where rownum=1 9 SCOTT select * from t where rownum!=7