热门标签 | 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 的增长。

 

 


推荐阅读
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • mysql-cluster集群sql节点高可用keepalived的故障处理过程
    本文描述了mysql-cluster集群sql节点高可用keepalived的故障处理过程,包括故障发生时间、故障描述、故障分析等内容。根据keepalived的日志分析,发现bogus VRRP packet received on eth0 !!!等错误信息,进而导致vip地址失效,使得mysql-cluster的api无法访问。针对这个问题,本文提供了相应的解决方案。 ... [详细]
  • ubuntu用sqoop将数据从hive导入mysql时,命令: ... [详细]
  • 如何在php中将mysql查询结果赋值给变量
    本文介绍了在php中将mysql查询结果赋值给变量的方法,包括从mysql表中查询count(学号)并赋值给一个变量,以及如何将sql中查询单条结果赋值给php页面的一个变量。同时还讨论了php调用mysql查询结果到变量的方法,并提供了示例代码。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • MySQL中的MVVC多版本并发控制机制的应用及实现
    本文介绍了MySQL中MVCC的应用及实现机制。MVCC是一种提高并发性能的技术,通过对事务内读取的内存进行处理,避免写操作堵塞读操作的并发问题。与其他数据库系统的MVCC实现机制不尽相同,MySQL的MVCC是在undolog中实现的。通过undolog可以找回数据的历史版本,提供给用户读取或在回滚时覆盖数据页上的数据。MySQL的大多数事务型存储引擎都实现了MVCC,但各自的实现机制有所不同。 ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
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社区 版权所有