Tuning Oracle Stored Procedures: Guy Harrison CTO, APM Business Unit, Quest Software
Tuning Oracle Stored Procedures: Guy Harrison CTO, APM Business Unit, Quest Software
Agenda
Using PL/SQL in place of standard SQL Principles of code optimization applied to PL/SQL Using array processing in PL/SQL Using and optimizing packages and procedures Optimizing PL/SQL cursors Using the PL/SQL profiler A comparison of Java and PL/SQL performance Tuning Java stored procedures
Reduce client server traffic Divide and conquer complex statements Be aware of the drawbacks
Coding/maintenance overhead Inability to return result sets in many development tools
Correlated updates
The WHEN clause contains a query that is equivalent to a query in the SET clause
Remove any statements within a loop that could be processed outside the loop
Especially when you have nested loops
deoptimal loop
optimal loop
0.54
20
40
60
80
100
120
140
Recursive
Non-recursive
1.23
10
20
30
40
50
Array Processing
The BULK COLLECT statement allows data from a query to be loaded directly into a PL/SQL table FORALL allows data to be inserted into a table directly from a PL/SQL block For large operations, array processing is typically 10 times faster than non-array
Array Processing
33.23 Insert 3.02
12
24
30
36
But you need to check that the row has not been altered since you opened the cursor
16.2
Save ROWID
8.35
10
15
25
30
35
40
NOCOPY clause
If a subroutine takes a PL/SQL table as an argument, a copy of the table is created for the subroutine
This may be time consuming, especially if the subroutine is called repeatedly
Example of NOCOPY
Effect of NOCOPY
115.18
Using NOCOPY
0.04
20
40
60
80
100
120
140
Avoiding Recompilation
Use packages rather than procedures.
Providing the package header remains constant, changes to subroutines will not cause recompilation of dependent procedures
Optimizing Triggers
Ensure that triggers fire only when required
Use WHEN clause to restrict trigger execution to specific logical conditions Use the UPDATE OF clause to restrict trigger execution to changes to specific rows
Explicit Cursors
Although you can embed SQL directly in a routine, explicitly creating a cursor is more efficient
An implicit cursor does a second fetch to ensure that only one row is returned This is especially important if the SQL performs a table scan
Explicit Cursors
5827 table scan 143
1000
2000
4000
5000
6000
0.83
148.33
20
40
60
80
100
120
140
160
20
40
60
80
100
Thank you