Oracle PL/SQL Programming Steven Feuerstein All About the (Amazing) Function Result Cache of Oracle Database 11g.

Slides:



Advertisements
Similar presentations
Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
Advertisements

Welcome to My Obsession
Oracle PL/SQL IV Exceptions Packages.
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Acknowledgments Byron Bush, Scott S. Hilpert and Lee, JeongKyu
15 Copyright © 2004, Oracle. All rights reserved. Monitoring and Managing Memory.
1 - Oracle Server Architecture Overview
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
IT The Relational DBMS Section 06. Relational Database Theory Physical Database Design.
SAGE Computing Services Customised Oracle Training Workshops and Consulting Are you making the most of PL/SQL? Hints and tricks and things you may have.
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
Basic Oracle Architecture
1 Robert Wijnbelt Health Check your Database A Performance Tuning Methodology.
© Dennis Shasha, Philippe Bonnet – 2013 Communicating with the Outside.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Database Systems Slide 1 Database Systems Lecture 5 Overview of Oracle Database Architecture - Concept Manual : Chapters 1,8 Lecturer : Dr Bela Stantic.
From Zero to Hero : Using an assortment of caching techniques to improve performance of SQL containing PL/SQL calls. Tim Hall.
Oracle9i Performance Tuning Chapter 2 Tuning the Buffer Cache.
1 Oracle Architectural Components. 1-2 Objectives Listing the structures involved in connecting a user to an Oracle server Listing the stages in processing.
Copyright  Oracle Corporation, All rights reserved. 1 Oracle Architectural Components.
Oracle Tuning Ashok Kapur Hawkeye Technology, Inc.
Oracle PL/SQL Practices. Critical elements of PL/SQL Best Practices Build your development toolbox Unit test PL/SQL programs Optimize SQL in PL/SQL programs.
Copyright © Oracle Corporation, All rights reserved. 1 Oracle Architectural Components.
An Oracle server:  Is a database management system that provides an open, comprehensive, integrated approach to information management.  Consists.
1 Chapter 14 DML Tuning. 2 DML Performance Fundamentals DML Performance is affected by: – Efficiency of WHERE clause – Amount of index maintenance – Referential.
Oracle9i Performance Tuning Chapter 12 Tuning Tools.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Chapter 18 Object Database Management Systems. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Motivation for object.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Maintaining Session State in the Data.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Outline Introduction to Oracle Memory Structures SGA, PGA, SCA The Specifics of the System Global Area (SGA) Structures Overview of Program Global Areas.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Maintaining Session State in the Data.
Process Architecture Process Architecture - A portion of a program that can run independently of and concurrently with other portions of the program. Some.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Giovanni Chierico | May 2012 | Дубна Data Concurrency, Consistency and Integrity.
Commercial RDBMSs: Office Access and Oracle Pertemuan 13 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
20 Copyright © 2008, Oracle. All rights reserved. Cache Management.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
1 Chapter 8 Execution Plan Management. 2 Overview of Execution Plan Management Review techniques to – override optimizer – Improve optimizer’s decisions.
Oracle9i Performance Tuning Chapter 4 Tuning the Shared Pool Memory.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
SQL Triggers, Functions & Stored Procedures Programming Operations.
8 Copyright © 2006, Oracle. All rights reserved. Tuning the Shared Pool.
3 Copyright © 2004, Oracle. All rights reserved. Database Architecture Comparison.
Caching for PL/SQL Performance Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g) OCA.
3 Copyright © 2006, Oracle. All rights reserved. Designing and Developing for Performance.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Oracle Database Architectural Components
4 Copyright © 2009, Oracle. All rights reserved. Working with Packages.
Chapter 21 SGA Architecture and Wait Event Summarized & Presented by Yeon JongHeum IDS Lab., Seoul National University.
UNIT - V STORED PROCEDURE.
Database Performance Tuning and Query Optimization
PRACTICE OVERVIEW PL/SQL Part - 2.
Oracle Memory Internals
Oracle Architectural Components
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

Oracle PL/SQL Programming Steven Feuerstein All About the (Amazing) Function Result Cache of Oracle Database 11g

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 2 Why are my queries so slow? Really? Are they? :-) Mostly, no. – Mostly, we write good-enough SQL and Oracle optimizes the heck out of the DB. But there can be limits to that optimization, especially for "special cases". For example: what if we know that the data we are querying doesn't change or at least doesn't change very often? – Materialized views! Then we might pay a price and not even realize that things could be much better.

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 3 System Global Area (SGA) of RDBMS Instance PL/SQL Runtime Memory Architecture Shared Pool Large Pool Reserved Pool show_emps calc_totalsupd_salaries Select * from emp Shared SQL Pre-parsed Update emp Set sal=... Library cache Session 1 memory UGA – User Global Area PGA – Process Global Area emp_rec emp%rowtype; tot_tab pkg.tottabtype; Session 2 memory UGA – User Global Area PGA – Process Global Area emp_rec emp%rowtype; tot_tab pkg.tottabtype; Session 1 Session 2

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 4 PGA-Based Caching When you declare variables at the package level, their state persists in your session. – A PGA-based cache, specific to each session. And if you declare a collection at the package level, you can cache multiple rows of data. – Very useful for static datasets like materialized views. Note: not a reliable technique for Web-based (usually stateless) applications

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 5 Avoiding Unnecessary SGA Lookups First access PGA Function Database / SGA Not in cache; Request data from database Pass Data to Cache Application Requests Data Data retrieved from cache Data returned to application Function PGA Subsequent accesses Application Requests Data Data returned to application Data retrieved from cache Database / SGA Data found in cache. Database is not needed. emplu.pkg / emplu.tst

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 6 PGA Caching: Things to keep in mind Must use package-level data so that it persists. – Memory is consumed by the PGA and so is multiplied for all users of the application. – Not a reliable technique for stateless application (Internet) Very difficult to share cache across sessions in the same instance. – One possibility involves DBMS_PIPE. Very difficult to update the cache once the data source is changed. – Especially by/from, other sessions. Possible to use DBMS_ALERT. Useful under specific scenarios.... – Small, static dataset – Single or small number of batch processes syscache.pkg

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 7 The Oracle 11g Function Result Cache 11g offers a far superior caching solution than PGA caching in 11g: the Function Result Cache. This cache is... – stored in the SGA – shared across sessions – purged of dirty data automatically You can use and should use it to retrieve data from any table that is queried more frequently than updated. 11g

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 8 How the Function Result Cache Works Add the RESULT_CACHE clause to your function's header. When a call is made to function, Oracle compares IN argument values to the cache. If no match, the function is executed and the inputs and return data are cached. If a match is found, the function is not executed; cached data is returned. If changes to a "relies on" table are committed, the cache is marked invalid and will be re-built. 11g 11g_frc_demo.sql

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 9 Minimal Impact on Code with Result Cache Add RESULT_CACHE keyword to header of function in both specification and body. RELIES_ON clause is deprecated in Oracle will automatically determine all tables on which the function relies. RELIES_ON is then ignored. CREATE OR REPLACE PACKAGE emplu11g IS FUNCTION onerow (employee_id_in IN employees.employee_id%TYPE) RETURN employees%ROWTYPE RESULT_CACHE; END emplu11g; CREATE OR REPLACE PACKAGE BODY emplu11g IS FUNCTION onerow (employee_id_in IN employees.employee_id%TYPE) RETURN employees%ROWTYPE RESULT_CACHE RELIES_ON (employees) IS.... END onerow; END emplu11g; 11g

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 10 Performance Impact of Result Cache The result cache is stored in the SGA. So we should expect it be slower than a PGA- based cache. But accessing result cache data does not require going through the SQL engine. So it should be much faster than executing a query. – Even if the statement is parsed and the data blocks are already in the SGA. Let's find out! 11g_emplu*.* 11g

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 11 Result Cache – Things to Keep in Mind - 1 If you have uncommitted changes in your session, dependent caches are ignored. – The cache will not override your own changed data. Caching is not performed for complex types: records with CLOBs, collections, etc. – But Oracle is optimistic! The cache is not related to SQL statements in your function. – It only keeps track of the input values and the RETURN clause data. 11g_frc_demo.sql 11g

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 12 Result Cache – Things to Keep in Mind - 2 You cannot use the result cache with invoker rights program units. – Bypass execution of function body, Oracle cannot resolve references to objects - the whole point of IR. Functions with session-specific dependencies must be "result-cached" with great care. – Virtual private database configurations – References to SYSDATE, reliance on NLS_DATE_FORMAT, time zone changes – Application contexts (calls to SYS_CONTEXT) Solution: move all dependencies into parameter list. 11g_frc_vpd.sql 11g_frc_vpd2.sql 11g

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 13 Managing the Result Cache Oracle offers a number of ways to manage the result cache and tune it to your specific application needs: RESULT_CACHE_MAX_SIZE initialization parameter – If the cache is too small, then the LRU algorithm negates the point of the cache. DBMS_RESULT_CACHE management package v$RESULT_CACHE_* performance views 11g show_frc_dependencies.sp

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 14 Fine Grained Dependencies in 11.2 Oracle keeps track of table dependencies on a per-result level. – Each result cached could have a different set of dependencies. A change to a table could invalidate just a subset of the results in the cache. – It's not all or nothing - when your function's different logic paths could "hit" different tables. 11g 11g_frc_dependencies.sql 11g_frc_dependencies2.sql

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 15 Make It Easy on Yourself I hope you will agree that the result cache is a great feature. But how easy will it be for you to apply it? If you write/duplicate queries throughout your code, upgrading will be expensive and slow. If you hide your queries behind functions, you have a single point of definition, and you can upgrade "instantly." 11g_frc_encapsulation.sql

Oracle PL/SQL Programming Copyright 2013 Feuerstein and AssociatesPage 16 Conclusions - Don't Assume ANYTHING! There's almost always a way to improve your application. – Especially when you can tune for specialized circumstances. PGA caching – Very fast, but of limited use Function result cache – Simplest and most widely applicable technique – Get ready for it now by hiding queries inside functions.