Presentation is loading. Please wait.

Presentation is loading. Please wait.

PL/SQL : Stop making the same performance mistakes Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OakTable Network.

Similar presentations


Presentation on theme: "PL/SQL : Stop making the same performance mistakes Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OakTable Network."— Presentation transcript:

1 PL/SQL : Stop making the same performance mistakes Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OakTable Network OCP DBA (7, 8, 8i, 9i, 10g, 11g) OCP Advanced PL/SQL Developer Oracle Database: SQL Certified Expert Books Oracle PL/SQL Tuning Oracle Job Scheduling

2 Stop using PL/SQL… … when you could use SQL.… when you could use SQL. PL/SQL is a procedural extension to SQL, not a replacement for it.PL/SQL is a procedural extension to SQL, not a replacement for it. SQL is usually quicker than the PL/SQL alternative.SQL is usually quicker than the PL/SQL alternative. Be an SQL expert who knows some PL/SQL, not the other way round.Be an SQL expert who knows some PL/SQL, not the other way round. Learning cool stuff like Analytic Functions will help you avoid writing unnecessary PL/SQL. (setup.sql)Learning cool stuff like Analytic Functions will help you avoid writing unnecessary PL/SQL. (setup.sql)setup.sql

3 Stop using unnecessary PL/SQL Stop using UTL_FILE to read text files if you can use external tables. (external_table.sql)Stop using UTL_FILE to read text files if you can use external tables. (external_table.sql)external_table.sql Stop writing PL/SQL merges if you can use the MERGE statement. (merge.sql)Stop writing PL/SQL merges if you can use the MERGE statement. (merge.sql)merge.sql Stop coding multi-table inserts manually. (multitable.sql)Stop coding multi-table inserts manually. (multitable.sql)multitable.sql Stop using FORALL when you could use DML error logging (DBMS_ERRLOG) to trap failures in DML (dml_el.sql)Stop using FORALL when you could use DML error logging (DBMS_ERRLOG) to trap failures in DML (dml_el.sql)dml_el.sql All use DML, which is easily parallelized.All use DML, which is easily parallelized.

4 Overview of the PL/SQL Engine PL/SQL contains procedural and SQL code.PL/SQL contains procedural and SQL code. Each type of code is processed separately.Each type of code is processed separately. Switching between code types causes an overhead.Switching between code types causes an overhead. The overhead is very noticeable during batch operations.The overhead is very noticeable during batch operations. Bulk binds minimize this overhead.Bulk binds minimize this overhead. Oracle Server PL/SQL Engine PL/SQLBlockPL/SQLBlockProceduralStatementExecutor SQL Statement Executor

5 Stop Avoiding Bulk-Binds (BULK COLLECT) Populate collections directly from SQL using BULK COLLECT. (bulk_collect.sql)Populate collections directly from SQL using BULK COLLECT. (bulk_collect.sql)bulk_collect.sql Collections are held in memory, so watch collection sizes. (bulk_collect_limit.sql)Collections are held in memory, so watch collection sizes. (bulk_collect_limit.sql)bulk_collect_limit.sql Implicit array processing introduced in 10g. (implicit_array_processing.sql)Implicit array processing introduced in 10g. (implicit_array_processing.sql)implicit_array_processing.sql SELECT * BULK COLLECT INTO l_tab FROM tab1; OPEN c1; LOOP FETCH c1 FETCH c1 BULK COLLECT BULK COLLECT INTO l_tab LIMIT 1000; INTO l_tab LIMIT 1000; EXIT WHEN l_tab.count = 0; EXIT WHEN l_tab.count = 0; -- Process chunk. -- Process chunk. END LOOP; CLOSE c1; FOR cur_rec IN (SELECT * FROM tab1) LOOP -- Process row. -- Process row. END LOOP;

6 Stop Avoiding Bulk-Binds (FORALL) Bind data in collections into DML using FORALL. (insert_forall.sql)Bind data in collections into DML using FORALL. (insert_forall.sql)insert_forall.sql Triggers may not work as you expect.Triggers may not work as you expect. Use INDICIES OF and VALUES OF for sparse collections.Use INDICIES OF and VALUES OF for sparse collections. Use SQL%BULK_ROWCOUNT to return the number of rows affected by each statement.Use SQL%BULK_ROWCOUNT to return the number of rows affected by each statement. The SAVE EXCEPTIONS allows bulk operations to complete.The SAVE EXCEPTIONS allows bulk operations to complete. Exceptions captured in SQL%BULK_EXCEPTIONS.Exceptions captured in SQL%BULK_EXCEPTIONS. FORALL i IN l_tab.FIRST.. l_tab.LAST INSERT INTO tab2 VALUES l_tab(i); INSERT INTO tab2 VALUES l_tab(i);

7 Stop Using Pass-By-Value (NOCOPY) By-value: Procedure uses temporary buffer. Copies value back on successful completion.By-value: Procedure uses temporary buffer. Copies value back on successful completion. By-reference: Procedure uses original memory location directly.By-reference: Procedure uses original memory location directly. The NOCOPY hint allows OUT and IN OUT parameter to be passed by-reference, rather than by-value. (nocopy.sql)The NOCOPY hint allows OUT and IN OUT parameter to be passed by-reference, rather than by-value. (nocopy.sql)nocopy.sql Beware of affect of error handling and parameter aliasing on parameter values.Beware of affect of error handling and parameter aliasing on parameter values. Its a hint, not a directive, so it can be ignoredIts a hint, not a directive, so it can be ignored PROCEDURE myproc (p_tab IN OUT NOCOPY CLOB) IS BEGIN -- Do something. -- Do something.END;

8 Stop Using the Wrong Datatypes When you use the wrong datatypes, Oracle performs implicit conversions.When you use the wrong datatypes, Oracle performs implicit conversions. Datatype conversions take/waste time.Datatype conversions take/waste time. Oracle provide multiple numeric datatypes with differing performance characteristics. (numeric_types.sql)Oracle provide multiple numeric datatypes with differing performance characteristics. (numeric_types.sql)numeric_types.sql Use the appropriate datatype for the job.Use the appropriate datatype for the job.

9 Quick Points Stop using index searches when you can use ROWIDs.Stop using index searches when you can use ROWIDs. Stop using custom code when Oracle provide built-in functions.Stop using custom code when Oracle provide built-in functions. Stop using explicit cursors.Stop using explicit cursors. Stop avoiding instrumentation in your code.Stop avoiding instrumentation in your code. Short-Circuit Evaluations.Short-Circuit Evaluations. Logic/Branching order.Logic/Branching order. Stop waiting for requests to complete when you could decouple processes.Stop waiting for requests to complete when you could decouple processes.

10 The End… Questions?Questions? References: Demos:


Download ppt "PL/SQL : Stop making the same performance mistakes Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OakTable Network."

Similar presentations


Ads by Google