Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Oracle Day 5
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
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.
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
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;
l_chr_var1 VARCHAR2(15) :=
takes_five_miunte(..); BEGIN use_the_defined_variable(l_chr_var1); END;
END IF;
END;
ELSE
--Normal_code; END IF; END;
Copyright 2005, Infosys Technologies Ltd
8
10
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.
11
...
END;
...
END;
12
13
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;
14
15
16
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.
17
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.
18
Concurrency in Oracle
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
20
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
21
LOCK TABLE
LOCK TABLE LOCK TABLE <tableName> IN <mode> MODE
22
Locking Modes
EXCLUSIVE(X)
SHARE(S)
Allow queries but no updates Other transactions can acquire S Lock on this table Can allow SELECT..FOR UPDATE & SELECT statements
23
Locking Modes
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
24
Locking Modes
transactions
Allow other users to look at table but prevent sharing and updates by other transactions
25
Locking
SQL Statement Select .... from table insert,delete,update Select .... for update of Lock table in rowshare mode Mode of table lock none RX RS RS Row lock none
X
X
RX
S X SRX
26
Lock Compatibility
SQL statement Table Lock Compatibility RS RX S SRX X SELECT..FROM None Y Y Y Y Y
INSERT..INTO
UPDATE SELECT..FOR..UPD SHARE MODE SRX MODE
RX
RX RS S SRX
Y
Y Y Y Y
Y
Y Y N N
N
N Y Y N
N
N N N N
N
N N N N
EXCLUSIVE MODE
27
28
Recovery in Oracle
30
Process recovery
Process recovery to be done when user/server process failed Failure detected by PMON which rolls back the transaction and release resources If process is a background process, generally instance cannot function correctly
31
Instance recovery
Need Required because DBWR writes data to disk only when buffer is nearly full Some data which is committed may not be present in database Database may contain uncommitted data also Performed by SMON when database is restarted Roll forward using Redo Log to record data not present in data files Rolls back transactions that did not commit/were explicitly rolled back Release locks held by transactions in process at time of failure
32
Media recovery
Initiated by DBA Complete Media recovery Restore backup data files Start recovery program using SQL *DBA Retrieve archived log files to roll forward and roll back using generated roll back segments
33
Objectives
Export utility Import utility SQL Loader
35
36
Controlled through by passing parameters: One may pass parameters when one export data from the database.
Type the following at command prompt
C:\>exp scott/tiger file=empdept.expdat tables=(EMP,DEPT) log=empdept.log Parameterfile controlled: One may use a parameter file where the parameters are stored.
Type the following at command prompt
37
Controlled through by passing parameters: One may pass parameters when one import data.
Type the following at command prompt
Parameterfile controlled: One may use a parameter file where the parameters are stored.
Type the following at command prompt
38
SQL * Loader
SQL*Loader is Oracles utility program for loading data into an Oracle table. SQL*Loader takes two input files a control file and a data file and loads the data into a single Oracle table.
39
Data
Oracle Database
Data
ec or d fo s n r l ot oa se di le ng ct ed
at th r ds ro or er ec e R aus c
Discard File
Log File
Bad File
40
The name and location of the input data file The format of the records in the input data file The name of the table or tables to be loaded The correspondence between the fields in the input record and the columns in the database tables being loaded Selection criteria defining which records from the input file contain data to be inserted into the destination database tables. The names and locations of the bad file and the discard file (Explained later)
41
The names of the control file, log file, bad file, discard file, and data file The values of several command-line parameters A detailed breakdown of the fields and datatypes in the data file that was loaded Error messages for records that cause errors Messages indicating when records have been discarded A summary of the load that includes the number of logical records read from the data file, the number of rows rejected because of errors, the number of rows discarded because of selection criteria, and the elapsed time of the load
42
43
44
For this example, we'll be loading a Unix module mark sheet (XLS) of a FP batch taken from perception server (examination server) into Oracle
45
46
47
One can see your loaded data with your favorite: SQL>Select * from marks;
48
Because of the special binary format, files which had been created by the EXPORT utility can only be read by IMPORT utility.
To load data from other systems into the database one have to use SQL*LOADER.
49
Summary
PL/SQL program can feel a performance hit because of so many reasons. We have to ensure the performance with best programming practices, SQL tuning and utilizing the memory in the right way. DBMS_PROFILER, DBMS_TRACE are two useful tools which are used by developers to isolate the performance related issues in a PL/SQL program. The default locking in Oracle is Row Exclusive. Programmers can acquire lock on table level as well. Read consistency model is used to read values from database. Database may fail because of process, instance or media failure. Different kinds of database failures are recovered by different proceduressome implicitly and some explicitly by DBA. To understand data transfer utilities
50
Thank You!
Copyright 2005, Infosys Technologies Ltd
51