+ All Categories
Home > Documents > Plsql 9i New Features Ppt 129888

Plsql 9i New Features Ppt 129888

Date post: 03-Apr-2018
Category:
Upload: venkatesh-gunti
View: 220 times
Download: 0 times
Share this document with a friend
95
Transcript

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 1/95

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 2/95

Bryn Llewellyn

Product Manager 

for PL/SQL

Oracle Corporation

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 3/95

PL/SQLenhancements

in Oracle9i 

 paper #129, Oracle OpenWorld, San Francisco, Tue 4-Dec-2001

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 4/95

But before I start…But before I start…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 5/95

OTN homepageOTN homepage

TechnologiesTechnologies

PL/SQLPL/SQL

otn.oracle.com/tech/pl_sqlotn.oracle.com/tech/pl_sql

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 6/95

What are the Benefits?

y Speed and scalability

y Functionality

y Usability for the developer 

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 7/95

What’s been enhanced?

y Implementation

y Language features

y Supplied packages

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 8/95

What kind of enhancements?

y Transparent

y Semi-transparent

y  New features

 –   New constructs in the language –  Richer APIs in the supplied packages

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 9/95

Here comes the list…Here comes the list…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 10/95

Implementation

Feature Perf Funct UseFeatureFeature Perf Perf  FunctFunct UseUse

Native compilation of PL/SQL3

Native compilation of PL/SQLNative compilation of PL/SQL3

Manipulating records faster by up to 5x 3Manipulating records faster by up to 5xManipulating records faster by up to 5x 3Inter-package calls faster by up to 1.5x 3InterInter--package calls faster by up to 1.5xpackage calls faster by up to 1.5x 3Utl_Tcp native implementation 3Utl_TcpUtl_Tcp native implementationnative implementation 3

Common SQL parser 3 3Common SQL parserCommon SQL parser 3 3

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 11/95

Language features

Feature Perf Funct UseFeatureFeature Perf Perf  FunctFunct UseUse

Table functions 3 3 3Table functionsTable functions 3 3 3Cursor expressions 3 3 3Cursor expressionsCursor expressions 3 3 3Multilevel collections 3 3 3Multilevel collectionsMultilevel collections 3 3 3

Bulk binding in native dynamic SQL 3 3Bulk binding in native dynamic SQLBulk binding in native dynamic SQL 3 3Exception handling in bulk binding DML operations 3 3Exception handling in bulk Exception handling in bulk bindingbinding DML operationsDML operations 3 3

CASE statements and CASE expressions 3CASE statements and CASE expressionsCASE statements and CASE expressions 3

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 12/95

Exception handling in bulk DML

y Consider giving employees a 10% raise whereemployee_id among the values in a table of 

numbers…

y forall j in id.first..id.last

y

There might be a per department salary cap – so some intended updates will fail

y Pre-Oracle9i the whole bulk statement failed

y Now the OK updates will fail and the exceptionscan be reported

y save exceptions

sql%bulk_exceptions collection

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 13/95

Multilevel collection syntax

for j in my_multi.first..my_multi.last

loopfor k in

 my_multi(j).first..my_multi(j).last

loopShow ( my_multi(j)(k) );

end loop;

end loop;

y Collections can be nested to arbitrary depth

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 14/95

CASE statement syntax

case n

 when 1 then Action1; when 2 then Action2;

 when 3 then Action3;

else ActionOther;end case;

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 15/95

CASE statement syntax

case

 when p = 1 then Action1; when q = 1 then Action2;

 when r > 1 then Action3;

else ActionOther;end case;

y “searched” variety

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 16/95

CASE expression syntax

text := case n

 when 1 then 'one' when 2 then 'two'

 when 3 then 'three'

else 'other'end;

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 17/95

CASE expression syntax

text := case

 when p = 1 then 'one' when r = 2 then 'two'

 when q > 1 then 'three'

else 'other'end;

y “searched” variety

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 18/95

CASE_NOT_FOUND exception

 begin

 p:=0; q:=0; r:=0;

case when p = 1 then Action1;

 when r = 2 then Action2;

 when q > 1 then Action3;end case;

exception

 when case_not_found then

Show ('Exception: case_not_found' );

end;

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 19/95

Language features – cont.

Feature Perf Funct UseFeatureFeature Perf Perf  FunctFunct UseUse

VARCHAR2 <-> NVARCHAR2 (etc) assignment3

VARCHAR2 <VARCHAR2 <--> NVARCHAR2 (etc) assignment> NVARCHAR2 (etc) assignment3

VARCHAR2 <-> CLOB assignment 3VARCHAR2 <VARCHAR2 <--> CLOB assignment> CLOB assignment 3SUBSTR and INSTR w/ CLOB 3SUBSTR and INSTR w/ CLOBSUBSTR and INSTR w/ CLOB 3

Seamless access to new SQL features 3e.g. MERGE, multitable insert, new time datatypes…

Seamless access to new SQL featuresSeamless access to new SQL features 3e.g. MERGE, multitable insert, new time datatypese.g. MERGE, multitable insert, new time datatypes……

OO: Schema evolution 3 3inheritance support

a.k.a. subtyping and polymorphism

OOOO:: Schema evolutionSchema evolution 3 3inheritance supportinheritance support

a.k.a. subtyping and polymorphisma.k.a. subtyping and polymorphism

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 20/95

Supplied packages

Feature Perf Funct UseFeatureFeature Perf Perf  FunctFunct UseUse

Utl_Http enhanced3 3

Utl_HttpUtl_Http enhancedenhanced3 3

Utl_Raw enhanced 3Utl_ Utl_ RawRaw enhancedenhanced 3Utl_File enhanced 3Utl_FileUtl_File enhancedenhanced 3

 Nineteen new packages 3 Nineteen Nineteen new packagesnew packages 3

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 21/95

Utl_Http enhancements

y Used to send HTTP request and handle the replymechanically – especially in B2B applications

y Oracle9i adds…

 –  “POST” method – arbitarily long request

 –  Authentication

 –  Access to return status code

 –  RAW reply

 –  cookie support

y

 I.e. provides full support for the semantics that can be expressed via HTTP

y Full functionality for character set conversion for 

request and reply

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 22/95

I can’t possibly

talk abouteverything

on that listin detail!

I can’t possibly

talk abouteverything

on that listin detail!

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 23/95

So instead

I’m going topick just a couple

to look atin depth…

So instead

I’m going topick just a couple

to look atin depth…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 24/95

• Native compilation• Native compilation

• Table functionsand cursor expressions• Table functionsand cursor expressions

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 25/95

Native compilation of PL/SQL

y When PL/SQL is used as a thin wrapper for SQL

its execution speed is rarely an issuey But we see an increasing trend to use PL/SQL for 

computationally intensive database independent

tasks

y Here it is the execution speed of the PL/SQL code

that determines the performance

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 26/95

Background

y Pre-Oracle9i, compilation of PL/SQL source code

always results in bytecode which is stored in thedatabase and interpreted at run-time by a virtual

machine implemented within ORACLE

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 27/95

What’s new?

y In Oracle9i PL/SQL source code may optionally be

compiled into native object code which is linkedinto ORACLE

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 28/95

What’s the benefit?

y Speed increased by up to 40%

y “Thin wrapper” programs won’t speed up so much

…but they won’t slow down!

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 29/95

How does it work?

y The code generator translates the PL/SQL source

code into C source codey This is compiled on the given platform and stored

as object files on the filesystem…

y …and then linked into ORACLE

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 30/95

How do you do it?

y One-time DBA setup

y The developer chooses at compile time via thesession parameter…

 plsql_compiler_flags

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 31/95

One-time DBA setup

y Specify 3rd party utilities for compiling and linking

 –  Should be owned by the ORACLE owner 

 –  Only the ORACLE owner should have

write access

y Specify directories for the compiled object libraries

y Done via system parameters

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 32/95

Developer choice

alter session

set plsql_compiler_flags ='NATIVE'

/* or 'INTERPRETED' */;

ysets the compilation mode for subsequentlycompiled PL/SQL library units

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 33/95

Oracle recommends…

y All PL/SQL library units that are called from agiven natively compiled top-level unit should also

 be compiled natively

there is a cost for the context switch when a library unit 

compiled in native mode invokes one compiled in interpreted mode 

y Recommendation includes the Oracle-suppliedlibrary units (by default these are compiled ininterpreted mode)

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 34/95

Oracle9i in Action

y 170 Systems, Inc have been an Oracle Partner for 

eleven years and participated in the Beta Programfor the Oracle9i Database with particular interest inPL/SQL Native Compilation

y

They have now certified their 170 MarkViewDocument Management and Imaging System™against Oracle9i

yThey have updated the install scripts to optionallyturn on Native Compilation

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 35/95

170 MarkView

Document Managementand Imaging System

y Provides Content Management, DocumentManagement, Imaging and Workflow solutions

y Tightly integrated with the Oracle9i Database,Oracle9i Application Server andthe Oracle E-Business Suite

y Enables businesses to capture and manage all of their information online in a single, unified system

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 36/95

170 MarkView™ 

y

Customers include… –  British Telecommunications

 –  E*TRADE Group

 –  the Apollo Group

 –  the University of Pennsylvania

y Very large numbers of documents, images,

concurrent users, and high transaction rates

y Performance and scalability especially important

y Large-scale multi-user, multi-access system

170 M kVi ™

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 37/95

170 MarkView™ 

y Business logic, including preparation of data for 

 presentation, is implemented in the database inPL/SQL

y Involves complex logic and intensive string

 processing supported by stacks and lists of values

modeled as PL/SQL collections.

y Visit 170 Systems at Booth # 1914

170 S t

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 38/95

170 Systems

y Have observed a performance increase of up to

40% for computationally intensive routines

…and no performance degradation

N ti C il ti

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 39/95

Native Compilation - summary

y It’s a semi-transparent enhancement

y It gives you improved performance

y Try it !

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 40/95

• Table functions

and cursor expressions

• Table functionsand cursor expressions

T bl f ti

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 41/95

Table functions

and cursor expressions

y Cursor variables – recap

y Manipulating cursor expressions in PL/SQL

y Using a cursor expression

as an actual parameter to a PL/SQL function

y Table functions pre-Oracle9i

y

Oracle9i pipelined table functionsy Daisy-chaining table functions

y Parallelization

C i bl

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 42/95

Cursor variables - recap

y Supported pre-Oracle9i

y Allow encapsulation of logicspecific to tuples of a particular format

to be independent of 

the concrete SELECT statementwhich retrieves them

Cursor variables recap

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 43/95

Cursor variables - recap

 procedure Bulk_Fetch_From_Cursor

( p_cursor in sys_refcursor )is...

y SYS_REFCURSOR is a new Oracle9i

usability feature

y Defines a generic weak cursor once and for all

Set up the target

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 44/95

Set up the target

y Declare an index-by PL/SQL table for the

fetched rows

...is

type names_t istable of varchar2(4000)

index by binary_integer;

the_names names_t; begin...

Do the bulk fetch

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 45/95

Do the bulk fetch

fetch p_cursor

 bulk collectinto the_names;

y Works pre-Oracle9i

y Performance boost because reduces context

switching between PL/SQL and SQL engines

Process the results

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 46/95

Process the results

for j in the_names.first..the_names.last

loopShow ( the_names(j) );

end loop;

y Works pre-Oracle9i

Invoke the procedure

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 47/95

Invoke the procedure

declare

the_cursor sys_refcursor;

 beginopen the_cursor for

'select last_name from employees

order by last_name';Bulk_Fetch_From_Cursor ( the_cursor );

close the_cursor;

y  Note we’re using Native Dynamic SQL

New in Oracle9i

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 48/95

New in Oracle9i 

y This is just one example of bulk binding working

with native dynamic SQLy The combination now works in all situations

 –   Defining 

SELECT –   In-binding 

FORALL … USING

 –  Out-binding FORALL … USING … RETURNING

Table functions

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 49/95

Table functions

and cursor expressions

y Cursor variables – recap

y Manipulating cursor expressions in PL/SQL

yUsing a cursor expressionas an actual parameter to a PL/SQL function

y Table functions pre-Oracle9i

y Oracle9i pipelined table functionsy Daisy-chaining table functions

y Parallelization

Cursor expressions and PL/SQL

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 50/95

Cursor expressions and PL/SQL

y Suppose we want to make a pretty print

…listing departments

…and under each department list its employees

Explicit 3GL approach

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 51/95

Explicit 3GL approach

for department in ( select ... )

loopShow ( department.department_name );

for employee in ( select ...

 where department_id =department.department_id )

loop

Show ( employee.last_name );

This SQL does the job in one go

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 52/95

This SQL does the job in one go

select

department_name,cursor (

select last_name

from employees e where e.department_id =

d.department_id 

)

from departments d;

Use in it PL/SQL – new in Oracle9i

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 53/95

Use in it PL/SQL new in Oracle9i 

declare

cursor depts isselect

department_name,

cursor ( select ... )from departments d;

y Since there’s only one SQL statement(we had two in the explicit approach)the CBO has a better chance

Set up the fetch targets

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 54/95

Set up the fetch targets

y Just for fun we’ll do some bulk fetching

v_dname dep...%type;

emps sys_refcursor;

type enames_t is table of emp...%type

index by...;v_enames enames_t;

Outer loop

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 55/95

Outer loop

y Fetch the cursor into the ref cursor target

and then fetch it (bulk) into its targetopen depts;

loop

fetch depts into v_dname, emps;

exit when depts%notfound;

Show ( v_dname );fetch emps

 bulk collect into v_ename;

Inner loop

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 56/95

Inner loop

y Process the bulk fetched results

for j in v_ename.first..v_ename.last

loopShow ( v_ename(j) );

end loop;

Table functions

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 57/95

and cursor expressions

y

Cursor variables – recapy Manipulating cursor expressions in PL/SQL

y Using a cursor expression

as an actual parameter to a PL/SQL functiony Table functions pre-Oracle9i

y Oracle9i pipelined table functionsy Daisy-chaining table functions

y Parallelization

Cursor terminology

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 58/95

gy

y A cursor variable (i.e. a variable of type

REF CURSOR ) points to an actual cursor y It may be used as a formal parameter to a PL/SQL

 procedure or function

y A cursor expression defines an actual cursor …and is legal in a SQL statement

Func ( cursor ( select c from t ) )

y So you’d expect to write…

Cursor expression as an actual

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 59/95

p

parameter to a PL/SQL function

y

Pre-Oracle9i this was never allowedy At Oracle9i it is allowed…

…when a function is invoked in

a top level SQL statement

Cursor expression in

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 60/95

p

 WHERE clause functionselect ...

from employees managers where Func(

cursor ( < select stuff for 

this manager's reports > ), managers.hire_date

) = 1;

Cursor expression in

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 61/95

p

 WHERE clause function

y

We’re deciding on the basis of elaborate procedural logic

y Writing the whole thing as a procedure you need to

output to a tabley With the logic in a WHERE clause function you can

define a VIEW for dynamic reuse

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 62/95

A cursor expression

can be an actual parameterto a PL/SQL function

in a top levelSQL statement

A cursor expression

can be an actual parameterto a PL/SQL function

in a top levelSQL statement

Hold that thought…

Table functions

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 63/95

and cursor expressions

y

Cursor variables – recapy Manipulating cursor expressions in PL/SQL

y Using a cursor expression

as an actual parameter to a PL/SQL function

y Table functions pre-Oracle9i

y

Oracle9i pipelined table functionsy Daisy-chaining table functions

y Parallelization

Pre-Oracle9i table function

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 64/95

y We define our row, and a table of our rows…

create type my_row

as object ( idx number,

text varchar2(20) );

create type my_tab as table of my_row;

Pre-Oracle9i table function

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 65/95

function Func return my_tab is

v_tab my_tab;

 begin

v_tab := my_tab ( my_row ( ... ) );

< extend the table and compute the rows >return v_tab;

end Func;

y All the rows are created before the function returns

Pre-Oracle9i table function

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 66/95

select *

from table( cast

( Func() as my_tab )

);

y Nice, but the response characterstics are not whatwe’re used to with a rowsource

Table functions

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 67/95

and cursor expressions

y Cursor variables – recap

y Manipulating cursor expressions in PL/SQL

y Using a cursor expression

as an actual parameter to a PL/SQL function

y Table functions pre-Oracle9i

yOracle9i pipelined table functions

y Daisy-chaining table functions

y Parallelization

Oracle9i pipelined table function

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 68/95

function Func return my_tab pipelined is

v_row my_row;

 begin

for ...

loop

v_row := my_row ( ... );

 pipe row ( v_row );

end loop;return;

end Func;

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 69/95

As soon as

a row is computed,it’s delivered

rowsource-style

As soon as

a row is computed,it’s delivered

rowsource-style

Hold that thought too…

 Note:

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 70/95

y The pipelined and pipe row keywords

can only be used together as shown

y A pipelined table function can be invoked

only in the FROM list of a SELECT clause

select * from table ( Func() );

y We can now use a simpler syntax…

y We can now use package-level types for the row

and for the table

Table functions

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 71/95

and cursor expressions

y Cursor variables – recap

y Manipulating cursor expressions in PL/SQL

y Using a cursor expression

as an actual parameter to a PL/SQL functiony Table functions pre-Oracle9i

y Oracle9i pipelined table functions

y Daisy-chaining table functionsy Parallelization

Remember…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 72/95

y A cursor expression can be an actual parameter to

a PL/SQL function in a top level SQL statementy So a table function may now be defined with an

input parameter of type REF CURSOR and invoked

with a cursor expression as the actual parameter 

function Func_2 ( p_cur in sys_refcursor )

return Pkg.my tab pipelined is

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 73/95

g y_ p p

v_in_row Pkg.my_in_row;

v_out_row Pkg.my_row;

 begin

loop

fetch p_cur into v_in_row;

exit when p_cur%notfound;< compute out-row(s) from in-row(s) >

 pipe row ( v_out_row );

end loop;close p_cur;

return;

end Func_2;

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 74/95

We’re consuming in-rows,Transforming them

procedurally to out-rowswith an arbitrarily complex

M:N algorithm,and piping out-rowsas soon as they’re ready

We’re consuming in-rows,Transforming them

procedurally to out-rowswith an arbitrarily complex

M:N algorithm,and piping out-rowsas soon as they’re ready

How do we use it?

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 75/95

y Suppose t is a table which supports a select list

compatible withPkg.my_row

y We can now invoke the table function thus…

select * from table(

Func_2

( cursor ( select * from t ) ));

Think about it…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 76/95

y We now have a very powerful generic

transformation techniquey It starts with the results from a SELECT and

transforms them into something you can SELECT

fromy Sounds familiar?

y

 Note: don’t forget, the table function can haveordinary input parameters

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 77/95

Think of a table functionas a deluxe,

parameterizable view

Think of a table functionas a deluxe,

parameterizable view

t might have been a view…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 78/95

create view t asselect * from table ( Func() );

ySo we’d actually be doing…select * from table

(

Func_2 ( cursor

(

select * from table ( Func() ) ))

);

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 79/95

Now we’re daisy-chainingNow we’re daisy-chaining

Daisy-chaining

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 80/95

y We can plug any number of transformations back-to-back 

y Each can perform an arbitrarily complextransformation

y

The starting point could be an Oracle tabley Or it could be a table function

that accesses external data

via Utl_Fileor via the call-out framework 

Table functions

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 81/95

and cursor expressions

y Cursor variables – recap

y Manipulating cursor expressions in PL/SQL

y Using a cursor expression

as an actual parameter to a PL/SQL functiony Table functions pre-Oracle9i

y Oracle9i pipelined table functions

y Daisy-chaining table functions

y Parallelization

Who’s going to use them?

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 82/95

y The driver for developing table functions was

datawarehousing, especially the ETL phase

y So of course the story wouldn’t be complete

without a parallel capability

y Let’s look at the syntax…

PARALLEL_ENABLE syntax

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 83/95

function Func_2 ( p_cur in sys_refcursor )

return Pkg.my_tab pipelined 

 parallel_enable ( partition p_cur by any )

is...

y Must have an input REF CURSOR  parameter to drive

the partitioningy ANY is the simple, special case – results don’t

depend on the order of processing

Caution…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 84/95

y If you use ANY and your assertion is wrong, the

results will be unpredictable

y The developer must design the algorithm so that

the results don’t depend on the degree of 

 parallelism

What if the results do depend

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 85/95

on the order of processing?

y We can be more explicit when we declare how we

want the input rows to be partitioned

y This requires that we have a strongly typed input

cursor, e.g … package Pkg is

type my_row is record ( n number, ... );

type cur_t is ref cursor return my_row;

end Pkg;

CLUSTER … BY

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 86/95

function Func_3 ( p_cur in Pkg.cur_t )return Pkg.my_tab

 pipelined 

 parallel_enable

( partition p_cur by hash (n) )

cluster p_cur by (n)

is...

y The algorithm requires that all rows for a given

value of n come together to a single slave…

y …but doesn’t care about the order 

ORDER … BY

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 87/95

function Func_4 ( p_cur in Pkg.cur_t )return Pkg.my_tab

 pipelined 

 parallel_enable

( partition p_cur by range (n) )

order p_cur by ( n, m )

is...

y  Not only must all rows for a given value of n come

together to a single slave…

y …but also they must be ordered by n, m 

Before…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 88/95

stage1stage1t1t1 stage2stage2t2t2 t3t3

datadata

warehouse

oltpoltp

warehouse

Using Oracle9i table functions…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 89/95

t3t3t2t2t1t1

t3t3t2t2t1t1

t3t3t2t2t1t1

t3t3t2t2t1t1

t3t3t2t2t1t1

datadata

warehouse

oltpoltp

warehouse

 pipelined 3  parallelized 3

Comparison…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 90/95

y Sun Ultra-Enterprise 4500

y

3 GB RAM, 10 CPUs of 168 MHzy 1,000,000 row source table

y 1 row in to 7 rows out pivot transform

y Oracle8i – PL/SQL cursor loop with 7 INSERTs

y Oracle9i – table function with 7 PIPE ROW s

 Performance and Scalability in DSS Environment with Oracle9i,

 Neil Thombre, Oracle Corp – OOW paper #822also on otn.oracle.com/deploy/performance/

Oracle9Oracle9i i 

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 91/95

8787 minmin

Oracle8Oracle8i i 

Oracle9Oracle9i i 

pipelinedpipelined

3737 minmin

2.42.4  x  x 

pipelinedpipelined

parallel 20parallel 20

1212 minmin

7.57.5  x  x 

Table Functions - summary

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 92/95

y An exciting new language feature

y Generic applicability…

think deluxe, parameterizable view

y Especially beneficial in

Extraction Load Transformationphase in warehousing applications

Oracle9i PL/SQL gives you…

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 93/95

y Improved performance

y Increased functionality

y Better usability for the developer

 you should upgrade ! you should upgrade !

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 94/95

Q U E S T I O N SQ U E S T I O N S

A N S W E R SA N S W E R S

7/28/2019 Plsql 9i New Features Ppt 129888

http://slidepdf.com/reader/full/plsql-9i-new-features-ppt-129888 95/95


Recommended