Escolar Documentos
Profissional Documentos
Cultura Documentos
Confidential
TCS Confidential
Overview
Performance is a continuing process as far as database applications are concerned. Performance
comes into picture at various stages of software development lifecycle right from Requirement
Analysis through Design, Development, Testing, Deployment to Maintenance. It is with this aim
we have drafted this document. This document will come into use in the Development and
Deployment stages of all Oracle Database Applications. It aims at
Reducing time for Tuning before code rolls out into Production
Confidential
TCS Confidential
INDEX
Overview...........................................................................................................................................................2
INDEX.............................................................................................................................................................3
SQL Tuning Guidelines..................................................................................................................................4
Avoid datatype mismatch...........................................................................................................................4
Avoid functions on Indexed columns........................................................................................................4
Define initial columns in composite indexes.............................................................................................4
Using WHERE instead of HAVING.........................................................................................................5
Avoid nested queries...................................................................................................................................6
Using driving table in RULE based Optimizer........................................................................................6
Not exists and Outer joins.....................................................................................................................6
Do not use NOT EQUAL...........................................................................................................................6
Functional index for NVL columns...........................................................................................................6
Reduce the Number of Trips to the Database..........................................................................................7
Using TRUNCATE instead of DELETE..................................................................................................8
Advantages of using DECODE..................................................................................................................8
Counting rows from Table.........................................................................................................................9
Use UNION-ALL in Place of UNION (Where Possible).........................................................................9
Use WHERE Instead of ORDER BY Clause.........................................................................................10
Use IN in Place of OR...............................................................................................................................11
Full Table Scans are better?....................................................................................................................12
When to go for Fast Full Scans................................................................................................................12
Bitmap Indexes..........................................................................................................................................12
Using composite indexes...........................................................................................................................12
PL/SQL Tuning Guidelines..........................................................................................................................13
Use of bind variables................................................................................................................................13
Avoiding unnecessary database calls......................................................................................................13
Use of PLS_INTEGER.............................................................................................................................14
Avoid BINARY_INTEGER.....................................................................................................................14
Pinning stored procedures.......................................................................................................................14
Using WHEN clause in triggers...............................................................................................................14
Use AFTER row triggers..........................................................................................................................14
Use of Bulk binding..................................................................................................................................15
Use of LIMIT clause.................................................................................................................................15
PRO* C Tuning Guidelines..........................................................................................................................16
Confidential
TCS Confidential
Precompiler Options.................................................................................................................................16
Confidential
TCS Confidential
Ensure initial columns are defined when using a composite index. If the initial columns are not
used. Then the composite index will not be made use of.
For eg:
Consider a table in which brand and product columns have a composite index. And we
execute this query.
Select count(*)
From products
Where price < 1000;
The composite index will not be made use of. But the same composite index will be hit for
the query given below.
Select count(*)
From products
Where brand = Arrow;
Hence its important to that initial columns are defined while we are making use of
composite indexes. We can hit the index by doing the following.
Select count(*)
From products
Where brand > 0
and price < 1000;
Confidential
TCS Confidential
FROM
EMP
WHERE
EMP_NO = 0342;
SELECT
FROM
EMP
WHERE
EMP_NO = 0291;
FROM
EMP
C1 INTO , , ;
.
.
OPEN C1(291);
Confidential
TCS Confidential
C1 INTO , , ;
CLOSE C1;
END;
Method 3 (Most Efficient) :
SELECT
EMP
A,
WHERE
AND
A.EMP_NO = 0342
B.EMP_NO = 0291;
COUNT(*), SUM(SAL)
FROM
EMP
Confidential
TCS Confidential
SELECT
COUNT(*), SUM(SAL)
FROM
EMP
You can achieve the same result much more efficiently with DECODE:
SELECT COUNT(DECODE(DEPT_NO, 0020, X, NULL)) D0020_COUNT,
COUNT(DECODE(DEPT_NO, 0030, X, NULL)) D0030_COUNT,
SUM(DECODE(DEPT_NO, 0020, SAL, NULL)) D0020_SAL,
SUM(DECODE(DEPT_NO, 0030, SAL, NULL)) D0030_SAL
FROM
EMP
10
For example:
Least Efficient :
SELECT
ACCT_NUM, BALANCE_AMT
FROM
DEBIT_TRANSACTIONS
WHERE
TRAN_DATE = 31-DEC-95
UNION
SELECT
ACCT_NUM, BALANCE_AMT
FROM
CREDIT_TRANSACTIONS
WHERE
TRAN_DATE = 31-DEC-95
Most Efficient :
SELECT
ACCT_NUM, BALANCE_AMT
FROM
DEBIT_TRANSACTIONS
WHERE
TRAN_DATE = 31-DEC-95
UNION ALL
SELECT
ACCT_NUM, BALANCE_AMT
FROM
CREDIT_TRANSACTIONS
WHERE
TRAN_DATE = 31-DEC-95
All of the columns that make up the ORDER BY clause must be contained within a single
index in the same sequence.
All of the columns that make up the ORDER BY clause must be defined as NOT NULL within
the table definition. Remember, null values are not contained within an index.
WHERE clause indexes and ORDER BY indexes cannot be used in parallel.
For example:
Consider a table DEPT with the following fields:
Confidential
TCS Confidential
PK
NOT NULL
DEPT_DESC
NOT NULL
DEPT_TYPE
NULL
DEPT_CODE
FROM
DEPT
ORDER BY
DEPT_TYPE
Explain Plan:
SORT ORDER BY
TABLE ACCESS FULL
Most Efficient : (Here, index will be used)
SELECT
DEPT_CODE
FROM
DEPT
WHERE
DEPT_TYPE > 0
Explain Plan:
TABLE ACCESS BY ROWID ON EMP
INDEX RANGE SCAN ON DEPT_IDX
Use IN in Place of OR
The following query can be replaced to improve the performance as shown below:
Least Efficient:
SELECT . . .
FROM
LOCATION
WHERE
LOC_ID = 10
OR
LOC_ID = 20
Confidential
TCS Confidential
11
12
LOC_ID = 30
Most Efficient:
SELECT . . .
FROM
LOCATION
Bitmap Indexes
Consider bitmap indexes when where clause predicate contain low-cardinality columns,
contain logical operations such as OR, AND or NOT on those columns. It is advisable that bitmap
indexes are not used in OLTP applications.
Confidential
TCS Confidential
13
14
begin
Select sysdate into from_date from dual;
End;
Can be replaced by
Declare
From_date
begin
From_date:=sysdate
End;
Use of PLS_INTEGER
Make use of PLS_INTEGER instead of NUMBER or INTEGER in loop variables as
PLS_INTEGER makes use of machine arithmetic whereas NUMBER and INTEGER makes use
of functional arithmetic. Hence PLS_INTEGER will be faster.
Avoid BINARY_INTEGER
The use of BINARY_INTEGER datatypes within arithmetic operations performs slower than the
same operation using an INTEGER datatype.
Confidential
TCS Confidential
15
Use AFTER row triggers instead of BEFORE row triggers if at all possible. After row triggers are
faster.
16
bulk_limit := 15;
fetch c bulk collect into n LIMIT bulk_limit;
for i in n.first..n.last loop
dbms_output.put_line(n(i));
end loop;
close c;
end;
/
MAXOPENCURSORS (default value 10) signifies the initial size of the program cursor cache.
The size of the cursor cache may grow, depending on the values of HOLD_CURSOR and
RELEASE_CURSOR. Hence increase the MAXOPENCURSORS value in pre-compiling options.
Confidential
TCS Confidential
Confidential
TCS Confidential
17