multi-table queries (join) 2 tables

21
Retrieve the part name for all parts shipped in quantities equal to 200 SELECT DISTINCT PART.PartName FROM PART, SHIPMENT WHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

Upload: delano

Post on 05-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Multi-table queries (JOIN) 2 tables. Retrieve the part name for all parts shipped in quantities equal to 200. SELECT DISTINCT PART.PartName FROM PART, SHIPMENT WHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;. Conjunct #1 is a PK FK comparison. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Multi-table queries (JOIN) 2 tables

Retrieve the part name for all parts shipped in quantities equal to 200SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

Page 2: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

So how does this work?

Conjunct #1 is a PK FK comparison

Conjunct #2 qualifies retrieval

Page 3: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

Conceptually, we can understand the execution of the query in terms of row scans.

A Scan is a sequential inspection of many rows for the purpose of returning rows that meet a criteria.

The following demonstrations are a simplified version of the Nested Loop technique (as opposed to “merge sort” and “hashed join”)

Page 4: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

PK/FK comparison requires one scan of the FK set for each PK in the parent table.Row is included in the view where PK=FK and Qty = 200

Page 5: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

(Scan 1)

.

.

.(FK Scan)

(No Matches: TT)

(P1=P1 300=200)(P1=P2 200=200)

(P1=P5 100=200)

Page 6: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

(Scan 2)

.

.

.(FK Scan)

(Two Matches: TT)

(P2=P1 300=200)(P2=P2 200=200)

(P2=P5 100=200)

This removes duplicates in results

Page 7: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

(Scan 3)

.

.

.(FK Scan)

( No Matches: TT)

(P3=P1 300=200)(P3=P2 200=200)

(P3=P5 100=200)

Page 8: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

(Scan 4)

.

.

.(FK Scan)

( Two Matches: TT)

(P4=P1 300=200)(P4=P2 200=200)

(P4=P5 100=200)

(And So On...)

Page 9: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT PART.PartNameFROM PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SHIPMENT.quantity = 200;

(Scan 6)

How many row comparisons?rows(PK-table) X rows(FK-table)6 X 12 = 72

Page 10: Multi-table queries (JOIN) 2 tables

Three common methods used by DBMS optimizers to evaluate Joins◦ Nested Loop◦ Merge Scan◦ Hash Join

Page 11: Multi-table queries (JOIN) 2 tables

Nested Loop◦ Essentially the preceding demo◦ One table defined as external and one table

defined as internal (External:Internal) (1:M) (Parent:Child)

◦ If there isn’t an index on the FK, the internal table has to be opened for a scan for every row of the external table

Page 12: Multi-table queries (JOIN) 2 tables

Retrieve supplier name and part name for parts shipped in quantities less than 400.SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400;

Page 13: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400; (Scan 1)

(One Match: TT T)

.

.

.(FK Scan)

(P1=P1 S1=S1 400>300)(P1=P2 S1=S1 400>200)

(P1=P5 S1=S4 400>100)

Page 14: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400; (Scan 2)

(No Matches: TT T)

.

.

.(FK Scan)

(P1=P1 S2=S1 400>300)(P1=P2 S2=S1 400>200)

(P1=P5 S2=S4 400>100)

Page 15: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400; (Scan 2-5)

(No Matches: TT T)

.

.

.

So we finish out the “P1” Supplier scan with no matches

Page 16: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400; (Scan 6)

(One Match: TT T)

.

.

.(FK Scan)

(P2=P1 S1=S1 400>300)(P2=P2 S1=S1 400>200)

(P2=P5 S1=S4 400>100)

Page 17: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400; (Scan 7)

(One Match: TT T)

.

.

.(FK Scan)

(P2=P1 S2=S1 400>300)(P2=P2 S2=S1 400>200)

(P1=P5 S2=S4 400>100)

Page 18: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400; (Scan 8)

(One Match: TT T)

.

.

.(FK Scan)

(P2=P1 S3=S1 400>300)(P2=P2 S3=S1 400>200)

(P1=P5 S3=S4 400>100)

(And so on...)

Page 19: Multi-table queries (JOIN) 2 tables

SELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber AND SHIPMENT.Quantity < 400;

How many row comparisons?6x5x12 = 360

Page 20: Multi-table queries (JOIN) 2 tables

Retrieve the supplier name and part name for all shipments with quantity greater than or equal to 200 for which the warehouse is located in the same city as the supplierSELECT DISTINCT SUPPLIER.SupplierName, PART.PartNameFROM SUPPLIER, PART, SHIPMENTWHERE (PART.PartNumber = SHIPMENT.PartNumber AND SUPPLIER.SupplierNumber = SHIPMENT.SupplierNumber) AND (SHIPMENT.Quantity >= 200 AND SUPPLIER.SupplierCity = PART.PartCity);

Page 21: Multi-table queries (JOIN) 2 tables

For each row in the outer table (Parent), every row in the inner table (Child) is scanned for a PK/FK match and any qualifying predicate.In your WHERE clause, you must have a PK=FK statement for every Parent/Child relationship involved in the query.

(PK=FK Scan)

Data

Data

Data

Data

Data

FK Value

FK Value

FK Value

FK Value

FK Value

PK Value

PK Value

PK Value

Data

Data

Data

(Outer Table) (Inner Table)

OuterTable

InnerTable