当前位置:  数据库>oracle

Oracle cursor_sharing 参数详解

    来源: 互联网  发布时间:2017-04-06

    本文导语: 一. 官网的说明 http://download.Oracle.com/docs/cd/E11882_01/server.112/e17110/initparams042.htm#REFRN10025   1.1 CURSOR_SHARING Property Description Parameter type String Syntax CURSOR_SHARING = { SIMILAR | EXACT | FORCE } Default value EXACT Modifiable ALTER SESSION, ALTER ...

一. 官网的说明

http://download.Oracle.com/docs/cd/E11882_01/server.112/e17110/initparams042.htm#REFRN10025

 

1.1 CURSOR_SHARING

Property

Description

Parameter type

String

Syntax

CURSOR_SHARING = { SIMILAR | EXACT | FORCE }

Default value

EXACT

Modifiable

ALTER SESSION, ALTER SYSTEM

Basic

No

 

       CURSOR_SHARING determines what kind of SQL statements can share the same cursors.

 

Values:

(1)FORCE

       Allows the creation of a new cursor if sharing an existing cursor, or if the cursor plan is not optimal.

 

(2)SIMILAR

       Causes statements that may differ in some literals, but are otherwise identical, to share a cursor, unless the literals affect either the meaning of the statement or the degree to which the plan is optimized.

 

(3)EXACT

       Only allows statements with identical text to share the same cursor.

       --只有SQL 语句完全相同的情况下,才会使用相同的cursor,即执行计划。

 

Notes:

       (1)If you set CURSOR_SHARING, then Oracle recommends the FORCE setting unless you are in a DSS environment. FORCE limits the growth of child cursors that can occur when the setting is SIMILAR.

       (2)The value of the CURSOR_SHARING parameter has performance implications. Refer to Oracle Database Performance Tuning Guide before setting this parameter.

 

 

 

1.2 When to Set CURSOR_SHARING to a Nondefault Value

       The best practice is to write sharable SQL and use the default of EXACT for CURSOR_SHARING. However, for applications with many similar statements, setting CURSOR_SHARING can significantly improve cursor sharing, resulting in reduced memory usage, faster parses, and reduced latch contention. Consider this approach when statements in the shared pool differ only in the values of literals, and when response time is poor because of a very high number of library cache misses.

 

Setting CURSOR_SHARING to FORCE or SIMILAR has the following drawbacks:

       (1)The database must perform extra work during the soft parse to find a similar statement in the shared pool.

       (2)There is an increase in the maximum lengths (as returned by DESCRIBE) of any selected expressions that contain literals in a SELECT statement. However, the actual length of the data returned does not change.

       (3)Star transformation is not supported.

       (4)If stored outlines were generated with CURSOR_SHARING set to EXACT, then the database does not use stored outlines generated with literals. To avoid this problem, generate outlines with CURSOR_SHARING set to FORCE or SIMILAR and use the CREATE_STORED_OUTLINES parameter.

 

 

       When deciding whether to set CURSOR_SHARING to FORCE or SIMILAR, consider the performance implications of each setting.

       When CURSOR_SHARING is set to FORCE, the database uses one parent cursor and one child cursor for each distinct SQL statement. The database uses the same plan for each execution of the same statement.

 

 

       When set to SIMILAR, database behavior depends on the presence of histograms:

       (1)Histogram absent for column with system-generated bind value

              Only one parent cursor and one child cursor exists for each distinct SQL statement. In this case, all executions of a SQL statement use the same plan.

       (2)Histogram present for column with system-generated bind value

              If the same SQL statement is executed multiple times, each execution has its own child cursor. In this case, the database peeks at bind variable values and create a new child cursor for each distinct value. Thus, each statement execution uses a plan based on the specific literals in the statement.

 

For example, consider the following statement:

       SELECT * FROM hr.employees WHERE employee_id = 101

 

       If FORCE is used, or if SIMILAR is used when no histogram exists, then the database optimizes this statement as if it contained a bind variable and uses bind peeking to estimate cardinality. Statements that differ only in the bind variable share the same execution plan.

       If SIMILAR is used, and if a histogram does exist, then the database does not treat the statement as if a bind variable were used. The same query for a different employee may not use the same plan.

 

       If you set CURSOR_SHARING, then Oracle recommends the FORCE setting unless you are in a DSS environment. FORCE limits the growth of child cursors that can occur when the setting is SIMILAR.

       Also, function-based indexes may not work when using SIMILAR because the database converts index parameters to bind variables.

       For example, if the index is SUBSTR(id,1,3), then the database converts it to SUBSTR("ID",:SYS_B_0,:SYS_B_1)=:id, rendering the index invalid.

  

二. 测试

2.1 cursor_sharing=exact,这是cursor_sharing的默认值

 

2.1.1 查看cursor_sharing 值

SYS@anqing2(rac2)> show parameter cursor_sharing

 

NAME           TYPE          VALUE

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

cursor_sharing       string         EXACT

 

2.1.2 查看当前硬解析值

SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';

 

NAME                                VALUE

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

parse time cpu                    1882056

parse time elapsed                2648194

parse count (total)              12780229

parse count (hard)                9890010(硬解析次数)

parse count (failures)                 71

 

2.1.3 执行一条select 语句,然后查看硬解析次数

SYS@anqing2(rac2)> select * from ta where id=168;

        ID NAME

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

       168 dave

 

SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';

 

NAME                                VALUE

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

parse time cpu                    1882061

parse time elapsed                2648196

parse count (total)              12780360

parse count (hard)                9890021

parse count (failures)                 71

-- 这里硬解析的次数加一,因为之前SQL 没有解析过,所以需要进行硬解析之后才能执行。

 

2.1.4 执行与之前类似的SQL,谓词值不一样

SYS@anqing2(rac2)> select * from ta where id=198;

 

        ID NAME

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

       198 dave

 

SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';

 

NAME                                VALUE

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

parse time cpu                    1882061

parse time elapsed                2648196

parse count (total)              12780482

parse count (hard)                9890022

parse count (failures)                 71

-- 硬解析次数又加1了,没有重用之前的执行计划

 

2.1.5 执行相同的SQL 语句

SYS@anqing2(rac2)> select * from ta where id=198;

 

        ID NAME

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

       198 dave

 

SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';

 

NAME                                VALUE

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

parse time cpu                    1882061

parse time elapsed                2648196

parse count (total)              12780543

parse count (hard)                9890022

parse count (failures)                 71

-- 测试硬解析没有变化。 重用之前的cursor。

 

总结:

       在这种模式下,只有SQL 语句完全相同的情况下,才会使用相同的cursor,即执行计划。

       这种模式下,表有统计信息和没有统计信息的执行计划是有出入的。 所以该模式下的表,需要定期的去收集统计信息。

 

2.2 cursor_sharing=force

--修改cursor_sharing 值

SYS@anqing2(rac2)> alter session set cursor_sharing='force';

Session altered.

SYS@anqing2(rac2)> show parameter cursor_sharing

NAME           TYPE           VALUE

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

cursor_sharing     string          force

 

--查看硬解析次数

SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';

NAME                                VALUE

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

parse time cpu                    1882075

parse time elapsed                2648219

parse count (total)              12782090

parse count (hard)                9890067 (硬解析次数)

parse count (failures)                 71

 

-- select 查询

SYS@anqing2(rac2)> select * from ta where id=88;

        ID NAME

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

        88 dave

 

SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';

NAME                                VALUE

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

parse time cpu                    1882075

parse time elapsed                2648219

parse count (total)              12782215

parse count (hard)                9890068 -- 硬解析次数加一

parse count (failures)                 71

 

-- 执行相同的select,但谓词值不一样

SYS@anqing2(rac2)> select * from ta where id=99;

 

        ID NAME

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

        99 dave

 

SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';

 

NAME                                VALUE

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

parse time cpu                    1882075

parse time elapsed                2648219

parse count (total)              12782285

parse count (hard)                9890068

parse count (failures)                 71

--注意,这里的硬解析次数没有变化,这个就是force 的作用。只要sql语句相同,不管谓词值是否相同,都会当成相同的sql,重用之前的cursor,不会进行硬解析。

 

-- 查看child cursor 信息

SYS@anqing2(rac2)> select sql_text,child_number from v$sql where sql_text like 'select * from ta where%';

 

 

SQL_TEXT                                 CHILD_NUMBER

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

select * from ta where id=:"SYS_B_0"                0

select * from ta where id=:"SYS_B_0"                1

select * from ta where id=:"SYS_B_0"                2

 

注意:

       对于相同的SQL,oracle 在这里将不同的谓词值改成了变量,这样SQL_TEXT 就相同,正常情况下,应该使用同一个cursor,即执行计划,但是在我上面的查询中,Oracle 并没有重用,而是重新生成了一个child_cursor.  这就说明Oracle 认为这个cursor 并不是最优的,所有重新生成了一个。

 

可以通过如下SQL 查看为什么没有重用之前的cursor:

       SQL>select * from v$sql_shared_cursor where sql_id='c9swtz4spq3xz';

 

如果这里有Y,就是导致不能重用的原因。

  

总结:

       Allows the creation of a new cursor if sharing an existing cursor, or if the cursor plan is not optimal.

 

       When CURSOR_SHARING is set to FORCE, the database uses one parent cursor and one child cursor for each distinct SQL statement. The database uses the same plan for each execution of the same statement.

 

       FORCE limits the growth of child cursors that can occur when the setting is SIMILAR.

 

       当cursor_sharing 设置为force时, Oracle 会把相同SQL的不同谓词值转换成变量,这样SQL_TEXT就看上去一样。 Oracle 就会使用一个相同的cursor。 这样他们的执行计划也是一样的。

       当Oracle 认为存在的cursor 不是最优的时候,就会重新创建一个child cursor,而不重用之前的已经存在cursor。 可以通过v$sql_shared_cursor 查看为什么没有重用。

       这样就会和我们上面查询的一样,会有多个child cursor,但是他们的parent cursor是一样的。 这个child cursor 不是无限增常的,force 和similar 都会限制child cursor 的增长。

 

 


    
 
 

您可能感兴趣的文章:

  • Oracle 数据库(oracle Database)性能调优技术详解
  • oracle中lpad函数的用法详解
  • oracle修改scott密码与解锁的方法详解
  • 求.bash_profile配置oracle详解
  • Oracle数据库中分区功能详解
  • oracle指定排序的方法详解
  • 详解如何应用改变跟踪技术加速Oracle递增备份
  • oracle合并列的函数wm_concat的使用详解
  • oracle select执行顺序的详解
  • 使用Oracle数据挖掘API方法详解[图文]
  • Oracle多表级联更新详解
  • 安装Linux与Oracle数据库步骤详解
  • oracle求同比,环比函数(LAG与LEAD)的详解
  • 详解Linux平台下的Oracle数据库编程
  • oracle中去掉回车换行空格的方法详解
  • Oracle中job的使用详解
  • [Oracle] Data Guard 之 Redo传输详解
  • oracle用户权限管理使用详解
  • 深入ORACLE变量的定义与使用的详解
  • 详解Oracle的几种分页查询语句
  • oracle SQL递归的使用详解
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 深入解析Oracle参数及参数文件
  • Oracle数据库访问参数文件的顺序
  • Oracle初始参数与当前用户
  • Oracle中serveroutput参数一次设置永久保存方法
  • Oracle的spfile参数文件
  • Linux Oracle RAC内核参数
  • 怎么在java中向一个sql语句传参数,就像oracle的proc一样啊?
  • Sun Solaris运行Oracle数据库所需的内核参数
  • Linux下用SHELL脚本执行带输入输出参数的ORACLE存储过程并得到结果
  • Oracle Streams存储过程中的一些参数
  • 快速修复Oracle参数文件的另类方法
  • jps连接oracle9i数据库出现内部参数错
  • oracle 参数文件audit_trail的认识
  • 在linux下安装oracle时,使用sysctl检查系统参数时出现许多error信息,不知道怎么回事,望高手指点!
  • 每日Oracle:配置日志模式的相关参数log_archive_des
  • Oracle 子程序参数模式,IN,OUT,NOCOPY
  • Oracle 使用set修改数据库运行参数
  • oracle impdp network_link参数使用介绍
  • Oracle数据库中系统初始化参数分析
  • Oracle 启动例程 STARTUP参数说明
  • Oracle 12c发布简单介绍及官方下载地址
  • 在linux下安装oracle,如何设置让oracle自动启动!也就是让oracle那个服务自动启动,不是手动的
  • oracle 11g最新版官方下载地址
  • 请问su oracle 和su - oracle有什么不同?
  • Oracle 数据库(oracle Database)Select 多表关联查询方式
  • 虚拟机装Oracle R12与Oracle10g
  • Oracle数据库(Oracle Database)体系结构及基本组成介绍
  • Oracle 数据库开发工具 Oracle SQL Developer
  • 如何设置让Oracle SQL Developer显示的时间包含时分秒
  • Oracle EBS R12 支持 Oracle Database 11g
  • Oracle 10g和Oracle 11g网格技术介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3