High Performance PL/SQL<br />Guy Harrison<br />Director of Development, Melbourne<br />www.guyharrison.net<br />
Introductions <br />Buy Guy’s Book<br />Buy Quest Products <br />
http://www.motivatedphotos.com/?id=17760<br />
Measuring PL/SQL performance<br />DBMS_PROFILER is the best way to find PL/SQL “hot spots”:<br />
Scripts at www.guyharrison.net<br />
Toad Profiler support<br />
SQL*Navigator profiler support<br />
11g Hierarchical profiler<br />$ plshprof -output hprof demo1.trc<br />
Plshprof output <br />
DBMS_HPROF tables <br />Scripts at www.guyharrison.net<br />
When is PL/SQL faster?<br />PL/SQL routines most massively outperform other languages when network round trips are signifi...
Network traffic<br />Routines that process large numbers of rows and return simple aggregates are also candidates for a st...
Stored procedure alternative <br />
Network traffic example <br />
Aspects of PL/SQL performance <br />
PLSQL_OPTIMIZE_LEVEL<br />
It’s usually the SQL <br />Most PL/SQL routines spend most of their time executing SELECT statements and DML<br />SQL tuni...
SQL or PL/SQL?<br />Scripts at www.guyharrison.net<br />
Three ways of processing rows <br />Four<br />Memory Requirements<br />CPU & logical reads<br />
One at a time<br />
All at once <br />
In batches<br />
Array processing<br />Bulk collect without LIMIT<br />No bulk collect <br />(Prior to 10g or PLSQL_OPTIMIZE_LEVEL &lt;2)<b...
Array processing<br />PLSQL_OPTIMIZE_LEVEL&gt;1 causes transparent BULK COLLECT LIMIT 100<br />Bulk collect without LIMIT<...
BULK COLLECT worst case scenario <br />
Bind variables in Dynamic SQL <br />Using bind variables allows sharable SQL, reduces parse overhead and minimizes latch c...
Using bind variables <br />
Bind variable performance <br />10,000 calls<br />
NOCOPY<br />The NOCOPY clause causes a parameter to be passed “by reference” rather than “by value” <br />
NOCOPY performance gains<br />4,000 row, 10 column “table”;  4000 lookups:<br />
Associative arrays <br />Traditionally, sequential scans of PLSQL tables are used for caching database table data:<br />
Associative arrays<br />Associative arrays allow for faster and simpler lookups:<br />
Associative array performance<br />10,000 random customer lookups with 55,000 customers<br />
Reduce unnecessary Looping<br />Unnecessary loop iterations burn CPU<br />
Remove loop Invariant terms<br />Any term in a loop that does not vary should be extracted from the loop<br />PLSQL_OPTIMI...
Loop invariant performance improvements<br />
Recursion (see: recursion)<br />Recursive routines often offer elegant solutions*.<br />However, deep recursion is memory-...
Recursion memory overhead <br />
Number crunching (1)<br />
Number crunching (2)<br />
11g Function cache<br />Suits deterministic but expensive functions<br />Expensive table lookups on non-volatile tables<br />
Function cache performance <br />100 executions, random date ranges 1-30 days:<br />
In-lining<br />Manual in-lining<br />Modular design <br />
Automatic in-lining<br />PLSQL_OPTIMIZE_LEVEL = 3<br />OR:<br />
Ran out of time for...<br />Array insert using FORALL<br />Explicit vs. implicit cursors <br />RETURNING clause <br />Pipe...
Thank You – Q&A<br />
Upcoming SlideShare
Loading in...5
×

High Performance Plsql

4,269
-1

Published on

High Performance PLSQL - presentation given at Oracle Open World 2009

Published in: Technology, Business
1 Comment
5 Likes
Statistics
Notes
  • http://dbmanagement.info/Tutorials/PLSQL.htm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
4,269
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
216
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide
  • I’m worried about the Toad in the red shirt – we all know that red-shirt crewmen die in Star Trek!
  • This presentation contains a wide variety of PL/SQL performance best practicesand tuning techniques, but we shouldn’t just pick one of these at random! Instead,our first step should be to identify the most resource-intensive lines ofPL/SQL code and start by optimizing that code.To do this, we use the PL/SQL profiler. The profiler is implemented in thepackage DBMS_PROFILER. When we surround a program call with START_PROFILER and STOP_PROFILER calls, Oracle collates execution statistics on aline-by-line basis.
  • The profiling data is stored in a collection of tables prefixed withPLSQL_PROFILER. The above is a query that reports the most expensivelines of code (in terms of execution time) in the profiling run
  • Much of the time identifying the most-expensive lines of code is sufficient to discoverhot spots and tuning opportunities. But on other occasions you need toidentify expensive subroutines, or identify the calling routine to understand the context in which a line of code is being executed. To help with these scenarios,Oracle introduced the hierarchical profiler in Oracle 11g.You access this profiler via the DBMS_HPROF package. START_PROFILING and STOP PROFILING procedures commence and terminate the profilingrun. The output from the profiling session will be written to the external file identifiedin the START_PROFILING call. If you want to load this file into database tablesfor analyses, you can do so by using the ANALYZE procedure.In this example, we profile the NIGHTLY_BATCH procedure to an externalfile hprof_trace.trc that is created in the HPROF_DIR directory. We then load thetrace file into the profiling tables using the ANALYZE procedure
  • There are two ways to analyze the trace file. First, the plshprof commandline utility converts the trace file into an html report. We can view the report by pointing our browser at the hprof_report.htmlfile generated by the preceding command. The figure above shows the report.
  • Personally, I find the HTML report a bit hard to interpret and prefer to issueSQL against the profiler tables. In the above example, a hierarchical self-join isissued that exposes the call tree
  • Consider a scenario in which an application accepts input from the enduser, reads some data in the database, decides what statement to execute next, retrievesa result, makes a decision, executes some SQL, and so on. If the applicationcode is written entirely outside of the database, each of these steps would requirea network round trip between the database and the application. The timetaken to perform these network trips can easily dominate overall user responsetime.The right hand diagram shows the sequences of interactions that would be required withouta stored procedure approach.On the other hand, if a stored program is used to implement the fundstransfer logic, only a single database interaction is required. The stored programtakes responsibility for checking balances, withdrawal limits, and soon. The figure on the left illustrates stored procedure network traffic.
  • Network round trips can also become significant when an application is requiredto perform some kind of aggregate processing on large record sets in thedatabase. If the application needs to (for example) retrieve millions of rows to calculatesome sort of business metric that cannot easily be computed using nativeSQL (average time to complete an order, for example), a large number of roundtrips can result. In such a case, the network delay can again become the dominantfactor in application response time. Performing the calculations in a stored programwill reduce network overhead, which might reduce overall response time.The key determining factor will be the network latency
  • The further the code is (in network terms) from the database, the more the network effects will magnify. You can’t get any closer to the database than being inde the database as PL/SQL
  • For the majority of PL/SQL programs, the biggest gains will be obtained by tuning the SQL within the PL/SQL.Next most important is usually to ensure that PL/SQL handles data efficiently: by using array fetch and insert, by using the right types of collections and best practices such as associative array lookups and NOCOPY parameters. Thirdly one should look at the algorithmic efficiency of the PL/SQL code: loop structure, ordering of conditionals, avoiding recursion and so on.Usually the least gain is obtained by tweaking parameters or data types. Two exception are:If PLSQL_OPTIMIZE_LEVEL is <2 (10g) or 3 (11g) then increasing it can sometimes lead to significant improvementsWhen doing number crunching, data types and native compilation are recommended
  • Most parameters have little effect, but PLSQL_OPTIMIZE_LEVEL has major effect on PLSQL code, similar to the effects of an optimizing compiler in other languages. Level 0: No optimizationLevel 1: Minor optimizations, not much reorganizationLevel 2: (the default) Significant reorganization including loop optimizations and automatic bulk collectLevel 3: (11g only) Further optimizations, notably automatic in-lining of subroutines
  • This script shows the amount of times routines spent in PL/SQL code compared to total execution timeThe procedure shown spends only 3% of it’s time executing PL/SQL code: the vast bulk of time is spent by SQL embedded within the PL/SQL.
  • If you are a TOAD or SQL*Navigator user, consider the Xpert editions or the Development suites. These include SQL optimizer which can help you find and tune SQL
  • PL/SQL can fetch rows from the database one at a time, all at once or in batches. The more rows fetched in a batch, the lower the CPU / logical read overhead, but the greater the memory requirementsThe forth way is not to retrieve it at all. I.e. Can you do all you work in SQL without pulling the rows into PL/SQL?
  • Prior to 10g this is inefficient:*Excessive loop iterations Increases logical reads (rows in the same block fetched separately)In other languages, this pattern causes excessive network traffic, but luckily in PL/SQL this is not an issue.
  • Selects all data in a single operationLarge result sets might take longer as memory growsOther concurrent sessions may have limited memory for sorts, etc. Out of memory errors are possibleIf you are connected via MTS and have Auto Shared Memory Management (ASMM) or Automatic Memory Management (AMM) you can actually use up all of the PGA and SGA memory.
  • Considered best:Never more that p_array_size elements in collectionBest throughput, acceptable memory utilization
  • See http://guyharrison.squarespace.com/blog/2009/9/18/mts-amm-bulk-collect-trouble.html
  • Note that this example is also vulnerable to SQL injection. Bind variables enable SQLs that are essentially identical, differing onlyin parameter values, to be parsed only once and then executed many times. Usingbind variables reduces parse overhead—a CPU-intensive operation—and also reducescontention for latches and mutexes that protect shared SQL structures inthe shared pool.In most programming languages, we have to go to special effort to use bindvariables, and sometimes the convoluted code that results can be tedious to writeand hard to maintain. However in PL/SQL, bind variables are employed automatically:Every PL/SQL variable inside a SQL statement is effectively a bindvariable and, therefore, PL/SQL programs rarely suffer from parse overhead andassociated latch/mutex contention issues that are all too common in languagessuch as PHP, Java, or C#.However, when we use dynamic SQL in PL/SQL, this automatic bindingdoes not occur: Dynamically constructed SQL in PL/SQL is just
  • This implementation defines the bind variable placeholder as :columnValuein the dynamic SQL string. The actual value is provided by the USING clause.Although we still request the parse every time this routine is executed, Oraclewill quite possibly find a matching SQL in the shared pool, providing onlythat the same table and column names have been used in a previous execution.
  • As well as this performance gain, we reduce the chance of mutex contention if there are other sessions are trying to execute similar routines.
  • Users of other programming languages might be familiar with the concept ofpassing a parameter by value as opposed to by reference. When we pass a parameterby value, we create a copy of the parameter for the subroutine to use. Whenwe pass the parameter by reference, the subroutine uses the actual variablepassed; any changes made to the variable in the subroutine are visible to the callingroutine.The NOCOPY directive instructs PL/SQL function or procedure to use theparameter variable directly, by reference, rather than making a copy. This is animportant optimization when passing large PL/SQL collections into a subroutinebecause otherwise the process of creating a copy can consume significant resources.
  • Prior to Oracle 9.2, when seeking a non-numeric value in such a cache, we might use two collections: oneof which contained keys and the other which contained values. For instance, inthe following example, we scan through the G_CUST_NAMES table looking for aspecific customer name and date of birth. birth. If we find a match, we look in the correspondingindex of the G_CUST_IDS table to find the CUSTOMER_ID.
  • Associative arrays offer a more efficient solution. An associativearray might be indexed by a non-numeric variable; so we can look up thematching customer name directly. And the code is simpler.
  • The LOOP–END LOOP clauses repeatedly execute statements within a loop. Becausestatements within a loop are executed many times, they can often consumea high proportion of overall execution time. A badly constructed loop can have adrastic effect on performance.The code above illustrates the principle of exiting a loop as early as possible.The code calculates the number of prime numbers less than the numberprovided as a parameter (P_NUM). It does this by attempting to divide the numberby every number smaller than the parameter.I originally wrote this code many years ago when comparing Java andPL/SQL performance. In the first version of this code, I omitted to include theEXIT statement included as a comment in line 16. The program worked but performedpoorly because it kept looping when it did not need to. NB: PLSQL_OPTIMIZE_LEVEL cannot help here.
  • Loop invariant expressions are those that don’t change with each iteration ofthe loop. For instance in the following code, the expressions on lines 5 and 7 remainunchanged with each iteration of the loop that begins on line 3. Recalculatingthese expressions with each iteration of the loop is unnecessary and wastesCPU cycles.
  • A recursive routine is one that invokes itself. Recursive routines often offer elegantsolutions to complex programming problems but tend to consume large amountsof memory and to be less efficient than iterative—loop based—alternatives.
  • Remember, the memory youscomes from a pool of memory shared between sessions. Other sessions might have reduced access to memory.
  • So far, we have used the Oracle NUMBER data type when performing numericcomputation. The NUMBER type is extremely flexible and capable of storing bothhigh-precision and high-magnitude numbers. However, this flexibility comes at acost when performing numeric computations: Certain numeric calculations willbe faster if a less flexible data type is chosen.In particular, the PLS_INTEGER and SIMPLE_INTEGER data types performfaster than the NUMBER type for computation. Both are signed 32 bit integers,which means that they can store numbers between –2,147,483,648 and2,147,483,648. SIMPLE_INTEGER is the same as PLS_INTEGER but cannot beNULL and overflows (attempts to store numbers larger than 2,147,483,648, for instance)will not cause exceptions to be raised. SIMPLE_INTEGER can offer a performanceadvantage when the PL/SQL package is compiled to native code.
  • Oracle enables you to create stored procedures in the Java language. Java-storedprocedures can outperform PL/SQL for number crunching, though the advantagesof Java for computation have been steadily decreasing with each release of Oracle.When Java was first introduced, performance gains of anywhere between 10 to 100times were achievable when rewriting computationally intensive PL/SQL routinesin Java. However, improvements in PL/SQL language efficiency, including someof the optimizations outlined in this chapter, have closed the gap.As with all computational optimizations, using Java to optimize numbercrunching operations is not generally advisable for PL/SQL programs that aredatabase-intensive. Efforts to optimize math operations for a PL/SQL programthat does mainly database operations are probably misdirected.Furthermore, a lot of the advantages that Java had over PL/SQL in previousreleases can be overcome in Oracle 11g by using efficient data types, SIMPLE_INTEGER, for example, and native compilation.
  • This example illustrates a drawback of function caching. The query is not strictly deterministic, and should be flushed on a daily basis. Oracle 11g introduced the result set cache, which allows entire result sets to becached in memory. SQL statements that perform expensive operations on relativelystatic tables can benefit tremendously from the result set cache.The function cache is a related facility that can benefit PL/SQL routines orSQL statements that call PL/SQL functions. Oracle 11g can store the results of a PL/SQL function in memory and, if the function is expensive to resolve, can retrievethe results for the function from memory more efficiently than by reexecutingthe function.You might want to use the function cache in the following circumstances:❏ You have a computationally expensive function that is deterministic: It willalways return the same results given the same inputs.❏ You have a database access routine encapsulated in a function that accessestables that are infrequently updated.
  • Einstein’s famous equation is encapsulated in the MASS_TO_ENERGY subroutinein lines 1–9; this subroutine is called multiple times from within the EMC2Bprocedure at line 17. This encapsulation represents good programming practice,especially if Einstein’s equation needs to be called from other routines. (Perhaps thispackage will be utilized in Larry Ellison’s upcoming intergalactic yacht.) However,the subroutine calls add overhead, and from a performance point of view, it wouldprobably be better to include the equation directly within the calling routine, like this:
  • With 11g in-lining, you can write your code using modularity and encapsulationwithout paying a performance penalty because Oracle can automatically moverelevant subroutines in-line to improve performance. The optimizer will performsome in-lining automatically if PLSQL_OPTIMZE_LEVEL=3. If you want to performin-lining when PLSQL_OPTIMIZE_LEVEL=2 (the default in Oracle 11g) oryou want to increase the likelihood of in-lining when PLSQL_OPTIMIZE_LEVEL=3.
  • Some of these and other topics are covered in Chapter 12 of my book “Oracle Performance Survival Guide”.
  • High Performance Plsql

    1. 1. High Performance PL/SQL<br />Guy Harrison<br />Director of Development, Melbourne<br />www.guyharrison.net<br />
    2. 2. Introductions <br />Buy Guy’s Book<br />Buy Quest Products <br />
    3. 3. http://www.motivatedphotos.com/?id=17760<br />
    4. 4.
    5. 5. Measuring PL/SQL performance<br />DBMS_PROFILER is the best way to find PL/SQL “hot spots”:<br />
    6. 6. Scripts at www.guyharrison.net<br />
    7. 7. Toad Profiler support<br />
    8. 8. SQL*Navigator profiler support<br />
    9. 9. 11g Hierarchical profiler<br />$ plshprof -output hprof demo1.trc<br />
    10. 10. Plshprof output <br />
    11. 11. DBMS_HPROF tables <br />Scripts at www.guyharrison.net<br />
    12. 12. When is PL/SQL faster?<br />PL/SQL routines most massively outperform other languages when network round trips are significant.<br />
    13. 13. Network traffic<br />Routines that process large numbers of rows and return simple aggregates are also candidates for a stored procedure approach<br />
    14. 14. Stored procedure alternative <br />
    15. 15. Network traffic example <br />
    16. 16. Aspects of PL/SQL performance <br />
    17. 17. PLSQL_OPTIMIZE_LEVEL<br />
    18. 18.
    19. 19. It’s usually the SQL <br />Most PL/SQL routines spend most of their time executing SELECT statements and DML<br />SQL tuning is a big topic but:<br />Measure SQL overhead of PL/SQL routines first<br />Ensure best possible optimizer statistics <br />Consider adequacy of indexing<br />Learn how to use DBMS_XPLAN, SQL Trace, etc <br />Exploit 10g/11g tuning facilities (if licensed)<br />Don’t issue SQL when you don’t need to<br />
    20. 20. SQL or PL/SQL?<br />Scripts at www.guyharrison.net<br />
    21. 21.
    22. 22.
    23. 23. Three ways of processing rows <br />Four<br />Memory Requirements<br />CPU & logical reads<br />
    24. 24. One at a time<br />
    25. 25. All at once <br />
    26. 26. In batches<br />
    27. 27. Array processing<br />Bulk collect without LIMIT<br />No bulk collect <br />(Prior to 10g or PLSQL_OPTIMIZE_LEVEL &lt;2)<br />
    28. 28. Array processing<br />PLSQL_OPTIMIZE_LEVEL&gt;1 causes transparent BULK COLLECT LIMIT 100<br />Bulk collect without LIMIT<br />No bulk collect <br />10g or higher with PLSQL_OPTIMIZE_LEVEL &gt;1<br />
    29. 29. BULK COLLECT worst case scenario <br />
    30. 30. Bind variables in Dynamic SQL <br />Using bind variables allows sharable SQL, reduces parse overhead and minimizes latch contention <br />Unlike other languages, PL/SQL uses bind variables transparently<br />EXCEPT when using Dynamic SQL:<br />
    31. 31. Using bind variables <br />
    32. 32. Bind variable performance <br />10,000 calls<br />
    33. 33. NOCOPY<br />The NOCOPY clause causes a parameter to be passed “by reference” rather than “by value” <br />
    34. 34. NOCOPY performance gains<br />4,000 row, 10 column “table”; 4000 lookups:<br />
    35. 35. Associative arrays <br />Traditionally, sequential scans of PLSQL tables are used for caching database table data:<br />
    36. 36. Associative arrays<br />Associative arrays allow for faster and simpler lookups:<br />
    37. 37. Associative array performance<br />10,000 random customer lookups with 55,000 customers<br />
    38. 38.
    39. 39. Reduce unnecessary Looping<br />Unnecessary loop iterations burn CPU<br />
    40. 40. Remove loop Invariant terms<br />Any term in a loop that does not vary should be extracted from the loop<br />PLSQL_OPTIMIZE_LEVEL&gt;1 does this automatically<br />
    41. 41. Loop invariant performance improvements<br />
    42. 42. Recursion (see: recursion)<br />Recursive routines often offer elegant solutions*.<br />However, deep recursion is memory-intensive and usually not scalable<br />*<br />* Known In Australia as “smart-ass solutions”<br />
    43. 43. Recursion memory overhead <br />
    44. 44.
    45. 45. Number crunching (1)<br />
    46. 46. Number crunching (2)<br />
    47. 47. 11g Function cache<br />Suits deterministic but expensive functions<br />Expensive table lookups on non-volatile tables<br />
    48. 48. Function cache performance <br />100 executions, random date ranges 1-30 days:<br />
    49. 49. In-lining<br />Manual in-lining<br />Modular design <br />
    50. 50. Automatic in-lining<br />PLSQL_OPTIMIZE_LEVEL = 3<br />OR:<br />
    51. 51. Ran out of time for...<br />Array insert using FORALL<br />Explicit vs. implicit cursors <br />RETURNING clause <br />Pipelined functions<br />Optimizing triggers<br />Short circuit evaluations<br />IF and CASE comparison ordering<br />
    52. 52. Thank You – Q&A<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×