1 of 61

CSE 344: Section 5

BCNF, Relational Algebra

October 26th, 2023

2 of 61

Announcements

Announcements

  • Homework 5:
    • Due at 10:00 pm on Tuesday, October 31st
      • Focus on Relational Algebra and Cardinality Estimation

3 of 61

Boyce-Codd Normal Form (BCNF)

4 of 61

Motivation of BCNF

Recall the three anomalies:

  • Redundancy anomaly
  • Update anomaly
  • Deletion anomaly

If a relation is in BCNF, then it does not have these anomalies

If it is not in BCNF, then it does have these anomalies

5 of 61

BCNF Algorithm

 

 

6 of 61

Example: BCNF Decomposition

Restaurant(id,name,rating,popularity,rec)

  1. id -> name, rating
  2. rating -> popularity
  3. popularity -> rec?
  • Example: We know that popularity determines rec (ex: Okay = N, Respectable = N, Poppin = Y)
    • What if we want to update the rec so that a popularity = ‘Respectable’ has rec= ‘Y’
      • UPDATE Restaurant

SET rec? = ‘Y’

WHERE popularity = ‘Respectable’;

  1. How many tuples in the Restaurant table do we need to update? How can we reduce this number?

id

name

rating

popularity

rec?

1

Mee Sum Pastry

3

Okay

N

2

Café on the Ave

4

Respectable

N

3

Guanaco’s Tacos

4

Respectable

N

4

Aladdin Gyro-Cery

5

Poppin

Y

Restaurant

7 of 61

Example: BCNF Decomposition

Restaurant(id,name,rating,popularity,rec)

  1. id -> name, rating
  2. rating -> popularity
  3. popularity -> rec?

rating+ = rating, popularity, rec?

id

name

rating

popularity

rec?

1

Mee Sum Pastry

3

Okay

N

2

Café on the Ave

4

Respectable

N

3

Guanaco’s Tacos

4

Respectable

N

4

Aladdin Gyro-Cery

5

Poppin

Y

Restaurant

8 of 61

Example: BCNF Decomposition

id

name

rating

popularity

rec?

1

Mee Sum Pastry

3

Okay

N

2

Café on the Ave

4

Respectable

N

3

Guanaco’s Tacos

4

Respectable

N

4

Aladdin Gyro-Cery

5

Poppin

Y

Restaurant(id,name,rating,popularity,rec)

  1. id -> name, rating
  2. rating -> popularity
  3. popularity -> rec?

Restaurant

rating+ = rating, popularity, rec

R1(rating,popularity,rec?)

R2(rating,id,name)

rating

popularity

rec?

3

Okay

N

4

Respectable

N

5

Poppin

Y

id

name

rating

1

Mee Sum Pastry

3

2

Café on the Ave

4

3

Guanaco’s Tacos

4

4

Aladdin Gyro-Cery

5

R2

R1

9 of 61

Example: BCNF Decomposition

id

name

rating

popularity

rec?

1

Mee Sum Pastry

3

Okay

N

2

Café on the Ave

4

Respectable

N

3

Guanaco’s Tacos

4

Respectable

N

4

Aladdin Gyro-Cery

5

Poppin

Y

Restaurant(id,name,rating,popularity,rec)

  1. id -> name, rating
  2. rating -> popularity
  3. popularity -> rec?

Restaurant

rating+ = rating, popularity, rec

R1(rating,popularity,rec?)

R2(rating,id,name)

popularity+ = popularity, rec?

id

name

rating

1

Mee Sum Pastry

3

2

Café on the Ave

4

3

Guanaco’s Tacos

4

4

Aladdin Gyro-Cery

5

R2

10 of 61

Example: BCNF Decomposition

id

name

rating

popularity

rec?

1

Mee Sum Pastry

3

Okay

N

2

Café on the Ave

4

Respectable

N

3

Guanaco’s Tacos

4

Respectable

N

4

Aladdin Gyro-Cery

5

Poppin

Y

Restaurant(id,name,rating,popularity,rec)

  1. id -> name, rating
  2. rating -> popularity
  3. popularity -> rec?

Restaurant

rating+ = rating, popularity, rec

R1(rating,popularity,rec?)

R2(rating,id,name)

popularity+ = popularity, rec?

id

name

rating

1

Mee Sum Pastry

3

2

Café on the Ave

4

3

Guanaco’s Tacos

4

4

Aladdin Gyro-Cery

5

R2

R4(popularity,rating)

R3(popularity,rec?)

11 of 61

Example: BCNF Decomposition

id

name

rating

popularity

rec?

1

Mee Sum Pastry

3

Okay

N

2

Café on the Ave

4

Respectable

N

3

Guanaco’s Tacos

4

Respectable

N

4

Aladdin Gyro-Cery

5

Poppin

Y

Restaurant(id,name,rating,popularity,rec)

  1. id -> name, rating
  2. rating -> popularity
  3. popularity -> rec?

Restaurant

rating+ = rating, popularity, rec

R1(rating,popularity,rec?)

R2(rating,id,name)

popularity+ = popularity, rec?

id

name

rating

1

Mee Sum Pastry

3

2

Café on the Ave

4

3

Guanaco’s Tacos

4

4

Aladdin Gyro-Cery

5

R2

R4(popularity,rating)

R3(popularity,rec?)

popularity

rec?

Okay

N

Respectable

N

Poppin

Y

R3

rating

popularity

3

Okay

4

Respectable

5

Poppin

R4

12 of 61

Example: BCNF Decomposition

id

name

rating

popularity

rec?

1

Mee Sum Pastry

3

Okay

N

2

Café on the Ave

4

Respectable

N

3

Guanaco’s Tacos

4

Respectable

N

4

Aladdin Gyro-Cery

5

Poppin

Y

Restaurant(id,name,rating,popularity,rec)

  1. id -> name, rating
  2. rating -> popularity
  3. popularity -> rec?

Restaurant

rating+ = rating, popularity, rec

R1(rating,popularity,rec?)

R2(rating,id,name)

popularity+ = popularity, rec?

id

name

rating

1

Mee Sum Pastry

3

2

Café on the Ave

4

3

Guanaco’s Tacos

4

4

Aladdin Gyro-Cery

5

R2

R4(popularity,rating)

R3(popularity,rec?)

popularity

rec?

Okay

N

Respectable

N

Poppin

Y

R3

rating

popularity

3

Okay

4

Respectable

5

Poppin

R4

R2, R3, R4 is the normalized database schema. No more anomalies!

13 of 61

Example: BCNF Decomposition

Now when we update the rec? so that a popularity = ‘Respectable’ has rec?= ‘Y’

UPDATE R3 SET rec? = ‘Y’ WHERE popularity = ‘Respectable’;

  • Now we only update 1 tuple in the R3 table so we have reduced redundancy, and therefore avoid redundancy anomalies, update anomalies and deletion anomalies

id

name

rating

1

Mee Sum Pastry

3

2

Café on the Ave

4

3

Guanaco’s Tacos

4

4

Aladdin Gyro-Cery

5

R2

R3

R4

popularity

rec?

Okay

N

Respectable

N

Poppin

Y

rating

popularity

3

Okay

4

Respectable

5

Poppin

14 of 61

Relational Algebra

15 of 61

RA Operators

Standard:

⋂ - Intersect

⋃ - Union

- Difference

σ - Select

π - Project

⍴ - Rename

Extended:

δ - Duplicate Elim.

ɣ - Group/Agg.

τ - Sorting

Joins:

⨝ - Nat. Join

⟕ - L.O. Join

⟖ - R.O. Join

⟗ - F.O. Join

✕- Cross Product

15

16 of 61

Ɣ Notation

Grouping and aggregation on group:

ɣattr_1, …, attr_k, count/sum/max/min(attr) -> alias

Aggregation on the entire table:

ɣcount/sum/max/min(attr) -> alias

16

17 of 61

Format

  • Follows FJWGHOS structure

17

18 of 61

Query Plans (Example SQL -> RA)

Select-Join-Project structure

Make this SQL query into RA (remember FWGHOS):

SELECT R.b, T.c, max(T.a) AS T_max

FROM Table_R AS R, Table_T AS T

WHERE R.b = T.b

GROUP BY R.b, T.c

HAVING max(T.a) > 99

18

19 of 61

Query Plans (Example SQL -> RA)

Select-Join-Project structure

Make this SQL query into RA (remember FWGHOS):

SELECT R.b, T.c, max(T.a) AS T_max

FROM Table_R AS R, Table_T AS T

WHERE R.b = T.b

GROUP BY R.b, T.c

HAVING max(T.a) > 99

19

20 of 61

Difference Operator

SELECT DISTINCT R.a

FROM Table_R AS R

WHERE NOT EXISTS (

SELECT *

FROM Table_S AS S

WHERE S.b = R.a

AND S.c < 15

);

20

  • We need to correctly exclude rows if they exist in the subquery
  • We cannot use σ (select) to compare rows
  • Solution is to use the (difference) operator!

21 of 61

Difference Operator

SELECT DISTINCT R.a

FROM Table_R AS R

WHERE NOT EXISTS (

SELECT *

FROM Table_S AS S

WHERE S.b = R.a

AND S.c < 15);

21

πR2.a

22 of 61

Difference Operator

SELECT DISTINCT R.a

FROM Table_R AS R

WHERE NOT EXISTS (

SELECT *

FROM Table_S AS S

WHERE S.b = R.a

AND S.c < 15);

Equivalent SQL:

SELECT DISTINCT * FROM Table_R R2

EXCEPT

SELECT R1.a FROM Table_R R1, Table_S S

WHERE S.c < 15 AND R1.a = S.b;

22

πR2.a

23 of 61

SQL TO RA

24 of 61

General Approach

  • Follows FJWGHOS structure

24

25 of 61

Subqueries in FROM to RA

Relational Algebra

84

26 of 61

Subqueries in FROM to RA

Relational Algebra

85

27 of 61

Subqueries in FROM to RA

Relational Algebra

86

28 of 61

Subqueries in FROM to RA

Relational Algebra

87

29 of 61

Subqueries in FROM to RA

Relational Algebra

88

30 of 61

Subqueries in WHERE/HAVING

Relational Algebra

99

31 of 61

Subqueries in WHERE/HAVING

Relational Algebra

100

32 of 61

Subqueries in WHERE/HAVING

Relational Algebra

101

33 of 61

Subqueries in WHERE/HAVING

Relational Algebra

100

34 of 61

Subqueries in WHERE/HAVING

Relational Algebra

100

35 of 61

Query Optimizations

36 of 61

Rewrite Rules

  • WHERE
  • SELECT
  • WHERE-AND
  • WHERE-OR
  • SELECT-WHERE
  • WHERE-HAVING
  • WHERE-JOIN

37 of 61

RA to RA: WHERE

Query Optimization

37

 

=

 

 

 

Idempotency

38 of 61

RA to RA: SELECT

Query Optimization

38

 

 

=

 

 

 

=

 

 

 

Idempotency

39 of 61

RA to RA: WHERE-AND

Query Optimization

39

 

=

 

 

 

 

=

 

 

 

 

Commutativity

40 of 61

RA to RA: WHERE-OR

Query Optimization

40

 

=

 

 

 

set

set

41 of 61

RA to RA: SELECT-WHERE

Query Optimization

41

 

 

=

 

 

“push down”

Conditional* Commutativity* if c only references attributes in A

42 of 61

RA to RA: WHERE-HAVING

Query Optimization

42

 

 

 

 

Schema(G,A)

Schema(G,A)

=

“push down”

Conditional* Commutativity�* if c only references attributes in G

43 of 61

Conditional Commutativity

Query Optimization

43

Attribute P.Job not in {P.UserID, P.Name}

44 of 61

RA to RA: WHERE-JOIN

Query Optimization

44

 

 

=

 

 

 

Distributive

45 of 61

Example Query Optimization

Query Optimization

45

Supplier x

Supply y

 

 

 

Push Selections Down�σC1 and C2(R ⋈ S)

= σC1C2(R ⋈ S))

= σC1(R ⋈ σC2(S))

= σC1(R) ⋈ σC2(S)

 

Supplier x

Supply y

 

 

 

Supplier(sid, name, city, state)

Supply(sid, pid, quantity)

46 of 61

Worksheet

47 of 61

Cardinality Estimation: Factors

T(R) = # tuples for relation R

V(R, a) = # of unique values of attribute a in relation R

48 of 61

Selectivity Formulas

  • Selectivity Factor (X) → Proportion of total data needed
    • Assuming uniform distribution of data values on numeric attribute a in table R, if the condition is:
      • a = c → X 1 / V(R, a)
      • a < c→ X (c - min(R, a))/ (max(R, a) - min(R, a))
      • c1 < a < c2→ X (c2 - c1)/ (max(R, a) - min(R, a))
      • cond1 and cond2 → X X1 * X2

49 of 61

50 of 61

51 of 61

Cardinality Estimation Example

52 of 61

Cardinality Estimation

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

SELECT sname

FROM Supply x, Supplier y

WHERE x.sid=y.sid AND x.pno=2 AND y.scity=’Seattle’ AND y.sstate=’WA’;

Supply(sid, pno, quantity)

Supplier(sid, sname, scity, sstate)

sid=sid

53 of 61

Cardinality Estimation

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

sid=sid

54 of 61

Cardinality Estimation

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T(Supply) * 1 / V(Supply, pno)

= 4

T(Supplier) * 1 / V(Supplier, scity) * 1 / V(Supplier, sstate)

= 5

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

sid=sid

55 of 61

Cardinality Estimation

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T1 = 4

T2 = 5

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

sid=sid

56 of 61

Cardinality Estimation

supplier_id=sid

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T1 = 4

T2 = 50

But wait a second…. Seattle is in Washington!

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

57 of 61

Cardinality Estimation

supplier_id=sid

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T1 = 4

T2 = 50

(4 * 50) /

max(

V(Supplier, sid),

V(Supply, sid)

)

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

58 of 61

Cardinality Estimation

supplier_id=sid

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T1 = 4

T2 = 50

(4 * 50) / 1000

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

59 of 61

Cardinality Estimation

supplier_id=sid

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T1 = 4

T2 = 50

T3 = 0.2

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

60 of 61

Cardinality Estimation

supplier_id=sid

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T1 = 4

T2 = 50

T3 = 0.2

No filtering at this step

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname

61 of 61

Cardinality Estimation

supplier_id=sid

Supply Statistics:

  • T(Supply) = 10000
  • B(Supply) = 100
  • V(Supply, pno) = 2500
  • V(Supply, sid) = 1000

Supplier Statistics:

  • T(Supplier) = 1000
  • B(Supplier) = 100
  • V(Supplier, scity) = 20
  • V(Supplier, sstate) = 10
  • V(Supplier, sid) = 100

T1 = 4

T2 = 50

T3 = 0.2

Total ≅ 0.2

Supply

σpno=2

Supplier

σscity=’Seattle’ Λ sstate=’WA’

πsname