【优化】参数SESSION_CACHED_CURSORS与解析之间的关系

一:参数SESSION_CACHED_CURSORS,就是说的是一个session可以缓存多少个cursor(软软解析),让后续相同的SQL语句不再打开游标,从而避免软解析的过程来提高性能,软解析同硬解析一样,比较消耗资源

二:open_cursors设定每个session(会话)最多能同时打开多少个cursor(游标),是相对于SPA中的share poollibarary cache而言session_cached_cursor设定每个session(会话)最多可以缓存多少个关闭掉的cursor,是相对于PGA而言

三:另外检查SESSION_CACHED_CURSORS参数是否设置的合理,可以从两个statistic来检查,即select name,value from v$sysstat where name like '%cursor%';select name,value from v$sysstat where name like '%parse%';

 

1.创造环境:

SYS@ORA11GR2>create table t1 as select * from all_objects;

SYS@ORA11GR2>desc t1

SYS@ORA11GR2>select count(*) from t1;

 

  COUNT(*)

----------

     84999

 

2.当session_cached_cursors50进行操作

SYS@ORA11GR2>show parameter session_cached_cursors

 

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

session_cached_cursors               integer     50

 

1)直接进行查询操作(无绑定变量):

SYS@ORA11GR2>select count(*) from t1 where object_id=101;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>select count(*) from t1 where object_id=102;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>select count(*) from t1 where object_id=103;

 

  COUNT(*)

----------

         1

 

验证查看解析执行情况:

SYS@ORA11GR2>select sql_text,s.parse_calls,loads,executions from v$sql s where sql_text like 'select count(*) from t1%' order by 1,2,3,4;

 

SQL_TEXT                                 PARSE_CALLS   LOADS   EXECUTIONS

---------------------------------------- ----------- ---------- ----------

select count(*) from t1 where object_id=101           1          1          1

select count(*) from t1 where object_id=102           1          1          1

select count(*) from t1 where object_id=103           1          1          1

三次执行SQL都是硬解析,都是解析1次,执行一次;

 

2)用绑定变量执行sql查询语句:

SYS@ORA11GR2>var v_id number;

SYS@ORA11GR2>exec :v_id:=101;

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select count(*) from t1 where object_id=:v_id;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>exec :v_id:=102;

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select count(*) from t1 where object_id=:v_id;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>exec :v_id:=103;

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select count(*) from t1 where object_id=:v_id;

 

  COUNT(*)

----------

         1

 

验证查看解析执行情况:

SYS@ORA11GR2>select sql_text,s.parse_calls,loads,executions from v$sql s where sql_text like 'select count(*) from t1%' order by 1,2,3,4;

 

SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS

---------------------------------------- ----------- ---------- ----------

select count(*) from t1 where object_id=101           1          1          1

select count(*) from t1 where object_id=102           1          1          1

select count(*) from t1 where object_id=103           1          1          1

select count(*) from t1 where object_id=:v_id         3          1          3


绑定变量执行的后执行三次,硬解析1此,软解析2次,共解析3次,执行了3次,软解析发挥了作用;

 
    

SYS@ORA11GR2>exec :v_id:=104;

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select count(*) from t1 where object_id=:v_id;

 

  COUNT(*)

----------

         1
      

再次验证查看解析执行情况:

SYS@ORA11GR2>select sql_text,s.parse_calls,loads,executions from v$sql s where sql_text like 'select count(*) from t1%' order by 1,2,3,4;

 

SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS

---------------------------------------- ----------- ---------- ----------

select count(*) from t1 where object_id=101           1          1          1

select count(*) from t1 where object_id=102           1          1          1

select count(*) from t1 where object_id=103           1          1          1

select count(*) from t1 where object_id=:v_id         4          1          4

3)绑定变量循环执行五次操作:

begin
  for i in 1 .. 5 loop
    execute immediate 'select count(*) from t1 where object_id=:1'
      using i + 100;
  end loop;
end;
/

SYS@ORA11GR2>begin

  2   for i in 1..5 loop

  3     execute immediate 'select count(*) from t1 where object_id=:1' using i+100;

  4   end loop;

  5  end;

  6  /

 

PL/SQL procedure successfully completed.

 

验证查看解析执行情况:

SYS@ORA11GR2>select sql_text,s.parse_calls,loads,executions from v$sql s where sql_text like 'select count(*) from t1%' order by 1,2,3,4;

 

SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS

---------------------------------------- ----------- ---------- ----------

select count(*) from t1 where object_id=101           1          1       1 

select count(*) from t1 where object_id=102           1          1       1

select count(*) from t1 where object_id=103           1          1       1

select count(*) from t1 where object_id=:1            1          1        5 

SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS

---------------------------------------- ----------- ---------- ----------

select count(*) from t1 where object_id=:v_id         3          1       3


即总共解析了1次,而执行了五次,剩余四次的执行也没有经过解析就直接执行了,即软软解析

软软解析,定义为同一sql连续执行了三次以上,则将sql的执行计划放到server processpga中,下次再执行同一sql时跳过解析阶段直接执行。

 

 

3.session_cached_cursors=0时进行操作:

1)修改session_cached_cursor参数值为0

SYS@ORA11GR2>show parameter session_cached_cursor

 

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

session_cached_cursors               integer     50

SYS@ORA11GR2>alter system set session_cached_cursors=0 scope=spfile;

 

System altered.

 

SYS@ORA11GR2>shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SYS@ORA11GR2>startup

ORACLE instance started.

 

Total System Global Area  830930944 bytes

Fixed Size                  2257800 bytes

Variable Size             503319672 bytes

Database Buffers          322961408 bytes

Redo Buffers                2392064 bytes

Database mounted.

Database opened.

SYS@ORA11GR2>show parameter session_cached_cursor

 

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

session_cached_cursors               integer     0

 

2)直接进行查询操作(无绑定变量):

SYS@ORA11GR2>select count(*) from t1 where object_id=101;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>select count(*) from t1 where object_id=102;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>select count(*) from t1 where object_id=103;

 

  COUNT(*)

----------

         1

 

验证查看解析执行情况:

SYS@ORA11GR2>select sql_text,s.parse_calls,loads,executions from v$sql s where sql_text like 'select count(*) from t1%' order by 1,2,3,4;

 

SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS

---------------------------------------- ----------- ---------- ----------
select count(*) from t1 where object_id=101           1          1          1
select count(*) from t1 where object_id=102           1          1          1
select count(*) from t1 where object_id= 103          1          1          1

三次硬解析

 

3)用绑定变量执行sql查询语句:

SYS@ORA11GR2>var v_id number;

SYS@ORA11GR2>exec :v_id:=101;

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select count(*) from t1 where object_id=:v_id;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>exec :v_id:=102;

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select count(*) from t1 where object_id=:v_id;

 

  COUNT(*)

----------

         1

 

SYS@ORA11GR2>exec :v_id:=103;

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select count(*) from t1 where object_id=:v_id;

 

  COUNT(*)

----------

         1

验证查看解析执行情况:

SYS@ORA11GR2>select sql_text,s.parse_calls,loads,executions from v$sql s where sql_text like 'select count(*) from t1%' order by 1,2,3,4;

 

SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS

---------------------------------------- ----------- ---------- ----------

select count(*) from t1 where object_id=101           1          1          1

select count(*) from t1 where object_id=102           1          1          1

select count(*) from t1 where object_id=103           1          1          1

select count(*) from t1 where object_id=:v_id       3          1          3


软解析,共
3次解析,后2次软解析,执行3次;

 

4)绑定变量循环执行五次操作:
begin
  for i in 1 .. 5 loop
    execute immediate 'select count(*) from t1 where object_id=:1'
      using i + 100;
  end loop;
end;
/

SYS@ORA11GR2>begin

  2   for i in 1..5 loop

  3     execute immediate 'select count(*) from t1 where object_id=:1' using i+100;

  4   end loop;

  5  end;

  6  /

 

PL/SQL procedure successfully completed.

 

验证查看解析执行情况:

SYS@ORA11GR2>select sql_text,s.parse_calls,loads,executions from v$sql s where sql_text like 'select count(*) from t1%' order by 1,2,3,4;

 

SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS

---------------------------------------- ----------- ---------- ----------

select count(*) from t1 where object_id=101           1          1          1

select count(*) from t1 where object_id=102           1          1          1

select count(*) from t1 where object_id=103           1          1          1

select count(*) from t1 where object_id=:1           5          1          5

select count(*) from t1 where object_id=:v_id        3          1          3


此次没有参数软软解析,表明当session_cached_cursors值设为0时,不产生软软解析(不管同一语句执行了多少次);

 

open_cursorssession_cached_cursor

a、两个参数之间没有任何关系,相互也不会有任何影响。

b、两个参数有着相同的作用:让后续相同的sql语句不在打开游标,从而避免软解析过程来提供应用程序的效率。

Cpga中的cursor(软软解析)管理采用LRU原则当一个session打算parse一个sql,它会先去pga内搜索session cursor cache list,如果找到那么会把这个cursor脱离list,然后当关闭的时候再把这个cursor加到MRU.session_cached_cursor提供了快速软分析的功能,提供了比soft parse更高的性能.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31397003/viewspace-2126425/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31397003/viewspace-2126425/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值