Download - Teradata Joins

Transcript
Page 1: Teradata Joins

Everything You Wanted ToKnow About Joins But…..!!!

Mark N. Jauss

NCR Corporation

Page 2: Teradata Joins

Key Areas

• What are Joins

• Types of Joins

• Examples of Joins

• Potential problems with Joins

Page 3: Teradata Joins

The Basics -- What Are Joins?

• A concept in relational database theory

• Technique for accessing 2+ tables in a single answer set

• Each answer set row may contain data from each table

• Joined on “join” column

Page 4: Teradata Joins

ANSI 89 -vs- ANSI 92

• ANSI 89 Standard

• Sometimes called the “Where” version

• No Join command necessary

Page 5: Teradata Joins

ANSI 89 -vs- ANSI 92

• ANSI 92 Standard

• Requires a Join command

• Requires a From clause

• All factors equal, performance the same

Page 6: Teradata Joins

Types Of Joins

• We will discuss the following Joins:

• Inner Join

• Outer Joins

• Full

• Left

• Right

• Cross Join

• Self Join

Page 7: Teradata Joins

Inner Joins

• Rows which match based on join criteria

• Example:

Select employee.lname, employee.fname

From employee Inner Join department

On employee.departmentnum = department.departmentnum;

Page 8: Teradata Joins

Outer Joins

• Full: Both tables used to qualify and both extended with Nulls

• Left: Left table used to qualify and right table has Nulls for non-matching rows

• Right: Right table used to qualify and left table has Nulls for non-matching rows

Page 9: Teradata Joins

Full Outer Join

• Example:

Select employee.lname, employee.fname

From employee Full Outer Join department

On employee.departmentnum=department.departmentnum;

Page 10: Teradata Joins

Left Outer Join

• Example:

Select employee.lname, employee.fname

From employee Left Outer Join department

On employee.departmentnum = department.departmentnum;

Page 11: Teradata Joins

Right Outer Join

• Example:

Select employee.lname, employee.fname

From employee Right Outer Join department

On employee.departmentnum = department.departmentnum;

Page 12: Teradata Joins

Cross Join

• Each row of one table matched with each row of another table (no ON clause)

• Example:

Select employee.lname, employee.fname

From employee Cross Join department

Where employee.employee_number = 1008;

Page 13: Teradata Joins

Self Join

• Rows matching other rows within the same table

• Example:

Select employee.lname, employee.fname

From employee emp Inner Join employee mgr

On emp.departmentnum=mgr.departmentnum;

Page 14: Teradata Joins

Alias Names

• Temporary name for Table or View

• Defined in From clause

• Useful as abbreviation

• Required in Self Join

• Once defined, must be used in SQL where table name required

Page 15: Teradata Joins

Multiple Table Joins

• A join can have up to 64 participating tables

• An n-table join requires n-1 join conditions

• Omitting a join condition will result in a Cartesian Product Join

Page 16: Teradata Joins

Multiple Table Joins

• Example:

Select employee.lname, employee.fname

From employee Inner Join department

On employee.departmentnum = department.departmentnum

Inner Join job

On employee.jobcode = job.jobcode;

Page 17: Teradata Joins

Potential Problems With Joins

• Generating a Cartesian Product Join (spool problems)

• How:

• Cross Join with no Where clause

• Improper aliasing

• Omitting a Join condition

• Improper Join condition

Page 18: Teradata Joins

Potential Problems With Joins

• Improper aliasing:

Select employee.lname, employee.fname

From employee emp Inner Join department dep

On emp.departmentnum = dep.departmentnum

Page 19: Teradata Joins

Potential Problems With Joins

• Omitting a Join condition:

Select employee.lname, employee.fname

From employee Inner Join

department

On employee.departmentnum =

department.departmentnum Inner Join job;

Page 20: Teradata Joins

Potential Problems With Joins

• Improper Join condition:

Select employee.lname, employee.fname

From employee Inner Join department

On 3 = 3;

Page 21: Teradata Joins

Comparing Subqueries To Joins

• Subquery answer set only displays from outer query table

• Subquery easier to write (?)

• All things equal, Joins are usually better performers

Page 22: Teradata Joins

Sample Explain Of Subquery

EXPLAIN

SELECT first_name

,last_name

,department_number

FROM employee

WHERE department_number IN

(SELECT department_number

FROM department

WHERE department_name LIKE ‘%Research%’);

Page 23: Teradata Joins

Sample Explain Output Of Subquery

5) We do an all-AMPs JOIN step from Spool 2 (Last Use) by way of an all-rows scan, which is joined to CUSTOMER_SERVICE.departmentwith a condition of ("CUSTOMER_SERVICE.department.department_name LIKE '%Research%'"). Spool 2 and CUSTOMER_SERVICE.departmentare joined using a merge join, with a join condition of ("Spool_2.department_number CUSTOMER_SERVICE.department.department_number"). The result goes into Spool 1, which is built locally on the AMPs. The size of Spool 1 is estimated to be 24 rows. The estimated time for this step is 0.18 seconds.

-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0 hours and 0.24 seconds.

Page 24: Teradata Joins

Sample Explain Of Join

EXPLAIN

SELECT employee.first_name

,employee.last_name

,employee.department_number

FROM employee INNER JOIN

department

ON employee.department_number =

department.department_number

WHERE department.department_name LIKE ‘%Research%’;

Page 25: Teradata Joins

Sample Explain Output Of Join

5) We do an all-AMPs JOIN step from Spool 2 (Last Use) by way of an all-rows scan, which is joined to CUSTOMER_SERVICE.departmentwith a condition of ("CUSTOMER_SERVICE.department.department_name LIKE '%Research%'"). Spool 2 and CUSTOMER_SERVICE.departmentare joined using a merge join, with a join condition of ("Spool_2.department_number CUSTOMER_SERVICE.department.department_number"). The result goes into Spool 1, which is built locally on the AMPs. The size of Spool 1 is estimated to be 24 rows. The estimated time for this step is 0.18 seconds.

6) Finally, we send out an END TRANSACTION step to all AMPsinvolved in processing the request.

-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0 hours and 0.23 seconds

Page 26: Teradata Joins

Summary

• Several different types of Joins

• Inner Join

• Full Outer Join

• Left Outer Join

• Right Outer Join

• Cross Join

• Self Join

Page 27: Teradata Joins

Summary

• Potential problems

• Improper aliasing

• Improper Join condition

• Omitting a Join condition

• Subqueries -vs- Joins

• Question and Answer