Watch the companion webinar at: http://embt.co/16cXD4h
Join Oracle ACE Director, Dan Hotka and Solutions Consultant Director, Scott Walz in part two of the series, where they will continue to build on that knowledge and share even more expertise on PL/SQL procedures, functions and packages.
Watch the webinar to learn about:
+ Procedures, functions and packages
+ Tips on PL/SQL compiling options
+ Performance tuning
Learn more about DB PowerStudio at: http://embt.co/DBPower
2. EMBARCADERO TECHNOLOGIES
PL/SQL Tips and Techniques
• Rapid SQL
– PL/SQL Templates
• Read my blogs
– Live demo of using the Result Cache!
• PL/SQL Tips
– Packages
– Compiler Options
• PL/SQL Limits
• Includes some useful performance Tips for Functions
3. EMBARCADERO TECHNOLOGIES
Recap: PL/SQL Tips and Techniques Part 1
• PL/SQL Architecture
• PL/SQL IF-THEN-ELSE and CASE
• Profiler
• Debugger
• Introduction to Result_Cache
Why do we
use PL/SQL?
4. EMBARCADERO TECHNOLOGIES
PL/SQL Overview
• Procedure
– Does processing
– Returns a code that the work was successful or not
– May or may not be passed values
– May or may not return values
• Functions
– Similar to Procedure EXCEPT it does return a value!
• Packages
– Grouping related procedures and functions
– Can share cursors/work areas
– Code inheritance
– When one module is accessed…entire package is loaded into Oracle
5. EMBARCADERO TECHNOLOGIES
Packages
• Packages
– Organizing related procedures and functions
– Allows for sharing of resources
– Allows for overloading of procedures/functions
• Can have a varchar2 or a date function with same name
– Can be used to hide code
– Read into the library cache as a unit
• Packages can be pinned in the library cache
6. EMBARCADERO TECHNOLOGIES
Packages
• Packages have 2 parts:
– Specification (or header)
• Defines the package name
• Declares the visible packages and procedures
• Defines the common variables, exceptions, types, and cursors to
be shared by objects in the package
– Body
• Contains PRAGMA declarations
• Contains all procedures and functions
– Procedures and functions NOT listed in the spec are ‘private’, used
only in this package, code is not easily visible
– Public procedures/functions are displayed via DESCRIBE
7. EMBARCADERO TECHNOLOGIES
Packages
• Syntax: CREATE [or replace] PACKAGE <pkg name>
[AUTHID {DEFINER | CURRENT_USER }]
IS <proc/functions by name IN OUT vars>
[PRAGMA Declarations]
END;
CREATE [or replace] PACKAGE BODY <same pkg name>
IS <proc/functions by name IN OUT vars with code>
<object, exception, variables, cursors>
[PRAGMA Declarations]
END <same pkg name>;
8. EMBARCADERO TECHNOLOGIES
Packages
• Pragma Declarations
– Compiler directives
– Autonomous Transactions
• Run in separate transaction
• Declared only in the package body
– Serially_Reusable
• Reset global/public variables back to initial state
• Declared in both the package spec and body
12. EMBARCADERO TECHNOLOGIES
Package Body (continued)
• Optional Block
– BEGIN appears after all other definitions (at the end of the
package body)
– Used to load arrays, perform package startup-tasks, etc
13. EMBARCADERO TECHNOLOGIES
Package Body (continued)
. . .
FUNCTION fahrenheit_to_celsius (IN_Temp NUMBER)
RETURN NUMBER IS
. . .
END fahrenheit_to_celsius;
BEGIN
var1 := 1;
var2 := 2;
END temperature;
/
16. EMBARCADERO TECHNOLOGIES
Oracle 11g Query Result Cache
• IF same function called with same in_vars
• Enhances Oracle8i thru Oracle10 DETERMINISTIC
• SQL with /*+ RESULT_CACHE */ hint
• Function with RESULT CACHE syntax
– RELIES_ON (table1, table2, …) – optional table DML check
• Deprecated in Oracle11r2
• First result cache returned instead of rerunning the function/SQL!
17. EMBARCADERO TECHNOLOGIES
Oracle 11g Query Result Cache
• Results stored in the SGA (Result Cache Memory)
• Any session running the function/SQL can benefit!
– * Enterprise Edition of Oracle
• Good for any table that is queried more than it is updated
– Result Cache is flushed with an update
Poll Question: Do you use functions? Would this be of use to you?
18. EMBARCADERO TECHNOLOGIES
Oracle 11g Query Result Cache
• Query Result Cache Sizing and
Monitoring
– Sizing via Init.Ora settings:
• RESULT_CACHE_MAX_SIZE
– 0 disables it
– Size in bytes in increments of 32K [num
G | M | K ]
– Initially defaults to 10% Shared Pool Size
• RESULT_CACHE_MAX_RESULT
– 0 – 100
– Percentage of
RESULT_CACHE_MAX_SIZE single result
can take
– Dynamic performance views:
• V$RESULT_CACHE_STATISTICS
• V$RESULT_CACHE_MEMORY
• V$RESULT_CACHE_OBJECTS
• V$RESULT_CACHE_DEPENDENCY
19. EMBARCADERO TECHNOLOGIES
Oracle 11g Query Result Cache
• RESULT_CACHE from function calls
– IF same function called with same in_vars
– First result cache returned instead of rerunning the function!
– Results stored in the SGA…any session running the function can
benefit!
SQL> create or replace function get_dname
(in_deptno number) return in_dname varchar2
RESULT_CACHE
As
…
SQL> select get_dname(10) DNAME from dual;
SQL>
DNAME
-----------
CHICAGO
SQL> Time: 00.00.01
SQL> select get_dname(10) DNAME from dual;
SQL>
DNAME
-----------
CHICAGO
SQL> Time: 00.00.00
26. EMBARCADERO TECHNOLOGIES
Oracle 11g Query Result Cache
• RESULT_CACHE from SQL
– IF same SQL ran from any session:
• First result cache returned instead of rerunning the SQL!
• Appears to work with bind variables
– Init.Ora settings:
• RESULT_CACHE_MODE [MANUAL | FORCE]
– Manual…must use RESULT_CACHE hint
– FORCE…always uses result cache unless NO_RESULT_CACHE used
USER1
SQL> select /*+RESULT_CACHE */ ename from emp;
SQL>
ENAME
-----------
SMITH
SQL> Time: 00.00.10
SQL>
USER2
SQL> select /*+RESULT_CACHE */ ename from emp;
SQL>
ENAME
-----------
SMITH
SQL> Time: 00.00.00
SQL>
27. EMBARCADERO TECHNOLOGIES
Oracle 11g Query Result Cache
Be careful out there!
– Make sure the SQL inside the function does not have other
dependencies
• Such as a SYSDATE or some other variable NOT passed in as an in
variable
• Can probably fix about ANY problem by putting the dependencies
as in variables!
28. EMBARCADERO TECHNOLOGIES
PL/SQL Limits
• PL/SQL Limits (Oracle11 and before)
– Program/Variable Name – 30 characters
– PL/SQL Block Nesting – 255
– Bind Variables passed – 32K
– Record Nesting – 32
– Record Limit – 64K
– Sub Query Nesting – 254
– Trigger size 32K
Memory Footprint:
Select * from USER_OBJECT_SIZE
Where NAME = ‘<procedure/function/package name>’;
29. EMBARCADERO TECHNOLOGIES
PL/SQL Limits
• PL/SQL
– Varchar2 – 32K
– Char – 32K
• IF selecting/saving to database fields
– Always specify a SIZE…
– Manage sizes with SUBSTR
– Change database data type to CLOB
– Unmanaged: Ora-12899: value too large for column
• SQL
– Varchar2 – 4000 bytes
– Char – 2000 bytes
30. EMBARCADERO TECHNOLOGIES
New Limits
• Increased column size limits in Oracle12
– IF MAX_STRING_SIZE = EXTENDED
• Varchar2 – new limit 32K
• NVarchar2 – new limit 32K
• RAW – new limit 32K
– MAX_STRING_SIZE = STANDARD (default behavior)
• Varchar2 –limit 4K
• NVarchar2 –limit 4K
• RAW – limit 2K
Oracle Database 12c New Features pg 228 - 229
32. EMBARCADERO TECHNOLOGIES
New Limits
• New Oracle12 init.ora setting: Max_String_Size
– Set to EXTENDED
• Allows new limits
– Set to STANDARD
• Old limits used
– Indexes only support keys up to 6,400 bytes long
– THERE IS NO GOING BACK!!!
• Backup well/test then implement.
35. EMBARCADERO TECHNOLOGIES
Thank you for attending!
Connect with us Online
35
Read Dan’s Blog
http://community.embarcadero.co
m/index.php/article/articles-
database
Take our word for
it & Try it out!
http://www.embarcadero.co
m/products/rapid-sql
Take our survey
The survey is posted in the
webinar chat window