Tải bản đầy đủ (.ppt) (51 trang)

oracle slides05 fp2005 ver 1.0

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (240.84 KB, 51 trang )

PL/SQL Performance tuning
Concurrency & Recovery
Data transfer utilities
Oracle Day 5
2
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Objectives

To understand PL/SQL performance tuning

To understand few tools like DBMS_TRACE used to tune the PL/SQL code.

To understand the locking mechanism and types of locking in Oracle.

To understand the read consistency model.

To understand the concurrency schemes followed by Oracle.

To understand different kinds of database failures and procedure to recover
from them.

To understand data transfer utilities
PL/SQL Performance tuning
4
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0


Tuning PL/SQL Applications

Usually developer efforts are concentrated on tuning the SQL’s used by the
PL/SQL program for performance improvement.

Being a procedural language, there can be situations where excessive CPU
usage arises out of the code in Pl/SQL even though there are no database
accesses.

By tuning the applications, one can make sure they continue to deliver the
required response time and throughput.
5
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Reasons for PL/SQL Performance Problems

Badly written SQL statements

Poor programming practices

Misuse of shared memory.
6
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Solution to Badly written SQL statements



Analyze the execution plans and performance using EXPLAIN PLAN
statement.

Rewrite the SQL statements.

For more info on SQL statements, please refer the RDBMS artifacts.
7
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Solutions to Poor programming practices

Here different areas of PL/SQL code are categorized and some of the basic
principles of optimizing are discussed:

DECLARE – what is required by the code

Hand crafting the Built-in Functions

Inefficient Conditional Control Statements

Check the LOOP Statements

Implicit Datatype Conversions

Inappropriate Declarations for Numeric Datatypes

Unnecessary NOT NULL Constraints

8
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
DECLARE – what is required by the code

After the completion of the code, search for un-used variables if any. Remove
them from the code.

Defer the execution till it required.
DECLARE
l_chr_var1 VARCHAR2(15) :=
takes_five_miunte(… );
….
BEGIN
IF criteria1 THEN
use_the_defined_variable(l_chr_var1);
ELSE
say 90% of the cases follows this
Normal_code;
END IF;
END;
DECLARE
….
BEGIN
IF criteria1 THEN
DECALRE
l_chr_var1 VARCHAR2(15) :=
takes_five_miunte(… );

BEGIN
use_the_defined_variable(l_chr_var1);
END;
ELSE
Normal_code;
END IF;
END;
9
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Hand crafting the Built-in Functions

Built-in functions are more efficient.

Do not hand code one’s own versions of built-in functions such as REPLACE,
TRANSLATE, SUBSTR, INSTR, RPAD, and LTRIM. .
10
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Inefficient Conditional Control Statements

Most probable condition must be placed initially.

Now, consider the following AND expression:
IF credit_ok(cust_id) AND (loan < 5000) THEN


END IF;
The Boolean function credit_ok is always called.

However, if one switch the operands of AND as follows
IF (loan < 5000) AND credit_ok(cust_id) THEN

END IF;
The function is called only when the expression loan < 5000 is true
11
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Check the LOOP statements

Minimize the number of iterations inside loop. As soon as the code does the
required job EXIT the loop.

Loop within a loop – One common place where there is possibility of
unnecessary code execution.

Make sure that there should not be statements inside the loop that can be
executed outside the loop.
12
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Implicit Datatype Conversions


Avoiding implicit conversions can improve performance.
DECLARE
vNum NUMBER;
BEGIN
vNum:=vNum+15; converted
vNum:=vNum+15.0; not converted

END;
DECLARE
vChar CHAR(5);
BEGIN
vChar := 25; converted
vChar := '25'; not converted

END;
13
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Inappropriate Declarations for Numeric Datatypes


When one need to declare an integer variable, use the datatype
PLS_INTEGER, which is the most efficient numeric type.
14
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0

Unnecessary NOT NULL Constraints


Using the NOT NULL constraint incurs a performance cost.
PROCEDURE calc_m IS
m NUMBER NOT NULL := 0;
a NUMBER;
b NUMBER;
BEGIN

m := a + b;

END;
PROCEDURE calc_m IS
m NUMBER; no constraint
a NUMBER;
b NUMBER;
BEGIN

m := a + b;
IF m IS NULL THEN
enforce constraint programmatically

END IF;
END;
15
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0

Solution to Misuse of shared memory


Sizing the shared memory pool correctly.

Make sure it is large enough to hold all frequently used packages but not so
large that memory is wasted.
16
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Identifying PL/SQL Performance problems

There are few tools/API provided by PL/SQL to identify the PL/SQL
performance problems:

DBMS_PROFILER

DBMS_TRACE

One need DBA-SYS access to use these tools/API
17
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
DBMS_PROFILER

The Profiler API is implemented as PL/SQL package DBMS_PROFILER.


Provides services for gathering and saving run-time statistics.

The information is stored in database tables, which one can query later.

For example, one can learn how much time was spent executing each PL/SQL
line and subprogram.
18
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
DBMS_TRACE

The Trace API is implemented as PL/SQL package DBMS_TRACE.

Provides services for tracing execution by subprogram or exception.

One can see the order in which subprograms get executed.

In a typical session, follow the following steps:

Optionally, select specific subprograms for trace data collection.

Start by calling the procedure set_plsql_trace in package DBMS_TRACE.

Run the application to be traced.

Stop by calling the procedure clear_plsql_trace.
Concurrency in Oracle

20
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Locking mechanism

The smallest unit that can be locked in oracle is the row

For each SQL statement oracle acquires the appropriate lock
automatically.

Programmers cannot acquire locks at the row level explicitly

The smallest unit at which programmers can explicitly acquire locks is
the table

Locks are released when transaction commits or rolls back

Oracle does not acquire locks for reading
21
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Types of locks

When Oracle acquires locks at the row level, it automatically
acquires locks at the table level also


There are 5 different modes of locking at the table level

row share

row exclusive

share row exclusive

share

exclusive
22
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
LOCK TABLE
LOCK TABLE
LOCK TABLE <tableName> IN <mode> MODE
23
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Locking Modes

EXCLUSIVE(X)

No other transaction can lock the table.


SHARE(S)

Allow queries but no updates

Other transactions can acquire S Lock on this table

Can allow SELECT FOR UPDATE & SELECT statements
24
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0

ROW EXCLUSIVE(RX)

Allow concurrent access for queries, but prevent Share locking
also by other transactions

Permit Insert, Update, Delete or Select by other transactions/ or
lock rows in same table by other transactions

Prevent Share or Exclusive locking of the table by other
transactions
Locking Modes
25
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0


ROW SHARE(=SHARE UPDATE)

Allow concurrent access, prevent Exclusive locking by other
transactions

SHARE ROW EXCLUSIVE(SRX)

Allow other users to look at table but prevent sharing and updates
by other transactions
Locking Modes

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×