+ All Categories
Home > Documents > HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg...

HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg...

Date post: 14-Oct-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
39
1 Database Systems CSE 344 Lectures 9: Relational Algebra (part 2) and Query Evaluation (Ch. 5.2 & 16.3 (skim 16.3.2)) CSE 344 – Summer 2017
Transcript
Page 1: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

1

Database SystemsCSE 344

Lectures 9: Relational Algebra (part 2) and Query Evaluation

(Ch. 5.2 & 16.3 (skim 16.3.2))

CSE 344 – Summer 2017

Page 2: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Announcements

• WQ3 is due on Monday July 10

• HW1 grades were be posted

• HW3 - Get Azure Setup

CSE 344 – Summer 2017 2

Page 3: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Azure Subscription

CSE 344 – Summer 2017 3

You will get an email like this to your Azure account.

Page 4: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Relational Algebra Operators

• Union , intersection , difference -

• Selection s • Projection (Π)

• Cartesian product , join

• Rename • Duplicate elimination d• Grouping and aggregation g• Sorting t

CSE 344 - Summer 2017 4

RA

Extended RA

(Sigma)

(Pi)

(Rho)

(Delta)

(Gamma)

(Tau)

Page 5: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 – Summer 2017

Join Summary• Theta-join: R S = s(R x S)

– Join of R and S with a join condition – Cross-product followed by selection

• Equijoin: R S = s(R x S)– Join condition consists only of equalities

• Natural join: R S = A (s(R x S))– Equijoin

– Equality on all fields with same name in R and in S

– Projection A drops all redundant attributes

5

Page 6: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 – Summer 2017

More Joins

• Outer join– Include tuples with no matches in the output

– Use NULL values for missing attributes

– Does not eliminate duplicate columns

• Variants– Left outer join ( )

– Right outer join ( )

– Full outer join ( )

6

Page 7: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 – Summer 2017

More Examples

Supplier(sno,sname,scity,sstate)Part(pno,pname,psize,pcolor)

Supply(sno,pno,qty,price)

Name of supplier of parts with size greater than 10

sname(Supplier Supply (spsize>10 (Part))

Name of supplier of red parts or parts with size greater than 10

sname(Supplier Supply (spsize>10 (Part) spcolor=‘red’ (Part) ) )

7

Page 8: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Query Evaluation Steps

Parse & Check Query

Decide how best to answer query:

query optimization

Query Execution

SQL query

Return Results

Translate query string into internal

representation

Check syntax, access control,

table names, etc.

QueryEvaluation

CSE 344 – Summer 2017 8

Logical plan physical plan

Page 9: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

From SQL to RA

CSE 344 – Summer 2017 9

• SQL → RA → Syntax Tree• Graphical representation of evaluation order.

+

3 4

*

5 3

4 5

+

*

(3 + 4) * 5 3 + 4 * 5

Page 10: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

From SQL to RA

Product Purchase

x.pid = y.pid and y.cid = z.cid and

price > 100 and city = ‘Seattle’

x.name,z.name

δ

Customer

σ

Product(pid, name, price)Purchase(pid, cid, store)Customer(cid, name, city)

SELECT DISTINCT x.name, z.name

FROM Product x, Purchase y, Customer z

WHERE x.pid = y.pid and y.cid = z.cid andx.price > 100 and

z.city = ‘Seattle’

×

×

10

Is there a

better plan?

Page 11: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

RA: Equivalence Transformations

CSE 344 – Summer 2017 11

σθ1 θ2(E) = σθ1(σθ2(E))

σθ1(σθ2(E)) = σθ2(σθ2(E))

E1 θE2 = E2 θE1

(E1 E2) E3 = E1 (E2 E3)

Conjunctive selection operations can be deconstructed

Selection operations are commutative

Thata joins are commutative

Natural joins are associative

Page 12: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

From SQL to RA

Product Purchase

pid=pid

price>100 and city=‘Seattle’

x.name,z.name

δ

cid=cid

Customer

σ

Product(pid, name, price)Purchase(pid, cid, store)Customer(cid, name, city)

SELECT DISTINCT x.name, z.name

FROM Product x, Purchase y, Customer z

WHERE x.pid = y.pid and y.cid = z.cid andx.price > 100 and

z.city = ‘Seattle’

Can you think ofanother plan?

12

Page 13: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

From SQL to RA

Product Purchase

pid=pidcity=‘Seattle’

x.name,z.name

δ

cid=cid

Customer

σprice>100

σ

Push selections downthe query plan!

Product(pid, name, price)Purchase(pid, cid, store)Customer(cid, name, city)

SELECT DISTINCT x.name, z.name

FROM Product x, Purchase y, Customer z

WHERE x.pid = y.pid and y.cid = z.cid andx.price > 100 and

z.city = ‘Seattle’

Query optimization: find

an equivalent optimal plan

Can you think ofanother plan?

CSE 344 – Summer 2017 13

Page 14: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Extended RA: Operators on Bags

• Duplicate elimination d• Grouping g

– Takes in relation and a list of grouping operations (e.g., aggregates). Returns a new relation.

• Sorting t– Takes in a relation, a list of attributes to sort on,

and an order. Returns a new relation.

CSE 344 - Winter 2017 14

Page 15: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

15

Using Extended RA Operators

SELECT city, count(*)FROM salesGROUP BY cityHAVING sum(price) > 100

sales(product, city, price)

g city, sum(price)→p, count(*) → c

s p > 100

P city, c

T1(city,p,c)

T2(city,p,c)

T3(city, c)

T1, T2, T3 = temporary tables

CSE 344 - Winter 2017

Page 16: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 - Winter 2017

Typical Plan for a Query (1/2)

R S

join condition

σselection condition

πfields

join condition

SELECT-PROJECT-JOIN

Query

Answer

16

SELECT fieldsFROM R, S, …WHERE condition

Page 17: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 - Winter 2017

Typical Plan for a Query (1/2)πfields

ɣfields, sum/count/min/max(fields)

σhaving condition

σwhere condition

join condition

… …17

SELECT fieldsFROM R, S, …WHERE conditionGROUP BY fieldsHAVING condition

Page 18: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

SELECT Q.sno

FROM Supplier Q

WHERE Q.sstate = ‘WA’ and not exists

(SELECT *

FROM Supply PWHERE P.sno = Q.sno

and P.price > 100)

CSE 344 – Summer 2017

How about Subqueries?

18

Correlation !

Supplier(sno,sname,scity,sstate)

Part(pno,pname,psize,pcolor)

Supply(sno,pno,qty,price)

Page 19: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 – Summer 2017

SELECT Q.sno

FROM Supplier Q

WHERE Q.sstate = ‘WA’ and not exists

(SELECT *

FROM Supply PWHERE P.sno = Q.sno

and P.price > 100)

19

De-Correlation

SELECT Q.sno

FROM Supplier Q

WHERE Q.sstate = ‘WA’and Q.sno not in

(SELECT P.sno

FROM Supply PWHERE P.price > 100)

How about Subqueries?

Supplier(sno,sname,scity,sstate)

Part(pno,pname,psize,pcolor)

Supply(sno,pno,qty,price)

Page 20: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

How about Subqueries?

20

Un-nesting

SELECT Q.sno

FROM Supplier Q

WHERE Q.sstate = ‘WA’and Q.sno not in

(SELECT P.sno

FROM Supply PWHERE P.price > 100)

(SELECT Q.sno

FROM Supplier Q

WHERE Q.sstate = ‘WA’)

EXCEPT(SELECT P.sno

FROM Supply PWHERE P.price > 100)

Supplier(sno,sname,scity,sstate)

Part(pno,pname,psize,pcolor)

Supply(sno,pno,price)

EXCEPT = set difference

CSE 344 – Summer 2017

Page 21: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 – Summer 2017

Supply

σsstate=‘WA’

Supplier

σPrice > 100

21

−Finally…

snosno

(SELECT Q.sno

FROM Supplier Q

WHERE Q.sstate = ‘WA’)

EXCEPT(SELECT P.sno

FROM Supply PWHERE P.price > 100)

How about Subqueries?

Supplier(sno,sname,scity,sstate)

Part(pno,pname,psize,pcolor)

Supply(sno,pno,qty,price)

Page 22: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 – Summer 2017 22

From Logical Plans to Physical Plans

Page 23: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Relational Algebra

CSE 344 – Summer 2017 23

Give a relational algebra expression for this query

Supplier(sid, sname, scity, sstate)

Supply(sid, pno, quantity)

SELECT sname

FROM Supplier x, Supply y

WHERE x.sid = y.sidand y.pno = 2and x.scity = ‘Seattle’and x.sstate = ‘WA’

Page 24: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Relational Algebra

CSE 344 – Summer 2017 24

sname(σ scity=‘Seattle’∧ sstate=‘WA’∧ pno=2 (Supplier sid = sid Supply))

Supplier(sid, sname, scity, sstate)

Supply(sid, pno, quantity)

SELECT sname

FROM Supplier x, Supply y

WHERE x.sid = y.sidand y.pno = 2and x.scity = ‘Seattle’and x.sstate = ‘WA’

Page 25: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

25

Supplier Supply

sid = sid

σ scity=‘Seattle’ ∧ sstate=‘WA’ ∧ pno=2

sname

Relational Algebra

CSE 344 – Summer 2017

Relational algebra expression is also called the “logical query plan”

Supplier(sid, sname, scity, sstate)

Supply(sid, pno, quantity)

SELECT sname

FROM Supplier x, Supply y

WHERE x.sid = y.sidand y.pno = 2and x.scity = ‘Seattle’and x.sstate = ‘WA’

Page 26: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Query Evaluation Steps Review

Parse & Rewrite Query

Select Logical Plan

Select Physical Plan

Query Execution

Disk

SQL query

Queryoptimization

Logicalplan (RA)

Physicalplan

26

Page 27: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Physical Operators

Each of the logical operators may have one or more implementations = physical operators

Will discuss several basic physical operators, with a focus on join

CSE 344 – Summer 2017 27

Page 28: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

28

Physical Query Plan 1

Supplier Supply

sid = sid

σ scity=‘Seattle’ ∧sstate=‘WA’ ∧ pno=2

sname

(File scan) (File scan)

(Nested loop)

(On the fly)

(On the fly)

CSE 344 – Summer 2017

A physical query plan is a logical query plan annotated with physical implementation details

Supplier(sid, sname, scity, sstate)

Supply(sid, pno, quantity)

SELECT sname

FROM Supplier x, Supply y

WHERE x.sid = y.sidand y.pno = 2and x.scity = ‘Seattle’and x.sstate = ‘WA’

Page 29: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Main Memory Algorithms

Logical operator:

Product(pid, name, price) pid=pid Purchase(pid, cid, store)

Propose three physical operators for the join, assuming the tables are in main memory:

1. Nested Loop Join O(n2)2. Merge join O(n log n)3. Hash join O(n) … O(n2)

CSE 344 – Summer 2017 29

Product(pid, name, price)Purchase(pid, cid, store)

add n to hash – O(n)?lookup n in hash – O(n)?

Page 30: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

30

Physical Query Plan 2

Supplier Supply

sid = sid

σ scity=‘Seattle’ ∧sstate=‘WA’ ∧ pno=2

sname

(File scan) (File scan)

(Hash join)

(On the fly)

(On the fly)

CSE 344 – Summer 2017

Same logical query plan

Different physical plan

Supplier(sid, sname, scity, sstate)

Supply(sid, pno, quantity)

SELECT sname

FROM Supplier x, Supply y

WHERE x.sid = y.sidand y.pno = 2and x.scity = ‘Seattle’and x.sstate = ‘WA’

Page 31: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

31

Supplier Supply

sid = sid

σ scity=‘Seattle’ ∧sstate=‘WA’

sname

(File scan) (File scan)

(Sort-merge join)

(Scan & write to T2)

(On the fly)

σ pno=2

(Scan & write to T1)

Physical Query Plan 3

CSE 344 – Summer 2017

Supplier(sid, sname, scity, sstate)

Supply(sid, pno, quantity)

Different but equivalent logical query plan; different physical plan

SELECT sname

FROM Supplier x, Supply y

WHERE x.sid = y.sidand y.pno = 2and x.scity = ‘Seattle’and x.sstate = ‘WA’

Page 32: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Query Optimization Problem

• For each SQL query… many logical plans

• For each logical plan… many physical plans

• How do find a fast physical plan?– Will discuss in a few lectures

– First we need to understand how query operators are implemented

CSE 344 - Winter 2017 32

Page 33: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 - Winter 2017 33

Query Execution

Page 34: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Iterator Interface for Query Operators (Relations)

• open()– Initializes operator state

– Sets parameters such as selection condition

• next()– Operator invokes get_next() recursively on its

inputs

– Performs processing and produces an output tuple

• close(): clean-up state34

Page 35: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 - Winter 2017

Pipelined Query Execution

Suppliers Supplies

sno = sno

σscity=‘Seattle’ and sstate=‘WA’ and pno=2

πsname

(File scan) (File scan)

(Nested loop)

(On the fly)

(On the fly)

35

open()

open()

open()

open() open()

Page 36: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 - Winter 2017

Suppliers Supplies

sno = sno

σscity=‘Seattle’ and sstate=‘WA’ and pno=2

πsname

(File scan) (File scan)

(Nested loop)

(On the fly)

(On the fly)

36

next()

next()

next()

next()next()

next()

Pipelined Query Execution

In 444 you implement this.

Page 37: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 - Winter 2017

Pipelined Execution

• Tuples generated by an operator are immediately sent to the parent

• Benefits:– No operator synchronization issues

– No need to buffer tuples between operators

– Saves cost of writing intermediate data to disk

– Saves cost of reading intermediate data from disk

• This approach is used whenever possible

37

Page 38: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

CSE 344 - Winter 2017

Query Execution Bottom Line

• SQL query transformed into physical plan– Access path selection for each relation

• Scan the relation or use an index (next lecture)

– Implementation choice for each operator• Nested loop join, hash join, etc.

– Scheduling decisions for operators• Pipelined execution or intermediate materialization

• Pipelined execution of physical plan

38

Page 39: HW $]XUH 6HWXS€¦ · )urp 64/ wr 5$ 3urgxfw3xufkdvh slg slg flw\ µ6hdwwoh¶ [ qdph ] qdph / flg flg &xvwrphu s 1 sulfh! 1 3xvk vhohfwlrqv grzq wkh txhu\ sodq 3urgxfw slg qdph sulfh

Physical Data Independence

• Applications are insulated from changes in physical storage details

• SQL and relational algebra facilitate physical data independence – Both languages input and output relations

– Can choose different implementations for operators

CSE 344 - Winter 2017 39


Recommended