high performance plsql

60
© 2010 Quest Software, Inc. ALL RIGHTS RESERVED High Performance PL/SQL Guy Harrison Director of Development, Melbourne www.guyharrison.net [email protected] www.twitter.com/guyharrison

Upload: guy-harrison

Post on 09-Jun-2015

3.507 views

Category:

Technology


3 download

DESCRIPTION

Presentation given at RMOUG , Denver CO, Feb 17-18 2010 and Oracle Sydney meetup Feb 2011

TRANSCRIPT

Page 1: High Performance Plsql

© 2010 Quest Software, Inc. ALL RIGHTS RESERVED

High Performance PL/SQL

Guy Harrison

Director of Development, Melbourne

www.guyharrison.net

[email protected]

www.twitter.com/guyharrison

Page 2: High Performance Plsql

2

Introductions

Page 3: High Performance Plsql
Page 4: High Performance Plsql

4

Page 5: High Performance Plsql

5

Page 6: High Performance Plsql

6

Page 7: High Performance Plsql

7

Agenda

• Measuring PL/SQL Performance

• PL/SQL Performance advantages

• SQL Code

• PL/SQL Data handling

• PL/SQL code structure

• Compilation and datatype Tweaks

Page 8: High Performance Plsql

8

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

Page 9: High Performance Plsql

9Scripts at www.guyharrison.net

Page 10: High Performance Plsql

10

Toad Profiler support

Page 11: High Performance Plsql

11

SQL*Navigator profiler support

Page 12: High Performance Plsql

12

11g Hierarchical profiler

$ plshprof -output hprof demo1.trc

Page 13: High Performance Plsql

13

Plshprof output

Page 14: High Performance Plsql

14

DBMS_HPROF tables

Scripts at www.guyharrison.net

Page 15: High Performance Plsql

15

When is PL/SQL faster?

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

Page 16: High Performance Plsql

16

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

aggregates are also candidates for a stored procedure approach

Page 17: High Performance Plsql

17

Stored procedure alternative

Page 18: High Performance Plsql

18

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 19: High Performance Plsql

19

Aspects of PL/SQL performance

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 20: High Performance Plsql

20

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 21: High Performance Plsql

21

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 22: High Performance Plsql

22

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 23: High Performance Plsql

23

SQL or PL/SQL?

cachedPlsql.sql at guyharrison.net/opsg

Page 24: High Performance Plsql

24

Page 25: High Performance Plsql

25

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 26: High Performance Plsql

26

Three ways of retrieving rows C

PU

& lo

gica

l rea

ds

•On

e

at

a

tim

e

•In

Bat

ch

es

•All

at

on

ce

.

Mem

ory Requirem

ents

Page 27: High Performance Plsql

27

One at a time

Page 28: High Performance Plsql

28

All at once

Page 29: High Performance Plsql

29

In batches

Page 30: High Performance Plsql

30

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 31: High Performance Plsql

31

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 32: High Performance Plsql

32

BULK COLLECT worst case scenario

Page 33: High Performance Plsql

33

Array Insert• No Array Insert

• Insert all in single array:

Page 34: High Performance Plsql

34

Array Insert Performance

FORALL

FOR loop

0 50 100 150 200 250 300 350

25.06

341.86

Elapsed (s)

Page 35: High Performance Plsql

35

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 36: High Performance Plsql

36

Using bind variables

Page 37: High Performance Plsql

37

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 38: High Performance Plsql

38

NOCOPY• The NOCOPY clause causes a parameter to be passed “by

reference” rather than “by value”

Page 39: High Performance Plsql

39

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 40: High Performance Plsql

40

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

database table data:

Page 41: High Performance Plsql

41

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

Page 42: High Performance Plsql

42

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 43: High Performance Plsql

43

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 44: High Performance Plsql

44

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 45: High Performance Plsql

45

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 46: High Performance Plsql

46

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 47: High Performance Plsql

47

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 48: High Performance Plsql

48

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 49: High Performance Plsql

49

Compilation and datatype Tweaks

PL/SQL code structure

PL/SQL Data handling

SQL Code

Page 50: High Performance Plsql

50

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 51: High Performance Plsql

51

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.740000000000001

14.48

47.22

Elapsed time (s)

Page 52: High Performance Plsql

52

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

Page 53: High Performance Plsql

53

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 54: High Performance Plsql

54

In-lining

Manual in-liningModular design

Page 55: High Performance Plsql

55

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 56: High Performance Plsql

56

Expression “Short circuit”• Put the least likely condition first in an AND expression. Oracle

doesn’t need to evaluate the second expression if the first is FALSE.

Page 57: High Performance Plsql

57

Expression “Short circuit”• Put the most likely expression first in an OR expression. Oracle

does not have to evaluate the second expression if the first is TRUE.

Page 58: High Performance Plsql

58

Expression short circuit

OR

AND

0 50 100 150 200 250 300 350

216

308

295

181

Least likely first

Most likely first

PLSQL Elapsed time (ms)

Typ

e o

f co

mp

aris

on

Page 59: High Performance Plsql

59

IF and CASE statement ordering

Most likely First

Least likely First

0 500 1000 1500 2000 2500 3000 3500 4000

781

3,657

PL/SQL execution time (ms)

IF C

lau

se o

rder

ing

Page 60: High Performance Plsql

© 2010 Quest Software, Inc. ALL RIGHTS RESERVED

너를 감사하십시요 Thank You Danke Schön

Gracias 有難う御座いました Merci

बहवः� धन्यवःदाः� Obrigado 谢谢