`

Oracle 11g PL/SQL Function Result Cache(原创)

 
阅读更多

The PL/SQL Function Result Cache
The PL/SQL function result cache uses the same infrastructure as the SQL query result cache, and caches the results of the PL/SQL functions in the result cache component of the SGA. Ideal candidates for caching are functions that the database invokes frequently but which depend on information that changes infrequently or never. If you invoke a function with different combinations of parameter values, the database will cache one result for every unique combination of parameter values. The database uses the input parameters of the function as the lookup key. As with the SQL result cache, the database employs a least recently used algorithm to age out cached results. You can optionally specify the database objects that the cached result depends on, and the database will invalidate the cached results when those database objects change.

How the PL/SQL Function Cache Works
The very first time you execute the body of a result-cached PL/SQL function with a set of parameter values, the function will execute. The function will re-execute under the following circumstances:

  • When the cached result for the parameter values is invalid because an object specified in the relies_on clause has changed
  • When the function bypasses the result cache
  • When the cached result for the set of parameter values has aged out because the system needs memory

Creating a Cacheable Function
To make the database cache the results of a PL/SQL function, simply include the result_cache clause in the function definition. You can optionally specify the relies_on clause to make the database invalidate the cache when the database modifies any of the listed tables or views. The following example shows how to create a function that specifies that the database cache its results:
SQL> create or replace function
     get_dept_info (dept_id number) return dept_info_record
     result_cache relies_on (employees)
     is
     rec dept_info_record;
     begin
          select avg(salary), count(*) into rec
            from employees
           where department_id = dept_id;
          return rec;
     end get_dept_info;
     /
The GET_DEPT_INFO function fetches the number of employees and their average salary from a department that you specify. The result_cache clause ensures that the database saves the results of the function’s execution in the result cache. The optional relies_on clause specifies that the database must invalidate the cached results of this function whenever the EMPLOYEES table changes.

Note: The RELIES ON clause is unnecessary in 11.2 as it automatically tracks dependencies and invalidates the cached results when necessary.

Restrictions
In order for the database to cache its results, a function must satisfy all of these criteria:

  • It can’t be a pipelined table function.
  • It can’t have any out or in out parameters.
  • It is not defined in an anonymous block; it must be a named function.
  • It isn’t defined in a module that has invoker’s rights.
  • It can’t have any in parameters belong to the LOB type, ref cursor, and collection, object, or record types.
  • In addition, the function must not have any side effects or depend on session- specific settings or session-specific application contexts.

The Client Query Result cache

In addition to the SQL result cache and the PL/SQL function result cache, which are server-side caches, Oracle Database 11g also provides a new Oracle Call Interface (OCI) result cache to enable client-side caching of SQL result sets. All OCI applications and drivers, such as JDBC-OCI, ODP.NET, OCCI, Pro*C/C++, Pro*COBOL, and ODBC, can take advantage of the client result cache.

The OCI result cache, which is transparent to OCI applications, keeps the result data set consistent with any changes in the session attributes or in the database itself. OCI client caching leads to a tremendous improvement in query performance for frequently repeated statements because the results are cached on the client itself, thus avoiding the expensive round trip to the server. Because you use fewer server resources as a result, this feature also enhances server scalability. In addition to a lower server CPU usage, client result caching also relieves the server of additional I/O burden to process frequently repeated queries.

The OCI result cache, which is on a per-process basis among multiple client sessions, can use the same cached result sets. The database automatically refreshes the result sets in the cache and manages memory allocation to the cache. If, during the round trips the OCI process makes to the server, any database changes affect the result set, the database automatically invalidates the cached result sets. That is, the database keeps the client result set transparently consistent with changes on the server. The big difference between the server-side result cache and the OCI client result cache, of course, is that the OCI result cache is located on the client and, therefore, doesn’t make any use of the server SGA.

While the server result cache is enabled by default, the client result cache is not. The server result cache and the client result cache work independent of each other. You can enable the client result cache even if you decide to disable the server result cache. Note that while the client result cache caches only the results of top-level SQL queries, the server result cache can also cache query fragments.
Client result caching is especially useful when applications produce repeatable or small result sets, which tend to be static over time. Frequently executed queries are also candidates for caching on the client. Lookup tables are particularly attractive candidates for client caching.
Enabling and Disabling the Client Result Cache As with the server-side result cache, you set the result_cache_mode initialization parameter to control whether the database caches the query results on the client.

Here’s how the result_cache_mode initialization parameter settings affect client-side result caching: If you set the result_cache_mode parameter to manual, you must annotate a query with the result_cache hint for the database to store it in the client cache, as shown here:
SQL> select /*+ result_cache */ deptno, avg(sal)
     from emp
     group by deptno;
If you set the result_cache_mode parameter to force, the database will store all SQL query results in the client cache whenever it’s possible to do so. If you don’t want the database to use the client cache for a query, you must specify the /*+ no_result_cache */ hint in the query, as shown here:
SQL> select /*+ no_result_cache */ deptno, avg(sal)
     from emp
     group by deptno;
You can set the result_cache_mode parameter with the alter system or alter session statement. The no_result_cache hint overrides the force setting of the result_cache_mode parameter, which would cause result caching behavior without the hint.

How Client Result Caching Works
When you specify either the result_cache or the no result_cache hint, you must add the hint to the SQL text you pass to the OCIStmtPrepare() and the OCIStmtPrepare2() calls. Managing the Client Result Cache You manage the client result cache by setting the following initialization parameters:

  • client_result_cache_size:This parameter determines the maximum size of the client per-process result set cache (in bytes). The setting of the parameter also determines if the cache is enabled. By setting this parameter to its default value of zero, you can disable the client result cache. By default, the database allocates every OCI client process the maximum size specified by this parameter. You can override this parameter with the server-side configuration parameter oci_result_ cache_max_size. If you disable client result caching on the server itself, the client result cache will remain disabled, even if you set the client_ result_cache_size parameter to a positive value on the client. The following query shows the current value of the client_result_cache_size parameter:
    SQL> show parameter client_result_cache_size
    NAME                          TYPE           VALUE
    ---------------------------   -----------    -----
    client_result_cache_size      big integer     0
    As this parameter is static, you must restart the database to affect a change in the maximum size of the client result cache.
  • client_result_cache_lag:This parameter determines the lag time for the client result cache. If you set a low value for this parameter, it results in more round trips to the database from the OCI client library to keep the client result cache in sync with the database. If your OCI application accesses the database only infrequently, you can set this parameter to a low value.

You can also use a client configuration file, which overrides the parameters you set in the server initialization parameter file. You can use the sqlnet.ora file to specify the parameter values on the client side.

When you use the client configuration file, you can specify the following three parameters:

  • oci_result_cache_max_size enables you to set the maximum size of the query cache for a process (in bytes). This parameter overrides the value you set for the client_result_cache_lag_size initialization parameter on the server.
  • oci_result_cache_max_rset_size enables you to set the maximum size (in bytes) of a single query result in the query cache for a process.
  • oci_result_cache_max_rset_rows enables you to set the maximum size of a query result set (in rows) for a process.

You can specify the result_cache and the no_result_cache hints in OCI applications, as with the SQL statements for the SQL query cache. However, the OCIStmtExecute() mode settings override the SQL hints.
Monitoring the Client Result Cache
The CLIENT_RESULT_CACHE_STATS$ view shows the client result cache settings and the cache usage statistics. The view includes information such as the number of results cached on the client, the number of cache hits, and the number of invalidated result sets.
Restrictions
You can’t cache queries that include the following objects on the OCI client, even though you may cache them in the server-side result cache:

  • Views
  • Remote objects
  • Complex types in the select list
  • Flashback queries
  • Queries that include PL/SQL functions
  • Queries that reference VPD polices on the tables

参 考至:《McGraw.Hill.OCP.Oracle.Database.11g.New.Features.for.Administrators.Exam.Guide.Apr.2008》    http://www.oracle-base.com/articles/11g/query-result-cache-11gr1.php
    http://www.oracle-base.com/articles/11g/cross-session-plsql-function-result-cache-11gr1.php
本文原创,转载请注明出处、作者

如有错误,欢迎指正

邮箱:czmcj@163.com

 

 

0
0
分享到:
评论

相关推荐

    oracle10g_pl/sql

    Oracle 10g PL/SQL 是Oracle数据库系统中用于创建和管理存储过程、函数、触发器等数据库对象的编程语言。本教程旨在为初学者提供一个全面的学习平台,同时也为经验丰富的开发者提供参考资料。PL/SQL是Oracle特有的...

    Oracle Database 12c PL/SQL开发指南 实例源代码

    PL/SQL是Oracle专为数据库管理设计的一种过程化编程语言,它结合了SQL查询语言的威力与高级编程语言的灵活性,使得数据库开发者能够创建复杂的应用程序逻辑。 在"Oracle Database 12c PL/SQL开发指南(第7版)"这...

    oracle 11g PL/SQL

    在IT领域,Oracle 11g的PL/SQL语言是数据库管理和编程的重要组成部分,它为开发者提供了强大且灵活的数据处理能力。以下是对Oracle 11g PL/SQL的关键知识点的详细解析: ### 1. PL/SQL概述 PL/SQL(Procedural ...

    Oracle 11g pl/sql编程

    根据提供的文件信息,以下是对Oracle 11g PL/SQL编程知识点的详细说明: 标题中的“Oracle 11g PL/SQL编程”指出了文档的主旨,即介绍Oracle数据库的第11g版本中PL/SQL(过程式SQL)编程的技术细节和实践应用。PL/...

    oracle 9i pl/sql程序设计笔记

    ### Oracle 9i PL/SQL程序设计笔记精要 #### PL/SQL基础知识概览 **标题与描述**:本文档围绕“Oracle 9i PL/SQL程序设计笔记”这一核心主题,深入探讨了PL/SQL语言的基础知识及其在Oracle 9i数据库环境中的应用。...

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二)

     第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sql和pl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 ...

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(一)

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(一) http://download.csdn.net/source/3268267 Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二) ...

    Oracle PL/SQL实战(待续)

    Oracle PL/SQL是一种强大的编程语言,它结合了SQL的数据处理能力与PL的程序设计特性,是Oracle数据库系统中用于创建存储过程、函数、触发器和包的主要工具。在这个"Oracle PL/SQL实战(待续)"的主题中,我们将深入...

    Oracle Database 11g PL/SQL编程实战part1

    《oracle database 11gpl/sql编程实战》通过动手练习、大量的示例以及实际的项目帮助读者掌握pl/sql。书中包含大量最佳实践,涵盖了pl/sql语言所有的最新功能和改进之处。每章末尾配有测验题,可以帮助读者进一步...

    oracle pl/sql fundamentals

    11. **实用程序和内置函数**:熟悉Oracle提供的各种内置函数,如数学函数、字符串函数、日期时间函数等,以及如何利用它们增强PL/SQL代码的功能。 12. **数据库对象的DDL操作**:学习如何在PL/SQL中创建、修改和...

    精通oracle10g PL/SQL编程

    第1章为PL/SQL综述,介绍了SQL和PLSQL的作用、PL/SQL的优点,以及Oracle10g和Oracle9i中PL/SQL的新特性。这一章是全书的起点,旨在让读者对PL/SQL有一个全面的认识。 第2章专注于PL/SQL开发工具,详细介绍了SQL*...

    精通Oracle 10g SQL和PL/SQL

    sql是关系数据库的基本操作语言 它是应用程序与数据库进行交互操作的接口 pl/sql是oracle特有的编程语言 它不仅具有过程编程语言的基本特征 循环 条件分支等 而且还具有对象编程语言的高级特征 重载 继承等 ...

    精通oracle 10g pl/sql编程

    《精通Oracle 10g PL/SQL编程》是一本专门针对Oracle 10g版本的PL/SQL编程语言的权威指南书籍。Oracle 10g是甲骨文公司(Oracle Corporation)推出的数据库管理系统(DBMS)的一个重要版本,而PL/SQL是Oracle的存储...

    Oracle PL/SQL程序设计(第5版)(套装上下册)

    《Oracle PL/SQL程序设计(第5版)(套装上下册)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何...

    Oracle PL/SQL 实例精解(第4版涵盖Oracle 11g)+源码脚本

    在"Oracle PL/SQL 实例精解(第4版涵盖Oracle 11g)+源码脚本"中,读者可以深入理解PL/SQL的各种概念和实践技巧。 本书详细讲解了Oracle 11g版本中的PL/SQL语言,这涵盖了从基础语法到高级特性的广泛范围。PL/SQL是...

    Oracle 11g PL/SQL 从入门到精通(视屏讲解)第一章

    在"Oracle 11g PL/SQL 从入门到精通(视频讲解)第一章"中,我们将会学习到PL/SQL的基础概念和核心元素。这一章通常会包括以下几个方面: 1. **PL/SQL简介**:首先会介绍PL/SQL的基本结构和特性,它是如何与SQL交互...

    精通Oracle10g PL/SQL

    Oracle 10g PL/SQL 是Oracle数据库管理系统中用于创建和管理数据库应用程序的重要编程语言。在《精通Oracle10g PL/SQL》这部电子资料中,读者将深入学习如何利用PL/SQL的强大功能来设计、开发和优化高效、可靠的...

    精通Oracle 10g PL/SQL编程

    《精通Oracle 10g PL/SQL编程》是中国水利水电出版社出版的一本专为数据库管理员、软件开发者和系统架构师量身打造的书籍,由王海亮、林立新等专家共同编著。这本书深入浅出地介绍了Oracle数据库中最重要的编程语言...

    Oracle资料学习PL/SQL必备

    "Oracle资料学习PL/SQL必备"这个主题涵盖了对Oracle数据库系统以及PL/SQL编程语言的学习资源,特别是针对那些希望深入理解并掌握PL/SQL的初学者或专业人士。PL/SQL的基础部分是了解和使用Oracle数据库的关键,它包括...

    ORACLE PL/SQL从入门到精通

    ORACLE PL/SQL是从入门到精通的专业知识,涵盖了数据库开发与管理的多个方面,包括触发器、过程、函数、软件包、异常处理、游标、循环、分支、变量使用、数据库安装等关键知识点。 触发器是数据库中用来保证数据...

Global site tag (gtag.js) - Google Analytics