Tuning Oracle PL/SQL: Code Optimization

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

Tuning Oracle PL/SQL

PL/SQL - Oracle's procedural extensions to the SQL language - can be used to implement
complex application or data retrieval logic which cannot easily be expressed in single
SQL statements. PL/SQL plays a big part in many applications - particularly client-server
applications where PL/SQL can be used to implement application logic within the
database server itself.
PL/SQL has some advantages over standard SQL:

It can be stored in compiled form within the database. This reduces the overhead
of preparing an SQL statement for execution (parsing).
It operates completely within the server environment, eliminating or reducing
client-server and network traffic.
It allows the programmer to fully specify the processing logic and to implement
data access paths which might not be possible using standard SQL.
Using PL/SQL you can write triggers, which fire automatically when database
changes are made, and user-defined functions which can be embedded in your
SQL code.

Having decided to implement some processing in PL/SQL, whether as a replacement for


standard SQL or to implement some complex application logic, we have some
opportunities for improving the performance of that PL/SQL:

The procedural constructs in PL/SQL (loops, branches, etc.) are subject to many
of the same optimization techniques appropriate for other procedural languages
Storing PL/SQL routines in the database as a stored program reduces the overhead
of parsing PL/SQL.
There are some PL/SQL specific techniques which can be used to improve
database access (for instance, explicit cursors, CURRENT OF CURSOR, PL/SQL
tables).

Code Optimization
Usually, we think of PL/SQL as a database access language and concentrate on
optimizing the SQL within the PL/SQL program. But as a procedural language, PL/SQL
is subject to many of the same principles of optimization as other languages. There are
circumstances in which PL/SQL itself - without any database accesses - can consume
CPU resources at an excessive rate. Some of the basic principles for optimizing PL/SQL
code (and indeed other languages) are:

Optimize or minimize loop processing.


Optimize conditional statements (such as IF).
Avoid recursion.

You might also like