Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Konopnicki –1997, Rev. MS-2004 1 Optimizing Join Statements To choose an execution plan for a join statement, the optimizer must choose: ä Access.

Similar presentations


Presentation on theme: "David Konopnicki –1997, Rev. MS-2004 1 Optimizing Join Statements To choose an execution plan for a join statement, the optimizer must choose: ä Access."— Presentation transcript:

1 David Konopnicki –1997, Rev. MS-2004 1 Optimizing Join Statements To choose an execution plan for a join statement, the optimizer must choose: ä Access Paths ä Join Operation ä Join Order These Choices are related. Lets examine the different possible join operations.

2 David Konopnicki –1997, Rev. MS-2004 2 Nested Loops Join SELECT * FROM emp,dept WHERE emp.deptno=dept.deptno 1 - NESTED LOOPS 2 - TABLE ACCESS (FULL) emp 3 - TABLE ACCESS (BY ROWID) dept 4 - INDEX (UNIQUE SCAN) pk_dept The outer table or the driving table The inner Table

3 David Konopnicki –1997, Rev. MS-2004 3 Nested Loops Join ä The cost of nested loops operation is: ä Access(O) + Access(I) * Rows(O) ä Is good only when the outer table drives the inner table and the subsets accessed are small. ä In outer joins the outer table is dictated by the query.

4 David Konopnicki –1997, Rev. MS-2004 4 Hash Join 1 - HASH JOIN 3 - HASH (JOIN) 2- Table Access (FULL SCAN) emp 4 - Table Access (FULL SCAN) dept

5 David Konopnicki –1997, Rev. MS-2004 5 Hash Join ä The cost of a hash join is: ä Access (O) + Access(I) + Hash (min(I,O)) ä Can be used in equijoins only ä Best if Hash(min(I,O) ) can be stored in memory, otherwise it is partitioned. ä Not available with RBO. ä In outer joins the outer table is dictated by the query.

6 David Konopnicki –1997, Rev. MS-2004 6 Sort-merge Join 1 - MERGE JOIN 2 - SORT (JOIN)4 - SORT (JOIN) 3- Table Access (FULL SCAN) emp 5 - Table Access (FULL SCAN) dept

7 David Konopnicki –1997, Rev. MS-2004 7 Sort Merge Join ä The cost of a sort-merge join is: ä Access (O) + Access(I) + Sort (O) + Sort (I) ä Can not be used in non-equijoins only. ä No distinction between outer/inner tables except when dictated by the query. ä Preferred on hash when: It is not an equijoin, or It is not an equijoin, or Sorts are already required in other parts of the query, or Sorts are already required in other parts of the query, or SORT_AREA_SIZE >> HASH_AREA_SIZE SORT_AREA_SIZE >> HASH_AREA_SIZE

8 David Konopnicki –1997, Rev. MS-2004 8 Cluster Join 1 - NESTED LOOPS TABLE ACCESS (FULL) dept TABLE ACCESS (CLUSTER) emp

9 David Konopnicki –1997, Rev. MS-2004 9 Nested Loops ä The cost of nested loops operation is: ä Access(O) + Access(I) * Rows(O) ä Is good only when the outer table drives the inner table ä In outer joins the outer table is dictated by the query ä The cost of a sort-merge join is: ä Access (O) + Access(I) + Sort (O) + Sort (I) ä The cost of a hash join is: ä Access (O) + Access(I) + Hash (min(I,O)) ä Can be used in equijoins only ä Not available with RBO

10 David Konopnicki –1997, Rev. MS-2004 10 The Join Order ä The optimizer: ä begins with the joins that results in a single row (using keys & unique indexes). ä In outer joins the outer table must appear after the inner table in the join condition. ä The possible execution plans are generated and compared according to estimated costs.


Download ppt "David Konopnicki –1997, Rev. MS-2004 1 Optimizing Join Statements To choose an execution plan for a join statement, the optimizer must choose: ä Access."

Similar presentations


Ads by Google