Performance Tuning
Performance Tuning
PLSQL engine uses the PLSQL optimizer to rearrange the code for better
performance.
View Execution Plan -> Autotrace, SQL Developer, SQL Monitor, DBMS_XPLAN, TKPROF.
Explain Plan:
-------------
An Explain Plan is a tool that you can use to have Oracle explain to you how it
plans on executing
your query. This is useful in tuning queries to the database to get them to
perform better.
Once you know how Oracle plans on executing your query, you can change your
environment to
run the query faster.
The EXPLAIN PLAN method doesn't require the query to be run, greatly reducing the
time it
takes to get an execution plan for long-running queries compared to AUTOTRACE.
First the query must
be explained.
EXAMPLE:
-------
There are some specific situations where user needs to do some changes of PL SQL
performance
tuning.
1. Datatype Conversion:
----------------------
While using the datatype conversion PL SQL engine uses the implicit conversion of
the
datatype.User needs to avoid the implicit conversion of datatypes.
DECLARE
V_No NUMBER;
V_Char CHAR(10);
BEGIN
V_No := V_No + 50; -- converted implicitly; slow
V_No := V_No + 50.0; -- not converted; fast
V_Char := 25; -- converted implicitly; slow
V_Char := TO_CHAR(25); -- converted explicitly; still slow
V_Char := '25'; -- not converted; fast
END;
The PLSQL code will slow down because of the inefficient use of SQL statements. So
make sure that
user needs to use the perfect SQL statement.
Analyze the execution plan of SQL statement and try to improve performance:
-----------------------------------------------------------------------------
Check the explain plan and try to reduce cost using indexes.
Check the joins using trace facility.
5. Looping Practices:
Initialization outside the loop. -> initialize the variables outside the loop to
improve the
performance of the PLSQL block.
Use of FORALL statement
Use of Union, Intersect, Minus, Hierarchical Queries.
Bulk Collect .
Check whether the AND condition and or condition use in SQL statement. Try to avoid
OR
condition as it is full scanning the table.
PLS_INTEGER -> PLS_INTEGER requires less storage than Integer or Number datatype.
BINARY_INTEGER -> The Binary_Integer is same as PLS_INTEGER but PLS_INTEGER is
faster in
Oracle 9i and Oracle 8i.
BINARY_DOUBLE / BINARY_FLOAT -> Kindly use BINARY_DOUBLE or BINARY_FLOAT for
floating
point number declaration in PLSQL.
Kindly use the ‘Execute immediate’ statement and cursor variables while developing
the PLSQL
blocks. Native dynamic SQL code is more compact and much faster than calling the
DBMS_SQL
package. User can use ref cursor to process dynamic SQL statements.