Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra Instructor: Mohamed Eltabakh 1 Part II.

Similar presentations


Presentation on theme: "Relational Algebra Instructor: Mohamed Eltabakh 1 Part II."— Presentation transcript:

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

2 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

3 Examples of Relationships Among Operators 3

4 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

5 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

6 Operations On Bags 6

7 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

8 Example Operations on Bags: Union:  Consider two relations R and S that are union- compatible AB 12 34 12 R AB 12 34 56 S AB 12 12 12 34 34 56 R  SR  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

9 Example Operations on Bags: Intersection: ∩ Consider two relations R and S that are union-compatible AB 12 34 12 R AB 12 34 56 S AB 12 34 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

10 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. AB 12 34 12 R AB 12 34 56 S AB 12 R – S 10

11 cs3431 Project: π A1, A2, …, An (R) π A1, A2, …, An (R) returns tuples in R, but only columns A1, A2, …, An. ABC 125 346 127 128 R π A, B (R) AB 12 34 12 12 R is a set, but π A, B (R) is a bag

12 Some Basic Rules for Algebraic Expressions (For Better Performance) 12

13 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 CS343113

14 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 CS343114

15 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


Download ppt "Relational Algebra Instructor: Mohamed Eltabakh 1 Part II."

Similar presentations


Ads by Google