游标共享之cursor_sharing=EXACT

本文详细介绍了Oracle数据库中cursor_sharing参数的作用及其设置方法。通过示例演示了在不同设置下(如exact、similar和force)如何影响SQL语句的重用情况,特别是在使用绑定变量时如何提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值