Вы находитесь на странице: 1из 45

Oracle Performance Tuning

ss

Beacon Infotech Corporation


www.oracleact.com

Tamilselvan G
Be
In ac
Co f o t o n
rp ech
or
at
io
n

a
Cl

-2

2007-12-25

Oracle Performance Tuning


Tuning Joins and Sub-Queries

Beacon Infotech Corporation


www.oracleact.com

1.
2.
3.
4.
5.
6.
7.
8.

Nested Loops Join


Sort Merge Join
Hash Join
Hash Join Outer
Cartesian Join
Outer Join
Hierarchical (Self) Join
Sub Query

9.
10.

Semi Join
Anti Join

2007-12-25

Oracle Performance Tuning

1. Nested Loops Join

Nested loops join is the workhorse join of any RDBMS engine.


Oracle can work with 2 tables at the most at any time in a
join operation, no matter how many tables are used in the
from clause.
How it works : Oracle performs a search of the inner table for each row found in the
outer table, and join the image, then output the result.
SQL> select a.empid, a.bonus old_bonus, b.bonus new_bonus
from emp a, new_bonus b
where a.empid = b.empid ;
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=CHOOSE
1 0 NESTED LOOPS
2 1 TABLE ACCESS (FULL) OF 'NEW_BONUS'
3 1 TABLE ACCESS (BY INDEX ROWID) OF 'EMP'
4 3
INDEX (UNIQUE SCAN) OF 'EMP_UQ' (UNIQUE)
Beacon Infotech Corporation
www.oracleact.com

2007-12-25

Oracle Performance Tuning

1.Nested Loops Join

Pseudo code
For x in (select EMPID, BONUS from NEW_BONUS) loop
Look up the index EMP_UQ using x.EMPID,
if a matching KEY is found in EMP_UQ index, get the ROWID of EMP
Get the BONUS column from EMP for the ROWID
output the result
end if
End loop

Advantages of NL Join
1.
2.
3.
4.

Output the first row as fast as possible


No temporary space is needed in most of the cases, unless a aggregate function
is used
FIRST_ROW or RULE hint always favors NL Join
Uses Index scan if suitable index is available.

Disadvantages of NL Join
1. If the result set is very large, then performance is horrible.
2. Not suitable for DW system because of processing large data sets.
Beacon Infotech Corporation
www.oracleact.com

2007-12-25

Oracle Performance Tuning

1.Nested Loop Join

HINT Usage
SELECT /*+ LEADING(B) USE_NL(A) */
A.ID, A.NAME, B.DOB
FROM BIG_TABLE A, SMALL_TABLE B
WHERE A.ID = B.ID
Rows Row Source Operation
------- --------------------------------------------------20000 NESTED LOOPS (cr=44109 pr=3121 pw=0 time=1725511 us)
20000 TABLE ACCESS FULL SMALL_TABLE (cr=2774 pr=1540 pw=0 time=244921 us)
20000 TABLE ACCESS BY INDEX ROWID BIG_TABLE (cr=41335 pr=1581 pw=0 time=1405038 us)
20000 INDEX UNIQUE SCAN BIG_TABLE_IDX (cr=21335 pr=42 pw=0 time=637526 us)(object id 66371)

SELECT /*+ LEADING(A) USE_NL(B) */


A.NAME, A.ID, B.DOB
FROM BIG_TABLE A, SMALL_TABLE B
WHERE A.ID = B.ID
Rows Row Source Operation
------- --------------------------------------------------20000 NESTED LOOPS (cr=44110 pr=3122 pw=0 time=1771279 us)
20000 TABLE ACCESS FULL BIG_TABLE (cr=2775 pr=1541 pw=0 time=261823 us)
20000 TABLE ACCESS BY INDEX ROWID SMALL_TABLE (cr=41335 pr=1581 pw=0 time=1457905 us)
20000 INDEX UNIQUE SCAN SMALL_TABLE_IDX (cr=21335 pr=42 pw=0 time=661071 us)(object id 66373)

********************************************************************************

Beacon Infotech Corporation


www.oracleact.com

2007-12-25

Oracle Performance Tuning


select a.id, a.name, b.dob
from (select * from big_table
where rownum < 11
order by dbms_random.value) a,
(select * from small_table
where rownum < 11
order by dbms_random.value) b
where a.dob >= b.dob and
a.id <> b.id
call
count
-----------Parse
1
Execute 1
Fetch
4
-----------total
6

Beacon Infotech Corporation


www.oracleact.com

cpu elapsed
-------- ---------0.05
0.04
0.00
0.00
0.02
0.01
-------- ---------0.07
0.06

2.Sort Merge Join

Sort-Merge Join is used:


1.

When Oracle sees INEQUIVALITY Operator in the


WHERE Clause.

2.

When USE_MERGE hint is added


in the Query.

disk
query current rows
---------- ---------- ---------- ---------0
0
0
0
0
0
0
0
0
8
0
45
---------- ---------- ---------- ---------0
8
0
45

2007-12-25

Oracle Performance Tuning

2.Sort-Merge Join

Rows Row Source Operation


------- --------------------------------------------------45 MERGE JOIN (cr=8 pr=0 pw=0 time=20991 us)
10 SORT JOIN (cr=4 pr=0 pw=0 time=19593 us)
10 VIEW (cr=4 pr=0 pw=0 time=18898 us) -- Materialized
10 SORT ORDER BY (cr=4 pr=0 pw=0 time=18871 us)
10
COUNT STOPKEY (cr=4 pr=0 pw=0 time=491 us)
10
TABLE ACCESS FULL BIG_TABLE (cr=4 pr=0 pw=0 time=425 us)
45 FILTER (cr=4 pr=0 pw=0 time=1773 us)
55 SORT JOIN (cr=4 pr=0 pw=0 time=1633 us)
10 VIEW (cr=4 pr=0 pw=0 time=1257 us) -- Materialized
10
SORT ORDER BY (cr=4 pr=0 pw=0 time=1224 us)
10
COUNT STOPKEY (cr=4 pr=0 pw=0 time=426 us)
10
TABLE ACCESS FULL SMALL_TABLE (cr=4 pr=0 pw=0 time=387 us)
********************************************************************************

1.
2.
3.
4.

Beacon Infotech Corporation


www.oracleact.com

No driving table concept in Sort-Merge Join


Each data set is sorted prior to merge.
The output rows are NOT sorted even though individual data
set is sorted.
You need to specify the ORDER BY clause if you want sorted
order output.
7

2007-12-25

Oracle Performance Tuning

2.Sort-Merge Join

USE_MERGE HINT
SELECT /*+ USE_MERGE(A, B) */
COUNT(*), COUNT(A.ID), COUNT(B.DOB) , COUNT(A.NAME)
FROM BIG_TABLE A ,
SMALL_TABLE B
WHERE A.ID = B.ID
Rows Row Source Operation
------- --------------------------------------------------1 SORT AGGREGATE (cr=3087 pr=3081 pw=0 time=819538 us)
20000 MERGE JOIN (cr=3087 pr=3081 pw=0 time=811605 us)
20000 SORT JOIN (cr=1544 pr=1541 pw=0 time=322576 us)
20000 TABLE ACCESS FULL BIG_TABLE (cr=1544 pr=1541 pw=0 time=83240 us)
20000 SORT JOIN (cr=1543 pr=1540 pw=0 time=436453 us)
20000 TABLE ACCESS FULL SMALL_TABLE (cr=1543 pr=1540 pw=0 time=81751 us)
********************************************************************************
Note: The opposite of USE_MERGE is NO_USE_MERGE.
Event 10032 details about Sort, and event 10033 about Sort IO.
Beacon Infotech Corporation
www.oracleact.com

2007-12-25

Oracle Performance Tuning

3. Hash Join

Before we study Hash Join, examine the following query :


select /*+ RULE */
count(*) ---- To simulate NL Join, I used RULE hint
from sales a, customer b
where a.cust_id = b.cust_id

call
count
-----------Parse
1
Execute 1
Fetch
2
------- -----total
4

cpu elapsed disk


-------- ---------- ---------0.01
0.01
0
0.00
0.00
0
31.33
30.60
0
-------- ---------- ---------31.34
30.61
0

query
current
rows
---------------------------0
0
0
0
0
0
1,004,659
0
1
------------- ---------- ---------1,004,659
0
1

Rows Row Source Operation


------- --------------------------------------------------1 SORT AGGREGATE (cr=1004659 pr=0 pw=0 time=30602540 us)
1000000 NESTED LOOPS (cr=1004659 pr=0 pw=0 time=31000441 us)
1000000 TABLE ACCESS FULL SALES (cr=4657 pr=0 pw=0 time=5000343 us)
1000000

INDEX UNIQUE SCAN CUSTOMER_IDX (cr=1000002 pr=0 pw=0 time=22641908 us)(object id 66385)

Beacon Infotech Corporation


www.oracleact.com

2007-12-25

Oracle Performance Tuning

3. Hash Join ...

select count(*)
from sales a, customer b
where a.cust_id = b.cust_id
call
count cpu elapsed
disk
query current
rows
------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
1
0.04
0.04
0
0
0
0
Execute 1
0.00
0.00
0
0
0
0
Fetch
2
5.64
5.51
4655
4701
0
1
------- ------------- ---------- ---------- ---------- ---------- ---------total
4
5.68
5.55
4655
4701
0
1
Rows
Row Source Operation
--------------------------------------------------------1
SORT AGGREGATE (cr=4716 pr=4655 pw=0 time=5511398 us)
1000000 HASH JOIN (cr=4716 pr=4655 pw=0 time=7098567 us)
20000

1000000

INDEX FAST FULL SCAN CUSTOMER_IDX (cr=44 pr=0 pw=0 time=20331 us)(object id 66385)

TABLE ACCESS FULL SALES (cr=4657 pr=4655 pw=0 time=1004056 us)

Beacon Infotech Corporation


www.oracleact.com

10

2007-12-25

Oracle Performance Tuning

3. Hash Join

Comparison
Nested
Loops
Join
# of LIO

1,004,659

Run Time
30.61
in Seconds

Hash Join

Hash Join
Performance
Improvement

4,701

Consumed 213
times less LIO

5.55

5.51 times faster


run time

How did it happen?


Beacon Infotech Corporation
www.oracleact.com

11

2007-12-25

Oracle Performance Tuning

3. Hash Join

Let us study how Hash Join works:


Under optimal conditions, Oracle builds a hash table for the smaller of the two data
sets. The hash table is not a Hash Cluster. The join key CUST_ID in this case was
hashed by an index in the hash table. The hash table is an array and the row that went
with the hash key was stored in that Index entry. We can also call the index key as
bucket one bucket may have one or many rows. CUSTOMER_IDX is hashed in our
case.
Hash Join does not require latches, because the hash table in the memory is private,
no one can access this hash table. Once hashed the smaller table into memory, Oracle
full scans the larger table ( SALES in our case, other named used is PROBE TABLE),
then for each row Oracle hashes the CUST_ID, finds the matching index key (bucket
number) in the HASH table. If a match is found, then Oracle scans the rows with in the
bucket. If one or more rows match, then Oracle returns the joined image.
When hash table is not fit into memory, then the hash table is broken into smaller
chunks, and stored in the TEMP Tablespace.
Note: Tom says Hash Table is not a Hash Cluster, but its similar to Hash Table used in
Java or C++, where as Jonathan writes Hash Table is equivalent to a single Table
Hash Cluster.
Beacon Infotech Corporation
www.oracleact.com

12

2007-12-25

Oracle Performance Tuning

3. Hash Join

Why faster execution ?


1.

No latch is needed because the hash table is in private memory, hence cache
buffer chain latch is not acquired.

2.

The hash table itself acts like an index

3.

Full table scan on the larger data set if needed uses


DB_FILE_MULTIBLOCK_READ_COUNT parameter.

Where to use Hash Join:


1

When joining 2 big data sets

When joining one big data sets with smaller data sets

Categories in Hash Join


There are 3 categories of Workarea executions Optimal, Onepass and Multipass
which you can see in V$SYSSTAT.
Hash Join Trace
By enabling 10104 events, you can get a lot of details.

Beacon Infotech Corporation


www.oracleact.com

13

2007-12-25

Oracle Performance Tuning

3. Hash Join

Monitoring
SQL> select * from v$sysstat where name like 'workarea exec%'
STATISTIC# NAME
CLASS VALUE
--------------------------------------------- -----294
workarea executions - optimal
64
7423
295
workarea executions - onepass
64
5
296
workarea executions - multipass 64
0

Beacon Infotech Corporation


www.oracleact.com

14

STAT_ID
---------3211650785
798730793
3804491469

2007-12-25

Oracle Performance Tuning

3. Hash Join .

V$SQL_WORKAREA in 10g

V$SQL_WORKAREA_ACTIVE

Name
Null? Type
--------------------------------- -----------------ADDRESS
RAW(8)
HASH_VALUE
NUMBER
SQL_ID
VARCHAR2(13)
CHILD_NUMBER
NUMBER
WORKAREA_ADDRESS
RAW(8)
OPERATION_TYPE
VARCHAR2(20)
OPERATION_ID
NUMBER
POLICY
VARCHAR2(10)
ESTIMATED_OPTIMAL_SIZE
NUMBER
ESTIMATED_ONEPASS_SIZE
NUMBER
LAST_MEMORY_USED
NUMBER
LAST_EXECUTION
VARCHAR2(10)
LAST_DEGREE
NUMBER
TOTAL_EXECUTIONS
NUMBER
OPTIMAL_EXECUTIONS
NUMBER
ONEPASS_EXECUTIONS
NUMBER
MULTIPASSES_EXECUTIONS
NUMBER
ACTIVE_TIME
NUMBER
MAX_TEMPSEG_SIZE
NUMBER
LAST_TEMPSEG_SIZE
NUMBER

Name
-------------------------SQL_HASH_VALUE
SQL_ID
WORKAREA_ADDRESS
OPERATION_TYPE
OPERATION_ID
POLICY
SID
QCINST_ID
QCSID
ACTIVE_TIME
WORK_AREA_SIZE
EXPECTED_SIZE
ACTUAL_MEM_USED
MAX_MEM_USED
NUMBER_PASSES
TEMPSEG_SIZE
TABLESPACE
SEGRFNO#
SEGBLK#

Beacon Infotech Corporation


www.oracleact.com

15

Null? Type
-------- -----------------NUMBER
VARCHAR2(13)
RAW(8)
VARCHAR2(20)
NUMBER
VARCHAR2(6)
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
VARCHAR2(31)
NUMBER
NUMBER

2007-12-25

Oracle Performance Tuning

SQL> select * from state;

SQL> select * from supp ;


SUPP_ NAME
----- -----------------------------1001 ABC Inc
1002 Microsoft
1003 Oracle Corp
1004 IBM Corp

4. Hash Join Outer

ST
-CA
NC
CA
MA

ST NAME
-- -----------------------------GA Georgia
CA California
NY New York
AL Alabama
TX Texas
MA Massachusetts
SC South Carolina
NC North Carolina
AZ Arizona
9 rows selected.

Beacon Infotech Corporation


www.oracleact.com

16

2007-12-25

Oracle Performance Tuning

4. Hash Join Outer.

SQL> EXPLAIN PLAN FOR


SELECT B.NAME SUPPLIER, A.STATEID STATEID, A.NAME STATE
FROM STATE A , SUPP B
WHERE B.STATEID(+) = A.STATEID ;
Explained.
SQL> select * from table( dbms_xplan.display );
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost |
-------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |
| 9 | 333 |
5|
|* 1 | HASH JOIN OUTER
|
| 9 | 333 |
5|
| 2 | TABLE ACCESS FULL | STATE
| 9 | 144 |
2|
| 3 | TABLE ACCESS FULL | SUPP
| 4 | 84 |
2|
------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
--------------------------------------------------1 - access("B"."STATEID"(+)="A"."STATEID")
Note: cpu costing is off

Beacon Infotech Corporation


www.oracleact.com

17

2007-12-25

Oracle Performance Tuning


SQL> SELECT B.NAME SUPPLIER,
A.STATEID STATEID,
A.NAME STATE
FROM STATE A , SUPP B
WHERE B.STATEID(+) = A.STATEID ;

4. Hash Join Outer.

How Hash Join Outer Works:


Oracle document says the outer table (whose
rows are preserved) is used to build the
hash table, and the inner table is used to
probe the hash table.

SUPPLIER
ST STATE
------------------------- -- ----------------------------ABC Inc
CA California
Microsoft
NC North Carolina
Oracle Corp
CA California
IBM Corp
MA Massachusetts
AZ Arizona
NY New York
GA Georgia
TX Texas
SC South Carolina
AL Alabama

In our example, STATE table is hashed, and the


SUPP table is used to probe the hash table.

10 rows selected.

There is NO WAY a outer joined table


can be hashed.

Beacon Infotech Corporation


www.oracleact.com

Steps involved:
1.

STATE table is hashed

2.

Full Scan SUPP table for each row

3.

Using HASH function on STATEID from


both tables, rows are output and mark the
matched row in STATE table

4.

Finally, again full scan on STATE table and


output unmarked rows.

18

2007-12-25

Oracle Performance Tuning

5. Cartesian Join

In a Cartesian join, the product of two data sets are output.


SQL> SELECT STUDENT_ID ID,
STUDENT_NAME NAME
FROM STUDENT ;
ID NAME
---------- ---------101 Tamil
102 Ram
103 Tom
SQL> SELECT SUBJECT_ID,
SUBJECT_NAME
FROM SUBJECTS;
SUBJECT_ID SUBJECT_NAME
---------- -------------------21 English
22 Math
23 History
24 Computer Science
Beacon Infotech Corporation
www.oracleact.com

SQL> SELECT STUDENT_ID,


STUDENT_NAME NAME,
SUBJECT_NAME SUB_NAME
FROM SUBJECTS, STUDENT ;
STUDENT_ID NAME
SUB_NAME
---------- ---------- -------------------101 Tamil
English
101 Tamil
Math
101 Tamil
History
101 Tamil
Computer Science
102 Ram
English
102 Ram
Math
102 Ram
History
102 Ram
Computer Science
103 Tom
English
103 Tom
Math
103 Tom
History
103 Tom
Computer Science
12 rows selected.
19

2007-12-25

Oracle Performance Tuning

5. Cartesian Join

SQL> SELECT * FROM EMP ;


EMPNO
---------1001
1002
1003
1004
1005

ENAME
--------------Tamil
Tandon
Muthu
Ravi
Veera

SALARY DEPTNO
---------- ---------900
10
1900
10
3100
20
4100
20
2700
30

SQL> SELECT A.EMPNO, A.ENAME, SALARY ,


ROUND((SALARY*100/TOT),2) PCT
FROM EMP A ,
(SELECT SUM(SALARY) TOT FROM EMP) ;
EMPNO ENAME
SALARY
---------- --------------- ---------1001 Tamil
900
1002 Tandon
1900
1003 Muthu
3100
1004 Ravi
4100
1005Corporation
Veera
2700
Beacon Infotech
www.oracleact.com

PCT
---------7.09
14.96
24.41
32.28
21.26
20

We can change
the Cartesian
Product SQL into
Analytical
Function.
2007-12-25

Oracle Performance Tuning

6.Outer Join

What is a Outer Join?


A join condition using the outer join operator (+) with one or more columns of
one of the tables. Oracle returns all rows that meet the join condition. Oracle
also returns all rows from the table without the outer join operator for which
there are no matching rows in the table with the outer join operator.
SQL> SELECT * FROM EMP ;
EMPNO ENAME SALARY
------------------------1001
Tamil
1000
1002
Tandon
2000
1003
Muthu
3000
1004
Ravi
4000
SQL> SELECT * FROM DEPT;
DEPTNO DEPTNAME
-------------------------------10
Sales
20
Marketing
30
Accounts
Beacon Infotech Corporation
www.oracleact.com

DEPTNO
---------10
10
20
20

SQL> SELECT A.ENAME, B.DEPTNO,


B.DEPTNAME
FROM EMP A, DEPT B
WHERE B.DEPTNO = A.DEPTNO(+) ;
ENAME
--------------Tamil
Tandon
Muthu
Ravi

21

DEPTNO DEPTNAME
---------- ------------------10 Sales
10 Sales
20 Marketing
20 Marketing
30 Accounts -----

2007-12-25

Oracle Performance Tuning

6.Outer Join..

Types of Outer Joins: 1 Right Outer Join, 2 Left Outer Join, 3 Full Outer Join

Right Outer Join


SQL> SELECT A.ENAME,
NVL(A.DEPTNO,
B.DEPTNO) DEPTNO,
B.DEPTNAME
FROM EMP A, DEPT B
WHERE A.DEPTNO (+) = B.DEPTNO ;
ENAME
--------------Tamil
Tandon
Muthu
Ravi

Beacon Infotech Corporation


www.oracleact.com

ANSI SQL
SQL> SELECT A.ENAME,
NVL(A.DEPTNO, B.DEPTNO) DEPTNO,
B.DEPTNAME
FROM EMP A RIGHT OUTER JOIN DEPT B
ON (A.DEPTNO = B.DEPTNO) ;

DEPTNO DEPTNAME
--------- -----------10 Sales
10 Sales
20 Marketing
20 Marketing
30 Accounts

ENAME
--------------Tamil
Tandon
Muthu
Ravi

22

DEPTNO DEPTNAME
---------- -----------10
Sales
10
Sales
20
Marketing
20
Marketing
30
Accounts
2007-12-25

Oracle Performance Tuning

6.Outer Join..

Left Outer Join


ANSI SQL

SQL> SELECT A.ENAME,


2
NVL(A.DEPTNO, B.DEPTNO) DEPTNO,
3
B.DEPTNAME
4 FROM EMP A, DEPT B
5 WHERE A.DEPTNO = B.DEPTNO (+)
6 /

ENAME

DEPTNO DEPTNAME

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

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

Tamil

10

Sales

Tandon

10

Sales

Muthu

20

Marketing

Ravi

20

SQL> SELECT A.ENAME,


2
NVL(A.DEPTNO,B.DEPTNO) DEPTNO,
3
B.DEPTNAME
4 FROM EMP A
5
LEFT OUTER JOIN DEPT B
6
ON (A.DEPTNO = B.DEPTNO)
7 /
ENAME
DEPTNO DEPTNAME
--------------- ---------- -----------Tamil
10 Sales
Tandon
10 Sales
Muthu
20 Marketing
Ravi
20 Marketing
Veera

Marketing

Veera

Beacon Infotech Corporation


www.oracleact.com

23

2007-12-25

Oracle Performance Tuning

6.Outer Join..

FULL Outer Join


SQL> select a.ename,
2
nvl(a.deptno,b.deptno) deptno,

ANSI SQL

3
b.deptname
4 from emp a , dept b
5 where a.deptno = b.deptno(+)
6 union
7 select a.ename,
8
b.deptno deptno ,
9
b.deptname
10 from emp a, dept b
11* where a.deptno(+) = b.deptno
SQL> /

SQL> select a.ename,


2
nvl(a.deptno,b.deptno) deptno,
3
b.deptname
4 from emp a
5
full outer join dept b
6
on (a.deptno = b.deptno)
7 /
ENAME
--------------Tamil
Tandon
Muthu
Ravi
Veera

ENAME
DEPTNO DEPTNAME
--------------- ---------- -----------Muthu
20 Marketing
Ravi
Tamil
Tandon
Veera

20 Marketing
10 Sales
10 Sales

DEPTNO DEPTNAME
--------- -----------10 Sales
10 Sales
20 Marketing
20 Marketing
30 Accounts

30 Accounts
6 rows selected.
Beacon Infotech Corporation
www.oracleact.com

6 rows selected.
24

2007-12-25

Oracle Performance Tuning

6.Outer Join..

FULL Outer Join

In the previous query I used


UNION for the FULL OUTER
JOIN.

Can any one rewrite the query


with UNION ALL?

Beacon Infotech Corporation


www.oracleact.com

25

2007-12-25

Oracle Performance Tuning

6.Outer Join..

FULL Outer Join with ANSI Standard


SQL> explain plan for
2 select a.ename,
3
nvl(a.deptno,b.deptno) deptno,
4
b.deptname
5 from emp a
6
full outer join dept b
7
on (a.deptno = b.deptno)
8 /
Explained.

SQL> select * from table( dbms_xplan.display );


PLAN_TABLE_OUTPUT
----------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost |
---------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT
|
| 6 | 312 | 12 |
| 1 | VIEW
|
| 6 | 312 | 12 |
| 2 | UNION-ALL
|
|
|
|
|
| 3 | NESTED LOOPS OUTER
|
| 5 | 205 | 7 |
| 4 | TABLE ACCESS FULL
| EMP
| 5 | 110 | 2 |
|* 5 | TABLE ACCESS CLUSTER | DEPT
| 1 | 19 | 1 |
| 6 | NESTED LOOPS ANTI
|
| 1 | 32 | 5 |
| 7 | TABLE ACCESS FULL
| DEPT
| 3 | 57 | 2 |
|* 8 | TABLE ACCESS CLUSTER| EMP
| 5 | 65 | 1 |
--------------------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------5 - filter("A"."DEPTNO"="B"."DEPTNO"(+))
8 - filter("A"."DEPTNO"="B"."DEPTNO")

Beacon Infotech Corporation


www.oracleact.com

26

2007-12-25

Oracle Performance Tuning

7. Hierarchical Join

The Hierarchical query allows you to select rows in hierarchical order


Example:
SQL> SELECT * FROM EMP ORDER BY EMPID ;

EMPID LASTNAME DEPTID CUMSALARY


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

MGRID

----------

----------

----------

1001 Scott

10

10600

1002 Tom

10

2500

1001

--------

Tom reports to Scott

1003 Veer

10

700

1002

--------

Veer reports to Tom

1004 Ram

10

500

1002

1005 Ragu

10

3100

1001

---------- Ragu reports to Scott

1006 Bush

10

800

1005

---------- Bush reports to Ragu

1007 Huss

10

600

1005

1008 Gopi

10

400

1005

---------- CEO

8 rows selected. Note the CUMSALARY column. This column stores individual salary for those
employees who do not have subordinate, but stores cumulative salary for those employees who
have subordinate plus his salary.
Now our requirement is list all employees individual salary.

Beacon Infotech Corporation


www.oracleact.com

27

2007-12-25

Oracle Performance Tuning

7. Hierarchical Join

Now our requirement is list all employees individual salary.


SQL> SELECT EMPID, MGRID, CUMSALARY,
2
( CUMSALARY - (SELECT NVL(SUM(B.CUMSALARY),0) FROM EMP B
3
WHERE B.MGRID = A.EMPID
4
) ) IND_SAL
5 FROM EMP A
6 START WITH MGRID IS NULL
7 CONNECT BY PRIOR EMPID = MGRID ;
If the table is large, then create
separate indexes on EMPID
EMPID
MGRID CUMSALARY IND_SAL
and MGRID columns for
---------- ---------- ------------------performance improvement.
1001
10600
5000
1002
1001
2500
1300
1003
1002
700
700
1004
1002
500
500
1005
1001
3100
1300
1006
1005
800
800
1007
1005
600
600
1008
1005
400
400
8 rows selected.

Beacon Infotech Corporation


www.oracleact.com

28

2007-12-25

Oracle Performance Tuning

These pseudo columns are unique


features of Oracle only, You will not
find them in any other RDBMS.

Pseudo columns
1
2
3

CONNECT_BY_ISCYCLE
CONNECT_BY_ISLEAF
LEVEL

SQL> SELECT * FROM EMP ;

EMPID NAME

MGRID

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

----------

1001 Scott
1002 Tom

1001

1003 Kumar

1002

1004 Veera

1001

1005 Selvan

1004

Beacon Infotech Corporation


www.oracleact.com

7. Hierarchical Join

SQL> SELECT NAME, CONNECT_BY_ISLEAF "ISLEAF",


2
LEVEL,
3
SYS_CONNECT_BY_PATH(NAME,'/') "PATH"
4
FROM EMP
5
START WITH MGRID IS NULL
6* CONNECT BY PRIOR EMPID = MGRID
SQL> /
NAME
---------Scott
Tom
Kumar
Veera
Selvan

IsLeaf
LEVEL Path
---------- ---------- -------------------0
1
/Scott
0
2
/Scott/Tom
1
3
/Scott/Tom/Kumar
0
2
/Scott/Veera
1
3
/Scott/Veera/Selvan

29

2007-12-25

Oracle Performance Tuning

8. Sub Query

Simple Sub Query - Uncorrelated Sub Query


SQL> SELECT * FROM EMP ;
EMPNO ENAME
---------- --------------1001 Tamil
1002 Tandon
1003 Muthu
1004 Ravi
1005 Veera

SALARY DEPTNO
BONUS
---------- ---------- ---------900
10
1900
10
3100
20
4100
20
2700

SQL> SELECT * FROM EMP A


WHERE A.SALARY = ( SELECT MAX(SALARY)
FROM EMP_TEMP B ) ;
EMPNO ENAME
SALARY
---------- --------------- ---------1004 Ravi
4100

DEPTNO
---------20

-------- Sub Query is executed one time

BONUS
----------

Rows Row Source Operation


------- --------------------------------------------------1 TABLE ACCESS FULL EMP
1 SORT AGGREGATE
5 TABLE ACCESS FULL EMP_TEMP
Beacon Infotech Corporation
www.oracleact.com

30

2007-12-25

Oracle Performance Tuning


Correlated Sub Query
Sub query is executed for every row
from the outer query.
SELECT * FROM EMP A
WHERE A.SALARY = ( SELECT SALARY
FROM EMP_TEMP B
WHERE A.EMPNO = B.EMPNO )
Rows Row Source Operation
------- --------------------------------------------------5 FILTER
5 TABLE ACCESS FULL EMP
5 TABLE ACCESS FULL EMP_TEMP

Beacon Infotech Corporation


www.oracleact.com

8. Sub Query

Correlated Sub Query Using EXISTS


SELECT * FROM EMP A
WHERE EXISTS
( SELECT NULL FROM EMP_TEMP B
WHERE A.EMPNO = B.EMPNO )
Rows
------5
5
5

31

Row Source Operation


--------------------------------------------------HASH JOIN SEMI
TABLE ACCESS FULL EMP
TABLE ACCESS FULL EMP_TEMP

2007-12-25

Oracle Performance Tuning

8. Sub Query

IN Sub Query
SELECT * FROM EMP A
WHERE EMPNO
IN
( SELECT EMPNO FROM EMP_TEMP B)
Rows
------5
5
5

IN Sub Query is executed


only once, while EXISTS
sub query is executed once
per row of the parent
Query.

Row Source Operation


--------------------------------------------------HASH JOIN SEMI
TABLE ACCESS FULL EMP
TABLE ACCESS FULL EMP_TEMP

Where ever possible, allow the sub query to be executed


by a direct index lookup without a table access.

Beacon Infotech Corporation


www.oracleact.com

32

2007-12-25

Oracle Performance Tuning

9. Semi Join

MERGE SEMI JOIN


SELECT NAME, DOJ FROM CUSTOMER C
WHERE EXISTS
( SELECT /*+ MERGE_SJ */ NULL --- To simulate Merge Join
FROM SUPPLIER S
WHERE S.NAME = C.NAME AND S.DOJ = C.DOJ) ;
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes |TempSpc| Cost (%CPU)| Time |
---------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |
| 1 | 41 |
| 414 (8)| 00:00:04 |
| 1 | MERGE JOIN SEMI
|
| 1 | 41 |
| 414 (8)| 00:00:04 |
| 2 | SORT JOIN
|
| 20000 | 507K| 1432K| 357 (7)| 00:00:03 |
| 3 | TABLE ACCESS FULL| CUSTOMER | 20000 | 507K|
| 212 (6)| 00:00:02 |
|* 4 | SORT UNIQUE
|
| 5000 | 75000 |
| 57 (11)| 00:00:01 |
| 5 | TABLE ACCESS FULL| SUPPLIER | 5000 | 75000 |
| 54 (6)| 00:00:01 |
------------------------------------------------------------------------------------------------------------------------------------

RUN TIME = 0.85 Seconds LIO = 1949


When NO suitable index is available on the table used in the Sub Query, then
Oracle may use SEMI JOIN to optimize the query.

Beacon Infotech Corporation


www.oracleact.com

33

2007-12-25

Oracle Performance Tuning

9. Semi Join

HASH JOIN RIGHT SEMI in 10g


SELECT NAME, DOJ FROM CUSTOMER C
WHERE EXISTS
( SELECT NULL
FROM SUPPLIER S
WHERE S.NAME = C.NAME AND S.DOJ = C.DOJ) ;
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------Plan hash value: 1924774493
--------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT

41 | 268 (6)| 00:00:03 |

|* 1 | HASH JOIN RIGHT SEMI |

41 | 268 (6)| 00:00:03 |

| 2 | TABLE ACCESS FULL

| SUPPLIER | 5000 | 75000 |

54 (6)| 00:00:01 |

| 3 | TABLE ACCESS FULL

| CUSTOMER | 20000 | 507K| 212 (6)| 00:00:02 |

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

Run Time 0.45 Seconds LIO = 2257


Beacon Infotech Corporation
www.oracleact.com

34

2007-12-25

Oracle Performance Tuning

9. Anti Join

Anti Join - An anti join is a query that returns rows in one table that
do not match some set of rows from another table.
NOT IN Sub Query
SELECT NAME, DOJ FROM CUSTOMER C
WHERE (NAME, DOJ) NOT IN
( SELECT NAME, DOJ FROM SUPPLIER S) ;
PLAN_TABLE_OUTPUT in 10g
-----------------------------------------------------------------------------------------------------------------------Plan hash value: 3490453085
-----------------------------------------------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |
| 50 | 1300 | 27791 (8)| 00:03:29 |
|* 1 | FILTER
|
|
|
|
|
|
| 2 | TABLE ACCESS FULL| CUSTOMER | 1000 | 26000 | 213 (6) | 00:00:02 |
|* 3 | TABLE ACCESS FULL| SUPPLIER | 13 | 195 | 55 (8) | 00:00:01 |
-------------------------------------------------------------------------------------------------------------------------

Beacon Infotech Corporation


www.oracleact.com

35

2007-12-25

Oracle Performance Tuning

10. Anti Join

Anti Join - NOT IN Sub Query


SELECT NAME, DOJ
FROM CUSTOMER C
WHERE (NAME, DOJ) NOT IN
( SELECT NAME, DOJ
FROM SUPPLIER S)
call
count
-----------Parse
1
Execute
1
Fetch
76
-----------total
78

cpu elapsed
-------- ---------0.04
0.06
0.00
0.00
0.48
0.45
------- ---------0.52
0.52

disk
query current
rows
---------- ---------- ---------- ---------0
0
0
0
0
0
0
0
1945
2018
0
15000
---------- ---------- ---------- ---------1945
2018
0
15000

Rows Row Source Operation in 10g


------- --------------------------------------------------15000 HASH JOIN RIGHT ANTI (cr=2036 pr=0 pw=0 time=229979 us)
5000 TABLE ACCESS FULL SUPPLIER (cr=388 pr=0 pw=0 time=15230 us)
20000 TABLE ACCESS FULL CUSTOMER (cr=1630 pr=0 pw=0 time=100280 us)
10g optimizer has an improved execution plan for NOT IN Sub Query.
********************************************************************************

Beacon Infotech Corporation


www.oracleact.com

36

2007-12-25

Oracle Performance Tuning

10. Anti Join

NOT EXISTS Query


SELECT NAME, DOJ
FROM CUSTOMER C
WHERE NOT EXISTS
( SELECT 'X' FROM SUPPLIER S
WHERE S.NAME = C.NAME
AND S.DOJ = C.DOJ )
call count
cpu elapsed
disk
query current
------------ -------- ------------------- ---------- ---------Parse
1
0.05
0.06
0
0
0
Execute 1
0.00
0.00
0
0
0
Fetch
76
0.48
0.45
1945
2018
0
------------ -------- ---------- ---------- --------- ---------total
78
0.53
0.51
1945
2018
0
Rows
------15000
5000
20000

rows
---------0
0
15000
---------15000

Row Source Operation in 10g


--------------------------------------------------HASH JOIN RIGHT ANTI (cr=2036 pr=1945 pw=0 time=296890 us)
TABLE ACCESS FULL SUPPLIER (cr=388 pr=386 pw=0 time=68124 us)
TABLE ACCESS FULL CUSTOMER (cr=1630 pr=1559 pw=0 time=1522620 us)

********************************************************************************
Beacon Infotech Corporation
www.oracleact.com

37

2007-12-25

Oracle Performance Tuning

10. Anti Join

NOT IN and NOT EXISTS are not the same.


SQL> select * from t1;

SQL> select * from t2;

ID
---------1
5
7
9

ID NAME
---------- ---------5 ABC
6 ABC
Tom

SQL> select count(*) from t1


2 where id not in (select id from t2) ;

SQL> select count(*) from t1


where not exists
(select 'x' from t2 where t2.id = t1.id ) ;

COUNT(*)
---------0
Because there is a NULL value in a row in
T2 table.
Beacon Infotech Corporation
www.oracleact.com

COUNT(*)
---------3
38

2007-12-25

Oracle Performance Tuning

10. Anti Join

ANTI JOIN Using OUTER JOIN


select H1.object_name, H1.object_id
from H1, H2
where H1.object_id = H2.object_id(+)
and H1.object_name = H2.OBJECT_NAME (+)
and H2.OBJECT_NAME IS NULL
call count
cpu elapsed
disk
query current
rows
------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
1
0.00
0.00
0
0
0
0
Execute
1
0.00
0.00
0
0
0
0
Fetch
22
0.31
0.28
0
617
0
4010
------------ -------- ---------- ---------- ---------- ---------- ---------total
24
0.31
0.28
0
617
0
4010
Rows Row Source Operation
------- --------------------------------------------------4010 FILTER
24010 MERGE JOIN OUTER
24010 SORT JOIN
24010 TABLE ACCESS FULL H1
20000 SORT JOIN
20000 TABLE ACCESS FULL H2

Beacon Infotech Corporation


www.oracleact.com

39

There is NO index on H2
table, but still the
elapsed time is 0.28
seconds. ANTI JOIN
using Outer Join is one
of the fastest way to get
the result set.

2007-12-25

Oracle Performance Tuning

10. Anti Join

ANTI JOIN Using MINUS


SELECT OBJECT_NAME, OBJECT_ID FROM H1
MINUS
SELECT OBJECT_NAME, OBJECT_ID FROM H2
call count
cpu elapsed
disk
query current
rows
------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
1
0.00
0.00
0
0
0
0
Execute
1
0.01
0.00
0
0
0
0
Fetch
269
0.26
0.22
0
617
0
4010
------- ------ -------- ---------- ---------- ---------- ---------- ---------total
271
0.27
0.22
0
617
0
4010
Rows Row Source Operation
------- --------------------------------------------------4010 MINUS
24010 SORT UNIQUE
24010 TABLE ACCESS FULL H1
20000 SORT UNIQUE
20000 TABLE ACCESS FULL H2

Beacon Infotech Corporation


www.oracleact.com

40

If the parent query does


not contain a column
from the inner query,
then consider using
MINUS operator.
Also, the data types
should match in both
tables.

2007-12-25

Oracle Performance Tuning

10. Anti Join

ANTI JOIN Using HASH_AJ Hint


ALTER SESSION SET ALWAYS_ANTI_JOIN = HASH ;

To take advantage of anti


join optimization, the
following must be true:

SELECT OBJECT_NAME, OBJECT_ID


FROM H1
WHERE (OBJECT_NAME, OBJECT_ID) NOT IN
(SELECT /*+ HASH_AJ */
OBJECT_NAME, OBJECT_ID
FROM H2)
call count
cpu elapsed
disk
query current
rows
------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
1
0.00
0.00
0
0
0
0
Execute
1
0.00
0.00
0
0
0
0
Fetch
269
0.10
0.09
0
617
0
4010
------- ------ -------- ---------- ---------- ---------- ---------- ---------total
271
0.10
0.10
0
617
0
4010
Rows Row Source Operation
------- --------------------------------------------------4010 HASH JOIN ANTI
24010 TABLE ACCESS FULL H1
20000 TABLE ACCESS FULL H2

Among the various tests, this is the BEST RUN TIME in 9i..
Beacon Infotech Corporation
www.oracleact.com

41

1.

CBO must be enabled

2.

Anti Join columns must


NOT be NULL. Or IS
NOT NULL clause
appears in the WHERE
clause.

3.

Sub Query is not


correlated

4.

Parent Query does not


contain OR clause.

5.

ALWAYS_ANTI_JOIN
parameter must be set
to HASH or MERGE.
2007-12-25

Oracle Performance Tuning


Sub Query Categorization
#

Category

Characteristics

Correlated /
Noncorrelated

A correlated sub query references columns from an outer query block.


Correlated sub queries can often be transformed into joins; noncorrelated sub queries have some chance of becoming driving sub
queries.

Simple/Complex

Simple sub query contain a single table. Complex sub queries contain
many tables.

Aggregate

If a simple sub query contains some aggregation, then there are some
restrictions on how the optimizer may be able to transform them.

Single-row

Returns a single row. It can become a driving table.

IN/EXISTS

IN sub query may be written as EXISTS sub query. These can be


transformed into semi joins.

NOT IN / NOT
EXISTS

NOT IN sub query is not same as NOT EXISTS, but can be transformed
into ANTI-joins.

Beacon Infotech Corporation


www.oracleact.com

42

2007-12-25

Oracle Performance Tuning


Parameters in various Oracle releases affecting sub query
#

Name

8i

9i

10g

Description

_UNNEST_NOT_EXISTS_SQ

n/a

Singl
e

n/a

Unnests not exists sub query


with one or more tables if
possible

_UNNEST_SUBQUERY

false

True

true

Enables unnesting of correlated


sub query

_ORDERED_SEMI_JOIN

True

True

True

Enables ordered semi-join


(exists) sub query

_COST_EQUALITY_SEMI_JOIN

n/a

True

True

Enables costing of equiality semi


joins

_ALWAYS_ANTI_JOIN

NL

Choo
se

Choose

Always use this method for antijoin (not exists)

_ALWAYS_SEMI_JOIN

Stand
ard

Choo
se

Choose

Always use this for semi join


(EXISTS)

Beacon Infotech Corporation


www.oracleact.com

43

2007-12-25

Oracle Performance Tuning


Parameters in various Oracle releases affecting sub query
#

Name

8i

9i

10g

Description

_OPTIMIZER_CORRECT_SQ_SEL
ECTIVITY

n/a

n/a

True

Forces correct computation of sub


query selectivity

_OPTIMIZER_SQU_BOTTOMUP

n/a

n/a

True

Enables unnesting of sub queries in


a bottom up manner

_DISTINCT_VIEW_UNNESTING

n/a

n/a

False

Enables unnesting of IN Sub query


into SELECT DISTINCT view

10

_RIGHT_OUTER_HASH_ENABLE

n/a

n/a

True

Enables right outer hash join

11

_REMOVE_AGGR_SUBQUERY

n/a

n/a

True

Enables removal of subsumed


aggregate sub queries.

Beacon Infotech Corporation


www.oracleact.com

44

2007-12-25

Oracle Performance Tuning

I value your questions, comments and suggestions.


Please send your comment to tamil_s_g@oracleact.com.
Beacon Infotech Corporation.
Web: www.oracleact.com

Beacon Infotech Corporation


www.oracleact.com

45

2007-12-25

Вам также может понравиться