0% found this document useful (0 votes)
7 views4 pages

Performance Tuning

The document provides guidelines for PL/SQL performance tuning, emphasizing the use of the PLSQL optimizer, execution plans, and efficient SQL statements. Key strategies include avoiding implicit datatype conversions, utilizing appropriate indexing, and leveraging PL/SQL features like FORALL and Bulk Collect. Additionally, it highlights best practices for looping, string functions, and the use of native dynamic SQL for improved performance.

Uploaded by

shalih786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Performance Tuning

The document provides guidelines for PL/SQL performance tuning, emphasizing the use of the PLSQL optimizer, execution plans, and efficient SQL statements. Key strategies include avoiding implicit datatype conversions, utilizing appropriate indexing, and leveraging PL/SQL features like FORALL and Bulk Collect. Additionally, it highlights best practices for looping, string functions, and the use of native dynamic SQL for improved performance.

Uploaded by

shalih786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

PL/SQL Performance Tuning:

 PLSQL engine uses the PLSQL optimizer to rearrange the code for better
performance.

we can tune it using explain & execution plan

 Explain -> predicts how Oracle will process your query.


 Execution -> Describes the steps it actually took.

 View Execution Plan -> Autotrace, SQL Developer, SQL Monitor, DBMS_XPLAN, TKPROF.

 Generate Execution Plan -> EXPLAIN PLAN Command, V$SSQL_PLAN.

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:
-------

EXPLAIN PLAN FOR


SELECT *
FROM emp e, dept d
WHERE e.deptno = d.deptno
AND e.ename = 'SMITH';
SET LINESIZE 130
SET PAGESIZE 0
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);
-----------------------------------------------------------------------------------
------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
-----------------------------------
| 0 | SELECT STATEMENT | | 1 | 58 | 4 (0) | 00:00:01 |
| 1 | NESTED LOOPS | | | | | |
| 2 | NESTED LOOPS | | 1 | 58 | 4 (0) | 00:00:01 |
|* 3 | TABLE ACCESS FULL | EMP | 1 | 38 | 3 (0) | 00:00:01 |
|* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0) | 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 20 | 1 (0) | 00:00:01 |
-----------------------------------------------------------------------------------
------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("E"."ENAME"='SMITH')
4 - access("E"."DEPTNO"="D"."DEPTNO")
18 rows selected.
 table_name - Name of the PLAN_TABLE, default value 'PLAN_TABLE'.
 statement_id - Statement id of the plan to be displayed. The default value is
NULL, which
displays the most recent execution plan in the PLAN_TABLE.
 format - Controls the level of detail displayed, default value 'TYPICAL'. Other
values include
'BASIC', 'ALL', 'SERIAL'. There is also an undocumented 'ADVANCED' setting

 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;

2. Make use of Efficient SQL statements for PL/SQL Performance tuning:


---------------------------------------------------------------------

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.

 Appropriate Index use:


-----------------------
 User needs to create bit map indexes in case of distinct values in the
table.
 User needs to create simple index in case of non-distinct values.
 User needs to create composite index in case of joins (hash tables).

 Up to date table stats:


------------------------
 Stats -> Statistics.
 User should gather the stats of tables after indexing the columns or
changes in table
structure.
 Kindly use the subprogram of DBMS_STATS package to gather the stats.

 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.

 Change the SQL statements whenever necessary:


----------------------------------------------
 Try to avoid group by as well as distinct statements in SQL.
 User needs to rewrite the SQL statements whenever necessary.

3. Use Latest Features of PL/SQL:


---------------------------------
 Use of FORALL Statement
 Use of Bulk Collect.

4. Use of Correct Function Call: -> Function Based Index.

Select count(prod_id) from products;


In this statements to create the function based index for count(prod_id).
Create index index_prod on products(count(prod_id);

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 .

6. String Function Practices:

 Use In built String Functions


 Use multiple string functions rather than use PLSQL block.
 Oracle has defined set of pre defined optimized string functions like Replace,
Instr,
Substr, RPAD,LPAD etc.
 Use of REGEX function.
 If user wants to deal with regular expression make use of REGEX functions rather
than
writing whole code.

7. Use of AND OR Operator for PL SQL performance tuning:

Check whether the AND condition and or condition use in SQL statement. Try to avoid
OR
condition as it is full scanning the table.

8. Use of PLS and Binary Datatypes:

 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.

9. Use of Execute Immediate statement and Cursor Variables:

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.

10. Declaring the Size of Variable:


11. Use of Packages in spite of Subprograms:
12. Use of DBMS_PROFILER for PL SQL performance tuning:
13. Use of Different hints in SQL statements

You might also like