lectures 8: relational algebra - university of …...relational algebra operators • union ∪,...

27
1 Introduction to Data Management CSE 344 Lectures 8: Relational Algebra CSE 344 - Winter 2016

Upload: others

Post on 08-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

1

Introduction to Data ManagementCSE 344

Lectures 8: Relational Algebra

CSE 344 - Winter 2016

Page 2: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Announcements

• Homework 3 is posted– Microsoft Azure Cloud services!– Use the promotion code you received– Due in two weeks

2

Page 3: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Where We Are• Data models• SQL, SQL, SQL

– Declaring the schema for our data (CREATE TABLE)– Inserting data one row at a time or in bulk (INSERT/.import)– Querying the data (SELECT)– Modifying the schema and updating the data (ALTER/UPDATE)

• Next step: More knowledge of how DBMSs work– Relational algebra, query execution, and physical tuning– Client-server architecture

CSE 344 - Fall 2016 3

Page 4: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

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 - Winter 2016 4

Logical plan àphysical plan

RelationalAlgebra

Page 5: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

The WHAT and the HOW

• SQL = WHAT we want to get from the data

• Relational Algebra = HOW to get the data we want

• The passage from WHAT to HOW is called query optimization– SQL à Relational Algebra à Physical Plan– Relational Algebra = Logical Plan

5CSE 344 - Winter 2016

Page 6: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Relational Algebra

CSE 344 - Winter 2016 6

Page 7: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Turing Awards in Data Management

CSE 344 - Fall 2016 7

Charles Bachman, 1973IDS and CODASYL

Ted Codd, 1981Relational model

Michael Stonebraker, 2014INGRES and Postgres

Page 8: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Sets v.s. Bags

• Sets: {a,b,c}, {a,d,e,f}, { }, . . .• Bags: {a, a, b, c}, {b, b, b, b, b}, . . .

Relational Algebra has two semantics:• Set semantics = standard Relational Algebra• Bag semantics = extended Relational Algebra

DB systems implement bag semantics (Why?)CSE 344 - Winter 2016 8

Page 9: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Relational Algebra Operators• Union ∪, intersection ∩, difference -• Selection σ• Projection π• Cartesian product X, join ⨝• Rename ρ• Duplicate elimination δ• Grouping and aggregation ɣ• Sorting 𝛕

CSE 344 - Winter 2016 9

RA

Extended RA

All operators take in 1 or more relations as inputs and return another relation

Page 10: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Union and Difference

What do they mean over bags ?

R1 ∪ R2R1 – R2

CSE 344 - Winter 2016 10

Page 11: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

What about Intersection ?

• Derived operator using minus

• Derived using join

– Only makes sense if R1 and R2 have the same schema

R1 ∩ R2 = R1 – (R1 – R2)

R1 ∩ R2 = R1 ⨝ R2

CSE 344 - Winter 2016 11

Page 12: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Selection• Returns all tuples which satisfy a condition

• Examples– σSalary > 40000 (Employee)– σname = “Smith” (Employee)

• The condition c can be =, <, <=, >, >=, <>combined with AND, OR, NOT

σc(R)

CSE 344 - Winter 2016 12

Page 13: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

σSalary > 40000 (Employee)

SSN Name Salary1234545 John 200005423341 Smith 600004352342 Fred 50000

SSN Name Salary5423341 Smith 600004352342 Fred 50000

Employee

CSE 344 - Winter 2016 13

Page 14: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Projection• Eliminates columns

• Example: project social-security number and names:– πSSN, Name (Employee)– Answer(SSN, Name)

π A1,…,An(R)

CSE 344 - Winter 2016 14Different semantics over sets or bags! Why?

Page 15: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

π Name,Salary (Employee)

SSN Name Salary1234545 John 200005423341 John 600004352342 John 20000

Name SalaryJohn 20000John 60000John 20000

Employee

Name SalaryJohn 20000John 60000

Bag semantics Set semantics

15CSE 344 - Winter 2016Which is more efficient?

Page 16: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

CSE 344 - Winter 2016

Composing RA Operatorsno name zip disease1 p1 98125 flu2 p2 98125 heart3 p3 98120 lung4 p4 98120 heart

Patient

σdisease=‘heart’(Patient)no name zip disease2 p2 98125 heart4 p4 98120 heart

zip disease98125 flu98125 heart98120 lung98120 heart

πzip,disease(Patient)

πzip,disease(σdisease=‘heart’(Patient))

16

zip disease98125 heart98120 heart

Page 17: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Cartesian Product

• Each tuple in R1 with each tuple in R2

• Rare in practice; mainly used to express joins

R1 X R2

CSE 344 - Winter 2016 17

Page 18: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Name SSNJohn 999999999Tony 777777777

EmployeeEmpSSN DepName999999999 Emily777777777 Joe

Dependent

Employee X DependentName SSN EmpSSN DepNameJohn 999999999 999999999 EmilyJohn 999999999 777777777 JoeTony 777777777 999999999 EmilyTony 777777777 777777777 Joe

Cross-Product Example

CSE 344 - Winter 2016 18

Page 19: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Renaming

• Changes the schema, not the instance

• Example:– ρN, S(Employee) à Answer(N, S)

ρB1,…,Bn (R)

19

Not really used by systems, but needed on paper

CSE 344 - Winter 2016

Page 20: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Natural Join

• Meaning: R1⨝R2 = ΠA(σθ (R1 × R2))

• Where:– Selection checks equality of all common

attributes (i.e., attributes with same names)– Projection eliminates duplicate common

attributesCSE 344 - Winter 2016 20

R1 ⨝R2

Page 21: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Natural Join ExampleA BX YX ZY ZZ V

B CZ UV WZ V

A B CX Z UX Z VY Z UY Z VZ V W

R S

R ⨝ S =ΠABC(σR.B=S.B(R × S))

CSE 344 - Winter 2016 21

Page 22: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

CSE 344 - Winter 2016

Natural Join Example 2

age zip disease54 98125 heart20 98120 flu

AnonPatient P Voters V

P V

name age zipp1 54 98125p2 20 98120

age zip disease name

54 98125 heart p1

20 98120 flu p2

22

Page 23: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Natural Join

• Given schemas R(A, B, C, D), S(A, C, E), what is the schema of R ⨝ S ?

• Given R(A, B, C), S(D, E), what is R ⨝ S?

• Given R(A, B), S(A, B), what is R ⨝ S?

CSE 344 - Winter 2016 23

Page 24: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Theta Join

• A join that involves a predicate

• Here θ can be any condition• No projection in this case!• For our voters/patients example:

R1 ⨝θ R2 = σθ (R1 X R2)

P ⨝ P.zip = V.zip and P.age >= V.age -1 and P.age <= V.age +1 V24

AnonPatient (age, zip, disease)Voters (name, age, zip)

Page 25: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

Equijoin• A theta join where θ is an equality predicate• Projection drops all redundant attributes

• By far the most used variant of join in practice

R1 ⨝θ R2 = πA(σθ (R1 X R2))

CSE 344 - Winter 2016 25

Page 26: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

CSE 344 - Winter 2016

Equijoin Example

age zip disease54 98125 heart20 98120 flu

AnonPatient P Voters V

P P.age=V.age V

name age zipp1 54 98125p2 20 98120

26

age P.zip disease name V.zip

54 98125 heart p1 98125

20 98120 flu p2 98120

Page 27: Lectures 8: Relational Algebra - University of …...Relational Algebra Operators • Union ∪, intersection ∩, difference - • Selection σ • Projection π • Cartesian product

CSE 344 - Winter 2016

Join Summary• Theta-join: R ⨝θ S = σθ (R x S)

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

• Equijoin: R ⨝θ S = πA (σθ (R x S))– Join condition θ consists only of equalities– Projection πA drops all redundant attributes

• Natural join: R ⨝ S = πA (σθ (R x S))– Equijoin– Equality on all fields with same name in R and in S– Projection πA drops all redundant attributes

27