beacon ora perf class 2

45
2007-12-25 1 Beacon Infotech Corporation www.oracleact.com Oracle Performance Tuning Tamilselvan G Beacon Infotech Corporation Class - 2

Upload: tuancoi

Post on 10-Sep-2015

227 views

Category:

Documents


0 download

DESCRIPTION

ora-perf-class-2.pdf

TRANSCRIPT

  • 2007-12-251

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning

    Tamilselvan G

    B

    e

    a

    c

    o

    n

    I

    n

    f

    o

    t

    e

    c

    h

    C

    o

    r

    p

    o

    r

    a

    t

    i

    o

    n

    C

    l

    a

    s

    s

    -

    2

  • 2007-12-252

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning

    Tuning Joins and Sub-Queries

    1. Nested Loops Join

    2. Sort Merge Join

    3. Hash Join

    4. Hash Join Outer

    5. Cartesian Join

    6. Outer Join

    7. Hierarchical (Self) Join

    8. Sub Query

    9. Semi Join

    10. Anti Join

  • 2007-12-253

    Beacon Infotech Corporation

    www.oracleact.com

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

    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)

    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.

    Oracle Performance Tuning 1. Nested Loops Join

  • 2007-12-254

    Beacon Infotech Corporation

    www.oracleact.com

    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. Output the first row as fast as possible

    2. No temporary space is needed in most of the cases, unless a aggregate function

    is used

    3. FIRST_ROW or RULE hint always favors NL Join

    4. 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.

    Oracle Performance Tuning 1.Nested Loops Join

  • 2007-12-255

    Beacon Infotech Corporation

    www.oracleact.com

    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)

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

  • 2007-12-256

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 2.Sort Merge Join

    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 cpu elapsed disk query current rows

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

    Parse 1 0.05 0.04 0 0 0 0

    Execute 1 0.00 0.00 0 0 0 0

    Fetch 4 0.02 0.01 0 8 0 45

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

    total 6 0.07 0.06 0 8 0 45

    Sort-Merge Join is used:

    1. When Oracle sees IN-

    EQUIVALITY Operator in the

    WHERE Clause.

    2. When USE_MERGE hint is added

    in the Query.

  • 2007-12-257

    Beacon Infotech Corporation

    www.oracleact.com

    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. No driving table concept in Sort-Merge Join

    2. Each data set is sorted prior to merge.

    3. The output rows are NOT sorted even though individual data

    set is sorted.

    4. You need to specify the ORDER BY clause if you want sorted

    order output.

  • 2007-12-258

    Beacon Infotech Corporation

    www.oracleact.com

    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.

  • 2007-12-259

    Beacon Infotech Corporation

    www.oracleact.com

    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 cpu elapsed disk query current rows

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

    Parse 1 0.01 0.01 0 0 0 0

    Execute 1 0.00 0.00 0 0 0 0

    Fetch 2 31.33 30.60 0 1,004,659 0 1

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

    total 4 31.34 30.61 0 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)

  • 2007-12-2510

    Beacon Infotech Corporation

    www.oracleact.com

    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 INDEX FAST FULL SCAN CUSTOMER_IDX (cr=44 pr=0 pw=0 time=20331 us)(object id 66385)

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

  • 2007-12-2511

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 3. Hash Join

    5.51 times faster

    run time

    5.5530.61

    Run Time

    in Seconds

    Consumed 213

    times less LIO

    4,7011,004,659# of LIO

    Hash Join

    Performance

    Improvement

    Hash Join

    Nested

    Loops

    Join

    Comparison

    How did it happen?

  • 2007-12-2512

    Beacon Infotech Corporation

    www.oracleact.com

    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.

  • 2007-12-2513

    Beacon Infotech Corporation

    www.oracleact.com

    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

    2 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.

  • 2007-12-2514

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 3. Hash Join

    Monitoring

    SQL> select * from v$sysstat where name like 'workarea exec%'

    STATISTIC# NAME CLASS VALUE STAT_ID

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

    294 workarea executions - optimal 64 7423 3211650785

    295 workarea executions - onepass 64 5 798730793

    296 workarea executions - multipass 64 0 3804491469

  • 2007-12-2515

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 3. Hash Join .

    V$SQL_WORKAREA in 10g

    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

    V$SQL_WORKAREA_ACTIVE

    Name Null? Type

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

    SQL_HASH_VALUE NUMBER

    SQL_ID VARCHAR2(13)

    WORKAREA_ADDRESS RAW(8)

    OPERATION_TYPE VARCHAR2(20)

    OPERATION_ID NUMBER

    POLICY VARCHAR2(6)

    SID NUMBER

    QCINST_ID NUMBER

    QCSID NUMBER

    ACTIVE_TIME NUMBER

    WORK_AREA_SIZE NUMBER

    EXPECTED_SIZE NUMBER

    ACTUAL_MEM_USED NUMBER

    MAX_MEM_USED NUMBER

    NUMBER_PASSES NUMBER

    TEMPSEG_SIZE NUMBER

    TABLESPACE VARCHAR2(31)

    SEGRFNO# NUMBER

    SEGBLK# NUMBER

  • 2007-12-2516

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 4. Hash Join Outer

    SQL> select * from supp ;

    SUPP_ NAME ST

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

    1001 ABC Inc CA

    1002 Microsoft NC

    1003 Oracle Corp CA

    1004 IBM Corp MA

    SQL> select * from state;

    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.

  • 2007-12-2517

    Beacon Infotech Corporation

    www.oracleact.com

    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

  • 2007-12-2518

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 4. Hash Join Outer.

    SQL> SELECT B.NAME SUPPLIER,

    A.STATEID STATEID,

    A.NAME STATE

    FROM STATE A , SUPP B

    WHERE B.STATEID(+) = A.STATEID ;

    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

    10 rows selected.

    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.

    In our example, STATE table is hashed, and the

    SUPP table is used to probe the hash table.

    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.

    There is NO WAY a outer joined table

    can be hashed.

  • 2007-12-2519

    Beacon Infotech Corporation

    www.oracleact.com

    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

    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.

  • 2007-12-2520

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 5. Cartesian Join

    SQL> SELECT * FROM EMP ;

    EMPNO ENAME SALARY DEPTNO

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

    1001 Tamil 900 10

    1002 Tandon 1900 10

    1003 Muthu 3100 20

    1004 Ravi 4100 20

    1005 Veera 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 PCT

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

    1001 Tamil 900 7.09

    1002 Tandon 1900 14.96

    1003 Muthu 3100 24.41

    1004 Ravi 4100 32.28

    1005 Veera 2700 21.26

    We can change

    the Cartesian

    Product SQL into

    Analytical

    Function.

  • 2007-12-2521

    Beacon Infotech Corporation

    www.oracleact.com

    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 DEPTNO

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

    1001 Tamil 1000 10

    1002 Tandon 2000 10

    1003 Muthu 3000 20

    1004 Ravi 4000 20

    SQL> SELECT * FROM DEPT;

    DEPTNO DEPTNAME

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

    10 Sales

    20 Marketing

    30 Accounts

    SQL> SELECT A.ENAME, B.DEPTNO,

    B.DEPTNAME

    FROM EMP A, DEPT B

    WHERE B.DEPTNO = A.DEPTNO(+) ;

    ENAME DEPTNO DEPTNAME

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

    Tamil 10 Sales

    Tandon 10 Sales

    Muthu 20 Marketing

    Ravi 20 Marketing

    30 Accounts -----

  • 2007-12-2522

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 6.Outer Join..

    Types of Outer Joins: 1 Right Outer Join, 2 Left Outer Join, 3 Full 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 DEPTNO DEPTNAME

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

    Tamil 10 Sales

    Tandon 10 Sales

    Muthu 20 Marketing

    Ravi 20 Marketing

    30 Accounts

    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) ;

    ENAME DEPTNO DEPTNAME

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

    Tamil 10 Sales

    Tandon 10 Sales

    Muthu 20 Marketing

    Ravi 20 Marketing

    30 Accounts

    Right Outer Join

  • 2007-12-2523

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 6.Outer Join..

    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 Marketing

    Veera

    ANSI SQL

    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

    Left Outer Join

  • 2007-12-2524

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 6.Outer Join..

    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 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> /

    ENAME DEPTNO DEPTNAME

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

    Muthu 20 Marketing

    Ravi 20 Marketing

    Tamil 10 Sales

    Tandon 10 Sales

    Veera

    30 Accounts

    6 rows selected.

    ANSI 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 DEPTNO DEPTNAME

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

    Tamil 10 Sales

    Tandon 10 Sales

    Muthu 20 Marketing

    Ravi 20 Marketing

    Veera

    30 Accounts

    6 rows selected.

    FULL Outer Join

  • 2007-12-2525

    Beacon Infotech Corporation

    www.oracleact.com

    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?

  • 2007-12-2526

    Beacon Infotech Corporation

    www.oracleact.com

    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")

  • 2007-12-2527

    Beacon Infotech Corporation

    www.oracleact.com

    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 ---------- CEO

    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

    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.

  • 2007-12-2528

    Beacon Infotech Corporation

    www.oracleact.com

    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.

  • 2007-12-2529

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 7. Hierarchical Join

    Pseudo columns

    1 CONNECT_BY_ISCYCLE

    2 CONNECT_BY_ISLEAF

    3 LEVEL

    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 IsLeaf LEVEL Path

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

    Scott 0 1 /Scott

    Tom 0 2 /Scott/Tom

    Kumar 1 3 /Scott/Tom/Kumar

    Veera 0 2 /Scott/Veera

    Selvan 1 3 /Scott/Veera/Selvan

    SQL> SELECT * FROM EMP ;

    EMPID NAME MGRID

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

    1001 Scott

    1002 Tom 1001

    1003 Kumar 1002

    1004 Veera 1001

    1005 Selvan 1004

    These pseudo columns are unique

    features of Oracle only, You will not

    find them in any other RDBMS.

  • 2007-12-2530

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 8. Sub Query

    Simple Sub Query - Uncorrelated Sub Query

    SQL> SELECT * FROM EMP ;

    EMPNO ENAME SALARY DEPTNO BONUS

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

    1001 Tamil 900 10

    1002 Tandon 1900 10

    1003 Muthu 3100 20

    1004 Ravi 4100 20

    1005 Veera 2700

    SQL> SELECT * FROM EMP A

    WHERE A.SALARY = ( SELECT MAX(SALARY) -------- Sub Query is executed one time

    FROM EMP_TEMP B ) ;

    EMPNO ENAME SALARY DEPTNO BONUS

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

    1004 Ravi 4100 20

    Rows Row Source Operation

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

    1 TABLE ACCESS FULL EMP

    1 SORT AGGREGATE

    5 TABLE ACCESS FULL EMP_TEMP

  • 2007-12-2531

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 8. Sub Query

    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

    Correlated Sub Query Using EXISTS

    SELECT * FROM EMP A

    WHERE EXISTS

    ( SELECT NULL FROM EMP_TEMP B

    WHERE A.EMPNO = B.EMPNO )

    Rows Row Source Operation

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

    5 HASH JOIN SEMI

    5 TABLE ACCESS FULL EMP

    5 TABLE ACCESS FULL EMP_TEMP

  • 2007-12-2532

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 8. Sub Query

    IN Sub Query

    SELECT * FROM EMP A

    WHERE EMPNO

    IN

    ( SELECT EMPNO FROM EMP_TEMP B)

    Rows Row Source Operation

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

    5 HASH JOIN SEMI

    5 TABLE ACCESS FULL EMP

    5 TABLE ACCESS FULL EMP_TEMP

    IN Sub Query is executed

    only once, while EXISTS

    sub query is executed once

    per row of the parent

    Query.

    Where ever possible, allow the sub query to be executed

    by a direct index lookup without a table access.

  • 2007-12-2533

    Beacon Infotech Corporation

    www.oracleact.com

    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.

  • 2007-12-2534

    Beacon Infotech Corporation

    www.oracleact.com

    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 | | 1 | 41 | 268 (6)| 00:00:03 |

    |* 1 | HASH JOIN RIGHT SEMI | | 1 | 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

  • 2007-12-2535

    Beacon Infotech Corporation

    www.oracleact.com

    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 |

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

  • 2007-12-2536

    Beacon Infotech Corporation

    www.oracleact.com

    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 cpu elapsed disk query current rows

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

    Parse 1 0.04 0.06 0 0 0 0

    Execute 1 0.00 0.00 0 0 0 0

    Fetch 76 0.48 0.45 1945 2018 0 15000

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

    total 78 0.52 0.52 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.

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

  • 2007-12-2537

    Beacon Infotech Corporation

    www.oracleact.com

    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 rows

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

    Parse 1 0.05 0.06 0 0 0 0

    Execute 1 0.00 0.00 0 0 0 0

    Fetch 76 0.48 0.45 1945 2018 0 15000

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

    total 78 0.53 0.51 1945 2018 0 15000

    Rows Row Source Operation in 10g

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

    15000 HASH JOIN RIGHT ANTI (cr=2036 pr=1945 pw=0 time=296890 us)

    5000 TABLE ACCESS FULL SUPPLIER (cr=388 pr=386 pw=0 time=68124 us)

    20000 TABLE ACCESS FULL CUSTOMER (cr=1630 pr=1559 pw=0 time=1522620 us)

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

  • 2007-12-2538

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 10. Anti Join

    SQL> select count(*) from t1

    where not exists

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

    COUNT(*)

    ----------

    3

    SQL> select count(*) from t1

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

    COUNT(*)

    ----------

    0

    Because there is a NULL value in a row in

    T2 table.

    SQL> select * from t2;

    ID NAME

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

    5 ABC

    6 ABC

    Tom

    SQL> select * from t1;

    ID

    ----------

    1

    5

    7

    9

    NOT IN and NOT EXISTS are not the same.

  • 2007-12-2539

    Beacon Infotech Corporation

    www.oracleact.com

    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

    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-2540

    Beacon Infotech Corporation

    www.oracleact.com

    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

    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-2541

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning 10. Anti Join

    ANTI JOIN Using HASH_AJ Hint

    ALTER SESSION SET ALWAYS_ANTI_JOIN = HASH ;

    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..

    To take advantage of anti

    join optimization, the

    following must be true:

    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-2542

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning

    Sub Query Categorization

    NOT IN sub query is not same as NOT EXISTS, but can be transformed

    into ANTI-joins.

    NOT IN / NOT

    EXISTS

    6

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

    transformed into semi joins.

    IN/EXISTS5

    Returns a single row. It can become a driving table. Single-row4

    If a simple sub query contains some aggregation, then there are some

    restrictions on how the optimizer may be able to transform them.

    Aggregate3

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

    many tables.

    Simple/Complex2

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

    Correlated sub queries can often be transformed into joins; non-

    correlated sub queries have some chance of becoming driving sub

    queries.

    Correlated /

    Noncorrelated

    1

    CharacteristicsCategory#

  • 2007-12-2543

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning

    Parameters in various Oracle releases affecting sub query

    Always use this for semi join

    (EXISTS)

    ChooseChoo

    se

    Stand

    ard

    _ALWAYS_SEMI_JOIN6

    Always use this method for anti-

    join (not exists)

    Choose Choo

    se

    NL_ALWAYS_ANTI_JOIN5

    Enables costing of equiality semi

    joins

    TrueTruen/a_COST_EQUALITY_SEMI_JOIN4

    Enables ordered semi-join

    (exists) sub query

    TrueTrueTrue_ORDERED_SEMI_JOIN3

    Enables unnesting of correlated

    sub query

    trueTruefalse_UNNEST_SUBQUERY2

    Unnests not exists sub query

    with one or more tables if

    possible

    n/aSingl

    e

    n/a_UNNEST_NOT_EXISTS_SQ1

    Description10g9i8iName#

  • 2007-12-2544

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning

    Parameters in various Oracle releases affecting sub query

    Enables removal of subsumed

    aggregate sub queries.

    Truen/an/a_REMOVE_AGGR_SUBQUERY11

    Enables right outer hash joinTruen/an/a_RIGHT_OUTER_HASH_ENABLE10

    Enables unnesting of IN Sub query

    into SELECT DISTINCT view

    Falsen/an/a_DISTINCT_VIEW_UNNESTING9

    Enables unnesting of sub queries in

    a bottom up manner

    Truen/an/a_OPTIMIZER_SQU_BOTTOMUP8

    Forces correct computation of sub

    query selectivity

    Truen/an/a_OPTIMIZER_CORRECT_SQ_SEL

    ECTIVITY

    7

    Description10g9i8iName#

  • 2007-12-2545

    Beacon Infotech Corporation

    www.oracleact.com

    Oracle Performance Tuning

    I value your questions, comments and suggestions.

    Please send your comment to [email protected].

    Beacon Infotech Corporation.

    Web: www.oracleact.com