PL/SQL : Stop making the same performance mistakes

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

description

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 http://www.oracle-base.com Books Oracle PL/SQL Tuning - PowerPoint PPT Presentation

Transcript of PL/SQL : Stop making the same performance mistakes

Page 1: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

PL/SQL : Stop making the same PL/SQL : Stop making the same performance mistakesperformance mistakes

Tim HallTim Hall

Oracle ACE DirectorOracle ACE Director

Oracle ACE of the Year 2006Oracle ACE of the Year 2006

OakTable NetworkOakTable NetworkOCP DBA (7, 8, 8i, 9i, 10g, 11g)OCP DBA (7, 8, 8i, 9i, 10g, 11g)

OCP Advanced PL/SQL DeveloperOCP Advanced PL/SQL Developer

Oracle Database: SQL Certified ExpertOracle Database: SQL Certified Expert

http://www.oracle-base.com

BooksBooksOracle PL/SQL TuningOracle PL/SQL TuningOracle Job SchedulingOracle Job Scheduling

Page 2: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Stop using PL/SQL…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 helpLearning cool stuff like Analytic Functions will helpyou avoid writing unnecessary PL/SQL.you avoid writing unnecessary PL/SQL.

((setup.sql))

Page 3: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Stop using unnecessary PL/SQLStop using unnecessary PL/SQL

• Stop using UTL_FILE to read text files if you can use external Stop using UTL_FILE to read text files if you can use external tables. (tables. (external_table.sql))

• Stop writing PL/SQL merges if you can use the MERGE Stop writing PL/SQL merges if you can use the MERGE statement. (statement. (merge.sql))

• Stop coding multi-table inserts manually. (Stop coding multi-table inserts manually. (multitable.sql))

• Stop using FORALL when you could useStop using FORALL when you could useDML error logging (DML error logging (DBMS_ERRLOGDBMS_ERRLOG) to trap) to trapfailures in DML (failures in DML (dml_el.sql))

• All use DML, which is easily parallelized.All use DML, which is easily parallelized.

Page 4: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Overview of the PL/SQL EngineOverview 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 ServerOracle Server

PL/SQL EnginePL/SQL Engine

PL/SQLPL/SQLBlockBlock

PL/SQLPL/SQLBlockBlock

ProceduralProceduralStatementStatementExecutorExecutor

SQL StatementSQL StatementExecutorExecutor

Page 5: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Stop Avoiding Bulk-Binds (BULK COLLECT)Stop Avoiding Bulk-Binds (BULK COLLECT)

• Populate collections directly Populate collections directly from SQL using BULK from SQL using BULK COLLECT. (COLLECT. (bulk_collect.sql))

• Collections are held in Collections are held in memory, so watch collection memory, so watch collection sizes. (sizes. (bulk_collect_limit.sql))

• Implicit array processing Implicit array processing introduced in 10g.introduced in 10g.((implicit_array_processing.sql))

SELECT *SELECT *BULK COLLECT INTO l_tabBULK COLLECT INTO l_tabFROM tab1;FROM tab1;

OPEN c1;OPEN c1;LOOPLOOP FETCH c1FETCH c1 BULK COLLECTBULK 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;END LOOP;CLOSE c1;CLOSE c1;

FOR cur_rec IN (SELECT * FROM tab1)FOR cur_rec IN (SELECT * FROM tab1)LOOPLOOP -- Process row.-- Process row.END LOOP;END LOOP;

Page 6: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Stop Avoiding Bulk-Binds (FORALL)Stop Avoiding Bulk-Binds (FORALL)

• Bind data in collections into DMLBind data in collections into DMLusing FORALL. (using FORALL. (insert_forall.sql)insert_forall.sql)

• Triggers may not work as you expect.Triggers may not work as you expect.

• Use Use INDICIES OF and VALUES OF for sparse collections.INDICIES OF and VALUES OF for sparse collections.

• Use Use SQL%BULK_ROWCOUNT to return theSQL%BULK_ROWCOUNT to return thenumber of rows affected by each statement.number of rows affected by each statement.

• The SAVE EXCEPTIONS allows bulk operations toThe SAVE EXCEPTIONS allows bulk operations tocomplete.complete.

• Exceptions captured in SQL%BULK_EXCEPTIONS.Exceptions captured in SQL%BULK_EXCEPTIONS.

FORALL i IN l_tab.FIRST .. l_tab.LASTFORALL i IN l_tab.FIRST .. l_tab.LAST INSERT INTO tab2 VALUES l_tab(i);INSERT INTO tab2 VALUES l_tab(i);

Page 7: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Stop Using Pass-By-Value (NOCOPY)Stop Using Pass-By-Value (NOCOPY)

• By-value: Procedure uses temporary buffer. Copies value back By-value: Procedure uses temporary buffer. Copies value back on successful completion.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 The NOCOPY hint allows OUT and IN OUT parameter to be

passed by-reference, rather than by-value.passed by-reference, rather than by-value.

(nocopy.sql)(nocopy.sql)• Beware of affect of error handling and parameter Beware of affect of error handling and parameter

aliasing on parameter values.aliasing on parameter values.• It’s a hint, not a directive, so it can be ignoredIt’s a hint, not a directive, so it can be ignored

PROCEDURE myproc (p_tab IN OUT NOCOPY CLOB) ISPROCEDURE myproc (p_tab IN OUT NOCOPY CLOB) ISBEGINBEGIN -- Do something.-- Do something.END;END;

Page 8: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Stop Using the Wrong Datatypes Stop Using the Wrong Datatypes

• When you use the wrong datatypes, Oracle performs implicit When you use the wrong datatypes, Oracle performs implicit conversions.conversions.

• Datatype conversions take/waste time.Datatype conversions take/waste time.

• Oracle provide multiple numeric datatypes with differing Oracle provide multiple numeric datatypes with differing performance characteristics. (numeric_types.sql)performance characteristics. (numeric_types.sql)

• Use the appropriate datatype for the job.Use the appropriate datatype for the job.

Page 9: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

Quick PointsQuick 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 youStop waiting for requests to complete when youcould decouple processes.could decouple processes.

Page 10: PL/SQL : Stop making the same performance mistakes

http://www.oracle-base.com

The End… The End…

• Questions?Questions?

• References: References: http://www.oracle-base.comhttp://www.oracle-base.com

• Demos:Demos:http://www.oracle-base.com/workshopshttp://www.oracle-base.com/workshops