热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Oraclecursor_sharing参数详解

nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd

. 官网的说明

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:

1FORCE

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

 

2SIMILAR

       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.

 

3EXACT

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

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

 

Notes:

       1If 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.

       2The 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:

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

       2There 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.

       3Star transformation is not supported.

       4If 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:

       1Histogram 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.

       2Histogram 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

 

注意:

       对于相同的SQLoracle 在这里将不同的谓词值改成了变量,这样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 Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • MySQL数据库锁机制及其应用(数据库锁的概念)
    本文介绍了MySQL数据库锁机制及其应用。数据库锁是计算机协调多个进程或线程并发访问某一资源的机制,在数据库中,数据是一种供许多用户共享的资源,如何保证数据并发访问的一致性和有效性是数据库必须解决的问题。MySQL的锁机制相对简单,不同的存储引擎支持不同的锁机制,主要包括表级锁、行级锁和页面锁。本文详细介绍了MySQL表级锁的锁模式和特点,以及行级锁和页面锁的特点和应用场景。同时还讨论了锁冲突对数据库并发访问性能的影响。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • Windows7 64位系统安装PLSQL Developer的步骤和注意事项
    本文介绍了在Windows7 64位系统上安装PLSQL Developer的步骤和注意事项。首先下载并安装PLSQL Developer,注意不要安装在默认目录下。然后下载Windows 32位的oracle instant client,并解压到指定路径。最后,按照自己的喜好对解压后的文件进行命名和压缩。 ... [详细]
  • OCI连接MySQL_PLSQL Developer连接远程数据库OCI客户端安装方法
    本文介绍了使用OCI客户端连接MySQL和PLSQL Developer连接远程数据库的安装方法,避免了在本地安装Oracle数据库或类似的开发套件的麻烦,同时解决了PLSQL Dev连接远程Oracle时的配置问题。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • mysql-cluster集群sql节点高可用keepalived的故障处理过程
    本文描述了mysql-cluster集群sql节点高可用keepalived的故障处理过程,包括故障发生时间、故障描述、故障分析等内容。根据keepalived的日志分析,发现bogus VRRP packet received on eth0 !!!等错误信息,进而导致vip地址失效,使得mysql-cluster的api无法访问。针对这个问题,本文提供了相应的解决方案。 ... [详细]
  • 如何利用 Myflash 解析 binlog ?
    本文主要介绍了对Myflash的测试,从准备测试环境到利用Myflash解析binl ... [详细]
  • 本文介绍了关系型数据库和NoSQL数据库的概念和特点,列举了主流的关系型数据库和NoSQL数据库,同时描述了它们在新闻、电商抢购信息和微博热点信息等场景中的应用。此外,还提供了MySQL配置文件的相关内容。 ... [详细]
  • Jboss的EJB部署描述符standardjaws.xml配置步骤详解
    本文详细介绍了Jboss的EJB部署描述符standardjaws.xml的配置步骤,包括映射CMP实体EJB、数据源连接池的获取以及数据库配置等内容。 ... [详细]
author-avatar
Andy
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有