Top Banner
© 2009 Quest Software, Inc. ALL RIGHTS RESERVED High Performance PL/SQL Guy Harrison Director of Development, Melbourne www.guyharrison.net
52

High Performance Plsql

Dec 07, 2014

Download

Technology

Guy Harrison

High Performance PLSQL - presentation given at Oracle Open World 2009
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: High Performance Plsql

© 2009 Quest Software, Inc. ALL RIGHTS RESERVED

High Performance PL/SQL

Guy Harrison

Director of Development, Melbourne

www.guyharrison.net

Page 2: High Performance Plsql

2

Introductions

Buy Guy’s BookBuy Quest Products

Page 3: High Performance Plsql

3

http://www.motivatedphotos.com/?id=17760

Page 4: High Performance Plsql

4

Blue

Yellow

Red

0 10 20 30 40 50 60 70 80

Star trek shirt fatality analysis

Pct

Page 5: High Performance Plsql

5

Measuring PL/SQL performance• DBMS_PROFILER is the best way to find PL/SQL “hot

spots”:

Page 6: High Performance Plsql

6Scripts at www.guyharrison.net

Page 7: High Performance Plsql

7

Toad Profiler support

Page 8: High Performance Plsql

8

SQL*Navigator profiler support

Page 9: High Performance Plsql

9

11g Hierarchical profiler

$ plshprof -output hprof demo1.trc

Page 10: High Performance Plsql

10

Plshprof output

Page 11: High Performance Plsql

11

DBMS_HPROF tables

Scripts at www.guyharrison.net

Page 12: High Performance Plsql

12

When is PL/SQL faster?

• PL/SQL routines most massively outperform other languages when network round trips are significant.

Page 13: High Performance Plsql

13

Network traffic• Routines that process large numbers of rows and return simple

aggregates are also candidates for a stored procedure approach

Page 14: High Performance Plsql

14

Stored procedure alternative

Page 15: High Performance Plsql

15

Network traffic example

Stored Procedure

Java client

0 200 400 600 800 1,000 1,200 1,400 1,600 1,800

344

1703

297

313

Local Host

Remote Host

Elapsed time (ms)

Page 16: High Performance Plsql

16

Aspects of PL/SQL performance

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 17: High Performance Plsql

17

PLSQL_OPTIMIZE_LEVEL

0: No optimization

1: Minor (eliminate but not reorganize)

2: (default) Significantly reorganize code (array fetch, optimize loops)

3: 11g specific (in-lining, etc)

Page 18: High Performance Plsql

18

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 19: High Performance Plsql

19

It’s usually the SQL • Most PL/SQL routines spend most of their time executing

SELECT statements and DML• SQL tuning is a big topic but:

– Measure SQL overhead of PL/SQL routines first– Ensure best possible optimizer statistics – Consider adequacy of indexing– Learn how to use DBMS_XPLAN, SQL Trace, etc – Exploit 10g/11g tuning facilities (if licensed)– Don’t issue SQL when you don’t need to

Page 20: High Performance Plsql

20

SQL or PL/SQL?

Scripts at www.guyharrison.net

Page 21: High Performance Plsql

21

Page 22: High Performance Plsql

22

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 23: High Performance Plsql

23

Three ways of processing rows C

PU

& lo

gica

l rea

ds

•One at a time

•In Batches

•All at once .

Mem

ory Requirem

entsFour

Page 24: High Performance Plsql

24

One at a time

Page 25: High Performance Plsql

25

All at once

Page 26: High Performance Plsql

26

In batches

Page 27: High Performance Plsql

27

Array processing

1 10 100 1000 10000 100000 10000000

20

40

60

80

100

120

140

160

180

200

Bulk Collect Size

Ela

ps

ed

Tim

e

No bulk collect

Bulk collect without LIMIT

(Prior to 10g or PLSQL_OPTIMIZE_LEVEL <2)

Page 28: High Performance Plsql

28

Array processing

• PLSQL_OPTIMIZE_LEVEL>1 causes transparent BULK COLLECT LIMIT 100

1 10 100 1000 10000 100000 1000000 100000000

50

100

150

200

250

300

Array Size

Ela

sped

tim

e (s

)

10g or higher with PLSQL_OPTIMIZE_LEVEL >1

No bulk collect

Bulk collect without LIMIT

Page 29: High Performance Plsql

29

BULK COLLECT worst case scenario

Page 30: High Performance Plsql

30

Bind variables in Dynamic SQL • Using bind variables allows sharable SQL, reduces parse overhead and

minimizes latch contention • Unlike other languages, PL/SQL uses bind variables transparently

– EXCEPT when using Dynamic SQL:

Page 31: High Performance Plsql

31

Using bind variables

Page 32: High Performance Plsql

32

Bind variable performance • 10,000 calls

Bind variables

No Binds

0 1 2 3 4 5 6 7 8

3.42

7.84

Elasped Time (s)

Page 33: High Performance Plsql

33

NOCOPY• The NOCOPY clause causes a parameter to be passed

“by reference” rather than “by value”

Page 34: High Performance Plsql

34

NOCOPY performance gains• 4,000 row, 10 column “table”; 4000 lookups:

NOCOPY

NO NOCOPY

0 100 200 300 400 500 600 700 800 900

0.28

864.96

Elapsed time (s)

Page 35: High Performance Plsql

35

Associative arrays • Traditionally, sequential scans of PLSQL tables are used for caching

database table data:

Page 36: High Performance Plsql

36

Associative arrays• Associative arrays allow for faster and simpler lookups:

Page 37: High Performance Plsql

37

Associative array performance• 10,000 random customer lookups with 55,000 customers

Associative lookups

Sequential scan

0 5 10 15 20 25 30

0.04

29.79

Elapsed time (s)

Page 38: High Performance Plsql

38

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 39: High Performance Plsql

39

Reduce unnecessary Looping• Unnecessary loop iterations burn CPU

Well formed loop

Poorly formed loop

0 5 10 15 20 25 30 35

3.96

34.31

Elapsed time (s)

Page 40: High Performance Plsql

40

Remove loop Invariant terms• Any term in a loop that does not vary should be extracted

from the loop

PLSQL_OPTIMIZE_LEVEL>1 does this automatically

Page 41: High Performance Plsql

41

Loop invariant performance improvements

plsql_optimize_level=2

Optimized loop

Original loop

0 2 4 6 8 10 12

5.28

5.87

11.09

Elapsed time (s)

Page 42: High Performance Plsql

42

Recursion (see: recursion)• Recursive routines often offer elegant solutions*.• However, deep recursion is memory-intensive and

usually not scalable

* Known In Australia as “smart-ass solutions”

*

Page 43: High Performance Plsql

43

Recursion memory overhead

0 2000000 4000000 6000000 8000000 100000000

200

400

600

800

1000

1200

1400

Recursive

Non-recursive

Recursive Depth

PG

A m

emo

ry (

MB

)

Page 44: High Performance Plsql

44

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 45: High Performance Plsql

45

Number crunching (1)

SIMPLE_INTEGER

PLS_INTEGER

NUMBER

0 5 10 15 20 25

5.99

7.06

20.09

0.54

3.83

17.64

NATIVE

INTERPRETED

Elasped Time (s)

Page 46: High Performance Plsql

46

Number crunching (2)

Java stored procedure

PLSQL compiled, SIMPLE_INTEGER datatype

PLSQL interpreted, PLS_INTEGER data type

PLSQL interpreted, NUMBER data type

0 5 10 15 20 25 30 35 40 45 50

0.11

0.74

14.48

47.22

Elapsed time (s)

Page 47: High Performance Plsql

47

11g Function cache• Suits deterministic but expensive functions• Expensive table lookups on non-volatile tables

Page 48: High Performance Plsql

48

Function cache performance • 100 executions, random date ranges 1-30 days:

Function cache

No function cache

0 1 2 3 4 5 6

1.51

5.21

Elapsed time (s)

Page 49: High Performance Plsql

49

In-lining

Manual in-liningModular design

Page 50: High Performance Plsql

50

Automatic in-lining• PLSQL_OPTIMIZE_LEVEL = 3

• OR:

PRAGMA INLINE

Manual Inlining

No Inlining

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

2.54

2.56

4.95

Elapsed time (s)

Page 51: High Performance Plsql

51

Ran out of time for...

• Array insert using FORALL• Explicit vs. implicit cursors • RETURNING clause • Pipelined functions• Optimizing triggers• Short circuit evaluations• IF and CASE comparison ordering

Page 52: High Performance Plsql

52

Thank You – Q&A