游标共享之cursor_sharing=EXACT

2014-11-24 01:21:40 · 作者: · 浏览: 7
游标共享之cursor_sharing=EXACT
cursor sharing用来告诉oracle什么情况下可以共享游标,即SQL重用。oracle默认cursor_sharing 是exact 指的是SQL语句必须绝对一样的情况下才能共享游标,否则作为新的SQL语句处理。
适合场景:使用exact 高效的前提是应用代码中使用了绑定变量,是最好的实践方式,这也是oracle推荐的。
参数还有两种选择:
游标共享之cursor_sharing=similar
游标共享之cursor_sharing=force
SQL> Drop table t purge;

表已删除。

SQL> show parameter cursor_sharing;
NAME                                                                  TYPE        VALUE
------------------------------------                            ----------- ------------------------------
_optimizer_extended_cursor_sharing          string      UDO
cursor_sharing                                                  string      EXACT


SQL> create table t as select * from dba_objects;
SQL> alter system flush shared_pool;

SQL> select /*+test_exact*/count(1) from t where object_id=100;
  COUNT(1)
----------
         1
SQL>
select /*+test_exact*/count(1) from t where object_id=200; COUNT(1) ---------- 1 SQL> col sql_text format a80; ----可以看到谓词不同则产生两条解析的SQL SQL> select sql_text from v$sql where sql_text like '%/*+test_exact*/%' and sql_text not like '%sql_text%'; SQL_TEXT -------------------------------------------------------------------------------- select /*+test_exact*/count(1) from t where object_id=100 select /*+test_exact*/count(1) from t where object_id=200 SQL> alter system flush shared_pool; SQL> var x number; SQL> exec :x:=100; SQL> select /*+test_exact*/count(1) from t where object_id=:x; COUNT(1) ---------- 1 SQL> exec :x:=200; SQL> select /*+test_exact*/count(1) from t where object_id=:x; COUNT(1) ---------- 1 ----在使用绑定变量的情况下只产生一次硬解析 SQL> select sql_text from v$sql where sql_text like '%/*+test_exact*/%' and sql_text not like '%sql_text%'; SQL_TEXT -------------------------------------------------------------------------------- select /*+test_exact*/count(1) from t where object_id=:x