pl/sql tips and techniques webinar presentation

24
EMBARCADERO TECHNOLOGIES EMBARCADERO TECHNOLOGIES PL/SQL Tips & Techniques Scott Walz | Embarcadero Director of Software Consultants Dan Hotka |Author & Instructor |Oracle Ace Director

Upload: embarcadero-technologies

Post on 02-Jul-2015

259 views

Category:

Software


2 download

DESCRIPTION

View the companion webinar at: http://embt.co/1xcLFjJ If you’ve ever wanted to code or understand more about PL/SQL code, this 2-part series is for you. Join Oracle ACE Director, Dan Hotka and Solutions Consultant Director, Scott Walz as they present and demo the fundamentals of PL/SQL and much more. Watch the webinar to learn about: + PL/SQL variable types and naming convention + How to code PL-SQL + When to use IF-THEN-ELSE or CASE + Profiling and debugging PL/SQL

TRANSCRIPT

Page 1: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIESEMBARCADERO TECHNOLOGIES

PL/SQL Tips & Techniques

Scott Walz | Embarcadero Director of Software Consultants

Dan Hotka |Author & Instructor |Oracle Ace Director

Page 2: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

PL/SQL Tips and Techniques

• Rapid SQL– PL/SQL Templates

• Read my blogs

– PL/SQL Debugging

• Prior Webinar

– PL/SQL Profiling

• Read my blogs

• PL/SQL Tips– Oracle Overview

– Compiler Options

• Includes some useful performance Tips for Functions

Page 3: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Oracle Architecture

DefaultResult

Cache

(11g)

Oracle SGA

Dedicated

User

Processes

Listener

TNSNAMES.ORA

LISTENER.ORADatabase Files

Tablespaces

Database FilesDatabase Files

Buffer Pool

nK(Optional)

Keep(optional)

Recycle(Optional)

Library

Cache

Shared Pool

Dictionary

Cache

Shared

SQL

SQL

Source

Exe Plan

Call

Stack

PGA

UGA

Sess

Data

Cursor

Area

Sort

Area

Client

Result

Cache

(11g)

PL/SQL

EngineSQL Engine

SQ

L

Statem

ent

External Files

UTL File

External Tables

OCI/JDBC

URL

Page 4: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Oracle Architecture

Page 5: PL/SQL Tips and Techniques Webinar Presentation

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!

Page 6: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

PL/SQL Overview

• Packages– Grouping related procedures and functions– Can share cursors/work areas– Code inheritance– When one module is accessed…entire package is loaded

into Oracle

• Database Triggers– Assigned to tables– Executes before or after a DML to the table

Page 7: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Rapid SQL Templates

• IF-THEN-ELSE– Can nest 250+ Deep

– ELSE is optional

– ELSIF is indeed misspelled!

Ctrl+ Space bar

Page 8: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Rapid SQL Templates

IF_ENDIF IF_ELSIF

Page 9: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Rapid SQL Templates

Page 10: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Rapid SQL Templates

• Case– Alternative to IF-THEN-ELSE

– Can nest 250+ Deep

– ELSE is optional

Case + Ctrl+ Space bar

Page 11: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Rapid SQL Templates

CASE

Page 12: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Rapid SQL Templates

Page 13: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

PL/SQL Profiling

• Profiler

– How long each routine took to execute

– How long each line of code took to execute

– RapidSQL has a nice interface• Easy to use

• Highlights performance issues

– Dan will demonstrate how it works…

Page 14: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Rapid SQL Profiler

Page 15: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

PL/SQL Debugging

• Debugger:

– Oracle symbolic debugger

– Changing variable content on the fly

– Setting code breakpoints

– Stepping through code dependencies

Demo

Page 16: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Stored Programs Syntax

• Procedures– Optional parameters

• NOCOPY makes reference to original parameter

• Useful when passing collections

– AUTHID is Definer unless specified!

– Autonomous Transaction• Allows for DML no matter

how current transaction ends

– Accessible by (list)• Oracle12 New Feature

• Only executable by this list of routines

Create [or replace] Procedure[ (parameter IN OUT IN OUT …) [NOCOPY] ][AUTHID {DEFINER | CURRENT_USER } ][ACCESSIBLE BY (<list>)]{IS | AS}[PRAGMA AUTONOMOUS_TRANSACTION;][ local variables]BEGIN<logic flow code>[EXCEPTION]<exception code processing.END [procedure name];

Page 17: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Stored Programs Syntax

• Functions– Optional parameters– Allows for Parallel DML– DETERMINISTIC

• Allows for the previous result to be used rather than rerunning the function when using the same parameters

– Accessible by (list)• Oracle12 New Feature• Only executable by this

list of routines

Create [or replace] Function[ (parameter …) ]RETURN <datatype>[AUTHID {DEFINER | CURRENT_USER } ][PARALLEL_ENABLE][ACCESSIBLE BY (<list>)][DETERMINISTIC][RESULT_CACHE] - 11g {IS | AS}[PRAGMA AUTONOMOUS_TRANSACTION;][ local variables]BEGIN<logic flow code>Must have a RETURN clause[EXCEPTION]<exception code processing.END [function name];

Page 18: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Stored Programs Syntax

Functions called from SQL CANNOT:• Commit or rollback

• Update the database

• Update same table as DML SQL

Page 19: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Oracle 11g Query Result Cache• IF same function called with same in_vars

• SQL with /*+ RESULT_CACHE */ hint

• Function with RESULT CACHE syntax

– RELIES_ON (table1, table2, …) – optional table DML check• Depreciated in Oracle11r2

• First result cache returned instead of rerunning the function/SQL!

• 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

Page 20: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

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

Page 21: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Oracle 11g Query Result CacheBe 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!

Page 22: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

New PL/SQL FeaturesOracle12 allows for Granting Roles to PL/SQL Programs

– A major problem in the past is a called program accesses a table…the connected user needed permissions to both the program and the underlying table

– Oracle12 allows for permissions to be granted to just the package/procedure/function so the users cannot directly access the underlying tables!

Oracle Database 12c New Features pg 246-248

Page 23: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Questions

23

Page 24: PL/SQL Tips and Techniques Webinar Presentation

EMBARCADERO TECHNOLOGIES

Thank you for attending!

Connect with us Online

24

Read Dan’s Blog

http://community.embarcadero.com/index.php/article/articles-database

Take our word for

it & Try it out!

http://www.embarcadero.com/products/rapid-sql

Take our survey

The survey is posted in the webinar chat window

Details can be found: sql-server-take-the-quiz-for-a-chance-to-win-a-quadcopter-drone