VIEWS: 5 PAGES: 37 POSTED ON: 5/3/2012 Public Domain
Chapter 4 Relational Algebra p By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental power of the race. -- Alfred North Whitehead (1861 - 1947) Ch4-1: Relational Algebra S. Pisitkasem 1 Relational Query Languages • Query languages: Allow manipulation and retrieval of data from a database. • Relational model supports simple, powerful QLs: – Strong formal foundation based on logic. – Allows for much optimization. • Query Languages != programming languages! – QLs not expected to be “Turing complete”. – QLs not intended to be used for complex calculations. – QLs support easy, efficient access to large data sets. 2 Ch4-1: Relational Algebra S. Pisitkasem Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: Relational Algebra: More operational, very useful for representing execution plans. Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non-procedural, declarative.) Understanding Algebra & Calculus is key to understanding SQL, query processing! 3 Ch4-1: Relational Algebra S. Pisitkasem Preliminaries • A query is applied to relation instances, and the result of a query is also a relation instance. – Schemas of input relations for a query are fixed (but query will run over any legal instance) – The schema for the result of a given query is also fixed. It is determined by the definitions of the query language constructs. • Positional vs. named-field notation: – Positional notation easier for formal definitions, named-field notation more readable. – Both used in SQL • Though positional notation is not encouraged 4 Ch4-1: Relational Algebra S. Pisitkasem Relational Algebra: 5 Basic Operations • Selection ( ) Selects a subset of rows from relations. (horizontal) • Projection ( p ) Retains only wanted columns from (vertical) relation. • Cross-product ( ) Allows us to combine two relations. • Set-difference ( — ) Tuples in r1, but not in r2. • Union ( ) Tuples in r1 and/or in r2. Since each operation returns a relation, operations can be composed! (Algebra is “closed”.) 5 Ch4-1: Relational Algebra S. Pisitkasem Example Instances R1 sid bid day 22 101 10/10/96 58 103 11/12/96 S1 sid sname rating age 22 dustin 7 45.0 bid bname color 31 lubber 8 55.5 101 Interlake blue 102 Interlake red 58 rusty 10 35.0 103 Clipper green 104 Marine red S2 sid sname rating age Boats 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 6 Ch4-1: Relational Algebra S. Pisitkasem Projection •Examples: p age(S2) p sname,rating(S2) • Retains only attributes that are in the “projection list”. • Schema of result: – exactly the fields in the projection list, with the same names that they had in the input relation. • Projection operator has to eliminate duplicates (How do they arise? Why remove them?) – Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it. (Why not?) 7 Ch4-1: Relational Algebra S. Pisitkasem sname rating Projection yuppy 9 lubber 8 guppy 5 rusty 10 sid sname rating age p sname,rating (S 2) 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 age 58 rusty 10 35.0 35.0 S2 55.5 p age(S2) 8 Ch4-1: Relational Algebra S. Pisitkasem Selection () • Selects rows that satisfy selection condition (=,<,>,NOT,AND, OR) • Result is a relation. Schema of result is same as that of the input relation. • Do we need to do duplicate elimination? sid sname rating age sname rating 28 yuppy 9 35.0 yuppy 9 31 lubber 8 55.5 44 guppy 5 35.0 rusty 10 58 rusty 10 35.0 rating 8(S2) p sname,rating( rating 8(S2)) 9 Ch4-1: Relational Algebra S. Pisitkasem Union and Set-Difference • All of these operations take two input relations, which must be union-compatible: – Same number of fields. – `Corresponding’ fields have the same type. • For which, if any, is duplicate elimination required? 10 Ch4-1: Relational Algebra S. Pisitkasem Union sid sname rating age sid sname rating age 22 dustin 7 45.0 22 dustin 7 45.0 31 lubber 8 55.5 31 lubber 8 55.5 58 rusty 10 35.0 58 rusty 10 35.0 44 guppy 5 35.0 S1 28 yuppy 9 35.0 sid sname rating age S1 S2 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 S2 11 Ch4-1: Relational Algebra S. Pisitkasem Set Difference sid sname rating age sid sname rating age 22 dustin 7 45.0 22 dustin 7 45.0 31 lubber 8 55.5 S1 S2 58 rusty 10 35.0 S1 sid sname rating age sid sname rating age 28 yuppy 9 35.0 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 44 guppy 5 35.0 58 rusty 10 35.0 S2 – S1 S2 12 Ch4-1: Relational Algebra S. Pisitkasem Cross-Product • S1 R1: Each row of S1 paired with each row of R1. • Q: How many rows in the result? • Result schema has one field per field of S1 and R1, with field names `inherited’ if possible. – May have a naming conflict: Both S1 and R1 have a field with the same name. – In this case, can use the renaming operator: (C(1 sid1, 5 sid2), S1 R1) 13 Ch4-1: Relational Algebra S. Pisitkasem Cross Product Example sid bid day sid sname rating age 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 58 rusty 10 35.0 R1 S1 (sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 R1 X S1 = 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96 14 Ch4-1: Relational Algebra S. Pisitkasem Compound Operator: Intersection • In addition to the 5 basic operators, there are several additional “Compound Operators” – These add no computational power to the language, but are useful shorthands. – Can be expressed solely with the basic ops. • Intersection takes two input relations, which must be union-compatible. • Q: How to express it using basic operators? R S = R (R S) 15 Ch4-1: Relational Algebra S. Pisitkasem Intersection sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid sname rating age 31 lubber 8 55.5 S1 58 rusty 10 35.0 sid sname rating age 28 31 yuppy lubber 9 8 35.0 55.5 S1 S2 44 guppy 5 35.0 58 rusty 10 35.0 S2 16 Ch4-1: Relational Algebra S. Pisitkasem Compound Operator: Join • Joins are compound operators involving cross product, selection, and (sometimes) projection. • Most common type of join is a “natural join” (often just called “join”). R S conceptually is: – Compute R S – Select rows where attributes that appear in both relations have equal values – Project all unique atttributes and one copy of each of the common ones. • Note: Usually done much more efficiently than this. • Useful for putting “normalized” relations back together. 17 Ch4-1: Relational Algebra S. Pisitkasem Natural Join Example sid bid day sid sname rating age 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 58 rusty 10 35.0 R1 S1 R1 S1 = sid sname rating age bid day 22 dustin 7 45.0 101 10/10/96 58 rusty 10 35.0 103 11/12/96 18 Ch4-1: Relational Algebra S. Pisitkasem Other Types of Joins • Condition Join (or “theta-join”): R c S = c ( R S) (sid) sname rating age (sid) bid day 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 58 103 11/12/96 S1 < R1.sid R1 S1.sid • Result schema same as that of cross-product. • May have fewer tuples than cross-product. • Equi-Join: Special case: condition c contains only conjunction of equalities. 19 Ch4-1: Relational Algebra S. Pisitkasem Semijoin Applications in distributed database: • Product(pid,cid,pname,…) at site 1 • Company(cid,cname,…) at site 2 •Query: Find product which price > 1000 which company producer ay site 2. price>1000(Product) < Company • Compute as follows: T1 = price>1000(Product) site 1 T2 = pcid(T1) site 1 send T2 to site 2 (T2 smaller than T1) T3 = T2 < Company site 2 (semijoin) send T3 to site 1 (T3 smaller than Company) Answer = T1 < T3 site 1 (semijoin) 20 Ch4-1: Relational Algebra S. Pisitkasem Complex Queries Product (pid, name, price, category, maker-cid) Purchase(ssn, storeID ,pid) Company (cid, name, stock_price,country) Person(ssn, name, phone number, city) Store(storeID, name, phone, city) Note • in purchase : ssn, storeID and pid are foreign key in Person, Store and Product • in Product make-cid is a foreign key in Company Query •Find phone numbers of people who bought TV from ‘Bestbuy’ •Find IT products that somebody bought 21 Ch4-1: Relational Algebra S. Pisitkasem Exercises Product (pid, name, price, category, maker-cid) Purchase(ssn, storeID ,pid) Company (cid, name, stock_price,country) Person(ssn, name, phone number, city) Store(storeID, name, phone, city) Ex#1: Find people who bought IT product Ex#2: Find names of people who bought Thai products Ex#3: Find names of people who bought Thai products and did not buy American products Ex#4: Find name of people who bought American products from Depo Ex#5: Which store in his living city which “smith” bought stuff from 22 Ch4-1: Relational Algebra S. Pisitkasem Compound Operator: Division • Useful for expressing “for all” queries like: Find sids of sailors who have reserved all boats. • For A/B attributes of B are subset of attrs of A. – May need to “project” to make this happen. • E.g., let A have 2 fields, x and y; B have only field y: AB = x y B( x, y A) A/B contains all tuples (x) such that for every y tuple in B, there is an xy tuple in A. 23 Ch4-1: Relational Algebra S. Pisitkasem Examples of Division A/B sno pno pno pno pno s1 p1 p2 p2 p1 s1 p2 p4 B1 p2 s1 p3 B2 p4 s1 p4 s2 p1 sno B3 s2 p2 s1 s3 p2 s2 sno s4 p2 s3 s1 sno s4 p4 s4 s4 s1 A A/B1 A/B2 A/B3 24 Ch4-1: Relational Algebra S. Pisitkasem Expressing A/B Using Basic Operators • Division is not essential op; just a useful shorthand. – (Also true of joins, but joins are so common that systems implement joins specially.) • Idea: For A/B, compute all x values that are not `disqualified’ by some y value in B. – x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A. Disqualified x values: p x ((p x ( A) B) A) A/B: p x ( A) Disqualified x values 25 Ch4-1: Relational Algebra S. Pisitkasem sid bid day Reserves Examples 22 101 10/10/96 58 103 11/12/96 sid sname rating age Sailors 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 bid bname color Boats 101 Interlake Blue 102 Interlake Red 103 Clipper Green 104 Marine Red 26 Ch4-1: Relational Algebra S. Pisitkasem Find names of sailors who’ve reserved boat #103 Solution 1: p sname (( Re serves) Sailors) bid =103 Solution 2: p sname ( (Re serves Sailors)) bid =103 Which solution is more efficient ? 27 Ch4-1: Relational Algebra S. Pisitkasem Find names of sailors who’ve reserved a red boat •Information about boat color only available in Boats; so need an extra join; p sname(( color = ‘red’Boats) Reserves Sailors) p sname Sailors color = ‘red’ Reserves Boats 28 Ch4-1: Relational Algebra S. Pisitkasem Find names of sailors who’ve reserved a red boat •A more efficient solution p sname(p sid((p bid color = ‘red’ Boats) Res) Sailors) p sname p sid Sailors p bid Reserves color = ‘red’ Boats A query optimizer can find this given the first solution! 29 Ch4-1: Relational Algebra S. Pisitkasem Find names of sailors who’ve reserved a red or a green boat •Can identify all red or green boats, then find sailors who’ve reserved one of these boats: (Tempboats,( color = ‘red’ V color = ‘green’ Boats)) p sname (Tempboats Reserves Sailors) Can also define Tempboats using union! (How?) 30 Ch4-1: Relational Algebra S. Pisitkasem Find names of sailors who’ve reserved a red and a green boat • Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors): (Tempred , p ( ( Boats) Re serves)) sid color =' red ' (Tempgreen, p (( Boats) Reserves)) sid color =' green' p sname ((Tempred Tempgreen ) Sailors ) 31 Ch4-1: Relational Algebra S. Pisitkasem Find the names of sailors who’ve reserved all boats • Use division; schemas of the input relations to / must be carefully chosen: (Tempsids, (p sid, bid Reserves) / (p bid Boats)) p sname (Tempsids Sailors) To find sailors who’ve reserved all ‘red’ boats: …. / p bid ( color = ‘red’ Boats) 32 Ch4-1: Relational Algebra S. Pisitkasem Relational Algebra • Why bother ? Can write any RA expression directly in C++/Java , seem easy. • Two reasons: - Each Operator admits sophisticated implementations (think of , C) - Expressions in relational algebra can be rewritten : optimized 33 Ch4-1: Relational Algebra S. Pisitkasem Efficient Implementations of Operators • (age>=30 AND age<=35)(Employees) - Method 1 : scan the file,test each employee - Method 2 : use an index on age - Which one is better ? Well, depends…. • Employees Relatives - Iterate over Employees, then over Relatives - Iterate over Relatives, then over Employees - Sort Employees, Relatives, do “merge-join” - “hash-join” - etc 34 Ch4-1: Relational Algebra S. Pisitkasem Optimizations Product (pid, name, price, category, maker-cid) Purchase(ssn, storeID ,pid) Person(ssn, name, phone number, city) • Which is better : price>100(Product) (Purchase city=sea People) (price>100(Product) Purchase) city=sea People • Depends! This is the optimizer’s job… 35 Ch4-1: Relational Algebra S. Pisitkasem Finally : RA has Limitations! • Cannot compute “transitive closure” Name1 Name2 Name3 Fred Mary Father Mary Joe Cousin Mary Bill Spouse Nancy Lou Sister • Find all direct and indirect relatives of Fred • Cannot express in RA!!! Need to write C program 36 Ch4-1: Relational Algebra S. Pisitkasem Summary • Relational Algebra: a small set of operators mapping relations to relations – Operational, in the sense that you specify the explicit order of operations – A closed set of operators! Can mix and match. • Basic ops include: s, p, , , — • Important compound ops: , , / 37 Ch4-1: Relational Algebra S. Pisitkasem