DBAsupport.com Forums - Powered by vBulletin
Results 1 to 8 of 8

Thread: Execution Plan

  1. #1
    Join Date
    Sep 2005
    Posts
    278

    Execution Plan

    These tables are from sh schema.

    I erase the SQL statments which select the execution plan from plan_table.

    1 explain plan for
    2 select c.CUST_LAST_NAME
    3 from customers c, sales s, products p
    4 where c.cust_id = s.cust_id and
    5* p.prod_id = s.prod_id
    SQL> /

    Explained.

    rows plan
    ---------- ------------------------------------
    1016271 SELECT STATEMENT
    1016271 NESTED LOOPS
    1016271 NESTED LOOPS
    PARTITION RANGE ALL
    1016271 TABLE ACCESS FULL SALES
    1 INDEX UNIQUE SCAN PRODUCTS_PK
    1 INDEX UNIQUE SCAN CUSTOMERS_PK

    7 rows selected.

    After sometime i run the same query it gives the following result, why this kind of instability in chosing the execution plan by rdbms??

    1 explain plan for
    2 select c.CUST_LAST_NAME
    3 from customers c, sales s, products p
    4 where c.cust_id = s.cust_id and
    5* p.prod_id = s.prod_id
    SQL> /

    Explained.

    rows plan
    ---------- -----------------------------------
    1016271 SELECT STATEMENT
    1016271 NESTED LOOPS
    1016271 MERGE JOIN
    50000 SORT JOIN
    50000 TABLE ACCESS FULL CUSTOMERS
    1016271 SORT JOIN
    PARTITION RANGE ALL
    1016271 TABLE ACCESS FULL SALES
    1 INDEX UNIQUE SCAN PRODUCTS_PK

    9 rows selected.

    1 explain plan for
    2 select /*+ USE_HASH(c s)*/ c.CUST_LAST_NAME
    3 from customers c, sales s, products p
    4 where c.cust_id = s.cust_id and
    5* p.prod_id = s.prod_id
    SQL> /

    Explained.

    rows plan
    ---------- -----------------------------------
    1016271 SELECT STATEMENT
    1016271 NESTED LOOPS
    1016271 HASH JOIN
    50000 TABLE ACCESS FULL CUSTOMERS
    PARTITION RANGE ALL
    1016271 TABLE ACCESS FULL SALES
    1 INDEX UNIQUE SCAN PRODUCTS_PK

    7 rows selected.

    1 explain plan for
    2 select /*+ USE_NL(c s)*/ c.CUST_LAST_NAME
    3 from customers c, sales s, products p
    4 where c.cust_id = s.cust_id and
    5* p.prod_id = s.prod_id
    SQL> /

    Explained.

    rows plan
    ---------- --------------------------------------------
    1016271 SELECT STATEMENT
    1016271 NESTED LOOPS
    1016271 NESTED LOOPS
    PARTITION RANGE ALL
    1016271 TABLE ACCESS FULL SALES
    1 INDEX UNIQUE SCAN PRODUCTS_PK
    1 TABLE ACCESS BY INDEX ROWID CUSTOMERS
    1 INDEX UNIQUE SCAN CUSTOMERS_PK

    8 rows selected.

    After that i analyzed all the tables and indexes

    SQL> analyze table customers compute statistics;

    Table analyzed.

    SQL> analyze table sales compute statistics;

    Table analyzed.

    SQL> analyze table products compute statistics;

    Table analyzed.

    SQL> analyze index products_pk compute statistics;

    Index analyzed.

    SQL> analyze index customers_pk compute statistics;

    Index analyzed.

    and rerun the explain plan again

    1 explain plan for
    2 select c.CUST_LAST_NAME
    3 from customers c, sales s, products p
    4 where c.cust_id = s.cust_id and
    5* p.prod_id = s.prod_id
    SQL> /

    rows plan
    ---------- -------------------------------------
    1016271 SELECT STATEMENT
    1016271 NESTED LOOPS
    1016271 MERGE JOIN
    50000 SORT JOIN
    50000 TABLE ACCESS FULL CUSTOMERS
    1016271 SORT JOIN
    PARTITION RANGE ALL
    1016271 TABLE ACCESS FULL SALES
    1 INDEX UNIQUE SCAN PRODUCTS_PK


    my question is, after analyzing the tables and indexes oracle must chose the best plan but why it is not chosing the paln which uses Nested Loops, which are best from the above explain plans???

    Is it means hints are the best way to choose good execution plan and we may not need those expensive statistics gathering???

  2. #2
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    Why is the nested loop plan the best?
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

  3. #3
    Join Date
    Sep 2005
    Posts
    278
    Becoz the merge join performance additional steps, to sort the data

  4. #4
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    Which of the plans uses the most resources on execution?
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

  5. #5
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    by the way, try using ...
    select from table(dbms_xplan.display('plan_table',null,'TYPICAL'))

    ... to get the execution plans, It's a better formatting IMHO
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

  6. #6
    Join Date
    Sep 2005
    Posts
    278
    anyhow, Which is the best guide for tuning the SQL statements and database??
    Last edited by tabreaz; 09-10-2005 at 11:51 AM.

  7. #7
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    ==
    Which is the best guide for tuning the SQL statements and database??
    ==

    Use TKPROF.

    Tamil

  8. #8
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    In your example NESTED LOOP is the worse plan you can get.
    Imagine SALES has 1 million rows, nested loop that!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width