Relational Algebra Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1 Part II.

Post on 20-Jan-2016

217 views 0 download

Tags:

Transcript of Relational Algebra Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1 Part II.

Relational Algebra

Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

1

Part II

Summary of Relational-Algebra Operators

Set operators Union, Intersection, Difference

Selection & Projection & Extended Projection

Joins Natural, Theta, Outer join

Rename & Assignment

Duplicate elimination

Grouping & Aggregation

2

Examples ofRelationships Among Operators

3

Relationships Among Operators (I)

Intersect as Difference R ∩ S = R– (R–S)

Join as Cartesian Product + Select R ⋈C S = (σC (R X S))

Select is commutative σC2 (σC1 (R)) = σC1 (σC2 (R)) = σC1^C2 (R)

Order between Select & Project σC (πlist (R)) πlist (σC (R))

πlist (σC (R)) σC (πlist (R)) Only if “list” contains all columns needed by conditions C

4

Relationships Among Operators (II)

Join is commutative R ⋈C S = S ⋈C R

Left and Right outer joins are not commutative

Order between Select & Join σR.x=“5” (R ⋈R.a = S.b S ) ((σR.x=“5” (R)) ⋈R.a = S.b S)

5

Operations On Bags

6

Operations on Bags

Most DBMSs allow relations to be bags (not limited to sets)

All previous relational algebra operators apply to both sets and bags Bags allow duplicates

Duplicate elimination operator converts a bag into a set

Some properties may hold for sets but not bags Example: R U R = R (True for sets, False for bags)

7

Example Operations on Bags: Union: Consider two relations R and S that are union-

compatible

A B

1 2

3 4

1 2

RA B

1 2

3 4

5 6

S A B

1 2

1 2

1 2

3 4

3 4

5 6

R S

Suppose a tuple t appears in R m times, and in S n times.

Then in the union, t appears m + n times.

8

Example Operations on Bags: Intersection: ∩

Consider two relations R and S that are union-compatible

A B

1 2

3 4

1 2

R

A B

1 2

3 4

5 6

S

A B

1 2

3 4

R ∩ S

Suppose tuple t appears in R m times, and in S n times. Then in intersection, t appears min (m, n) times.

9

Example Operations on Bags: Difference: -

Suppose tuple t appears in R m times & in S n times. Then in R – S, t appears max (0, m - n) times.

A B

1 2

3 4

1 2

R

A B

1 2

3 4

5 6

S

A B

1 2

R – S

10

cs3431

Project: πA1, A2, …, An (R)

πA1, A2, …, An (R) returns tuples in R, but only columns A1, A2, …, An.

A B C

1 2 5

3 4 6

1 2 7

1 2 8

R πA, B (R)

A B

1 2

3 4

1 2

1 2

R is a set, but πA, B (R) is a bag

Some Basic Rules for Algebraic Expressions

(For Better Performance)

12

1- Joins vs. Cartesian Product

Use Joins instead of Cartesian products (followed by selection) R ⋈C S = (σC (R X S)) -- LHS is better

Intuition: There are efficient ways to do the L.H.S without going through the two-steps R.H.S

CS3431 13

2- Push Selection Down Whenever possible, push the selection down

Selection is executed as early as possible

Intuition: Selection reduces the size of the data

Examples σC (πlist (R)) πlist (σC (R)) -- RHS is better

σR.x=“5” (R ⋈R.a = S.b S ) ((σR.x=“5” (R)) ⋈R.a = S.b S) -- RHS is better

CS3431 14

3- Avoid Un-necessary Joins

Intuition: Joins can dramatically increase the size of the data

15

Find customers having account balance below 100 and loans above 10,000

R1 πcustomer_name (depositor ⋈ πaccount_number(σbalance <100 (account)))

R2 πcustomer_name (borrower ⋈ πloan_number(σamount >10,000 (loan)))

Result R1 ∩ R2

Better than joining the 4 relations and then selecting