rela onal algebra query processing
play

rela%onal algebra Query Processing 3 steps: Parsing & - PowerPoint PPT Presentation

rela%onal algebra Query Processing 3 steps: Parsing & Translation Optimization Evaluation 30 Relational Algebra Simple set of algebraic operations on relations Journey of a query SQL


  1. rela%onal ¡ algebra ¡

  2. Query Processing 3 steps: • Parsing & Translation • Optimization • Evaluation 30 ¡

  3. Relational Algebra • Simple set of algebraic operations on relations Journey ¡of ¡a ¡query ¡ ¡ SQL ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡select ¡… ¡from…where ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Rela%onal ¡algebra ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ π 13 (P ⨝ Q) ¡ ⨝ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Query ¡rewri%ng ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ π 14 (P ⨝ S) ¡ ⨝ ¡Q ¡ ⨝ ¡R ¡ ¡ ¡ ¡ ¡ ¡ We ¡use ¡ set ¡ seman/cs ¡(no ¡duplicates) ¡and ¡ no ¡nulls ¡ • There ¡are ¡extensions ¡with ¡bag ¡seman%cs ¡and ¡nulls ¡ • ¡ 31 ¡

  4. Relational Algebra Projection Eliminate some columns π X (R) Display only attributes X of relation R where R: table name & X ⊆ attributes(R) Example: Find titles of current movies π TITLE (SCHEDULE) 32 ¡

  5. Relational Algebra Projection Eliminate some columns π X (R) Display only attributes X of relation R where R: table name & X ⊆ attributes(R) Example: R π A (R) = π AB (R) = No repetitions A B C A A B of tuples! 0 1 2 0 0 1 0 2 2 1 0 2 1 3 1 1 3 0 1 3 33 ¡

  6. Relational Algebra Selection Compute set union σ cond (R) Select tuples of R satisfying condition cond where cond: condition involving only attributes of R (e.g., attr = value, attr ≠ value, attr1 = attr2, attr1 ≠ attr2, etc.) Example: R σ Α =0 (R) = σ B=C (R) = A A B C A B C B C 0 1 2 0 1 2 0 2 2 0 2 2 0 2 2 1 3 1 0 1 3 0 1 3 34 ¡

  7. Relational Algebra Selection Compute set union σ cond (R) Select tuples of R satisfying condition cond where cond: condition involving only attributes of R (e.g., attr = value, attr ≠ value, attr1 = attr2, attr1 ≠ attr2, etc.) Example: R σ Α≠ 0 (R) = A B C A B C 0 1 2 1 3 1 0 2 2 1 3 1 0 1 3 35 ¡

  8. Relational Algebra Union Compute set union R ∪ S Union of sets of tuples in R and S where R, S: tables with same attributes Example: R S R ∪ S = A B A B A B α 1 α 2 α 1 α 2 β 3 α 2 β 1 β 1 β 3 36 ¡

  9. Relational Algebra Difference Compute set difference R - S Difference of sets of tuples in R and S where R, S: tables with same attributes Example: R S R - S = A B A B A B α 1 α 2 α 1 α 2 β 3 β 1 β 1 37 ¡

  10. Relational Algebra Join Compute join R ⨝ S Natural Join of R, S where R, S: tables Example: R S R ⨝ S = A B B C A B C Note: More than one common attributes allowed! 38 ¡

  11. Relational Algebra Join Compute join R ⨝ S Natural Join of R, S where R, S: tables Example: R S R ⨝ S = A B B C A B C 0 1 1 2 0 1 2 0 2 1 3 0 1 3 5 3 2 2 0 2 2 39 ¡

  12. Definition of Join Let r and s be relations on schemas R and S respectively. Then, r ⨝ s is a relation with attributes att( R) ∪ att( S) obtained as follows: Consider each pair of tuples t r from r and t s from s . If t r and t s have the same value on each of the attributes in att( R) ∩ att( S) , add a tuple t to the result, where • t has the same value as t r on r • t has the same value as t s on s Note: if R ∩ S is empty, the join consists of all combinations of tuples from R and S, i.e. their cross-product

  13. Relational Algebra Attribute Renaming Rename attributes δ A1 → A2 (R) Change name of attribute A1 in rel. R to A2 where R: relation and A1: attribute in R Example: R δ A → C (R) = C Contents remain A B B unchanged! α 1 α 1 α 2 α 2 β 1 β 1 Note: Can rename several attributes at once 41 ¡

  14. Relational Algebra • Basic set of operations: π , σ , ∪ , -, ⨝ , δ • Back to movie example queries: 1. Titles of currently playing movies: π TITLE (schedule) 2. Titles of movies by Berto: π TITLE ( σ DIR=BERTO (movie)) 3. Titles and directors of currently playing movies: π TITLE, DIR (movie ⨝ schedule) 42 ¡

  15. Relational Algebra 4. Find the pairs of actors acting together in some movie π actor1, actor2 ( δ actor à actor1 (movie) ⨝ δ actor à actor2 (movie)) 5. Find the actors playing in every movie by Berto π actor (movie) – π actor [( π actor (movie) ⨝ π title ( σ dir =BERTO (movie))) - π actor,title (movie)] actor ¡ %tle ¡by ¡Berto ¡ actor ¡acts ¡in ¡%tle ¡ actors ¡ ¡for ¡which ¡there ¡is ¡a ¡movie ¡by ¡Berto ¡in ¡which ¡they ¡do ¡not ¡act ¡ In ¡this ¡case ¡(not ¡in ¡general): ¡Same ¡as ¡cartesian ¡product ¡ 43 ¡

  16. Relational Algebra Cartesian Product Compute cartesian product R × S Cartesian Product of R, S where R, S: tables Example: R S R ⨝ S = A B C D A B C D 0 1 1 2 0 1 1 2 0 2 1 3 0 1 1 3 0 2 1 2 0 2 1 3 Same as R ⨝ S, when R and S have no common attributes 44 ¡

  17. Relational Algebra Cartesian Product Compute cartesian product R × S Cartesian Product of R, S where R, S: tables Example: R S R ⨝ S = A B A C R.A B S.A C 0 1 1 2 0 1 1 2 0 2 1 3 0 1 1 3 0 2 1 2 If 2 attributes in R, S have the same 0 2 1 3 name A, they are renamed to R.A and S.A in the output 45 ¡

  18. Other useful operations • Intersection R ∩ S • Division (Quotient) R ÷ S R S A B B R ÷ S: {a | <a, b> ∈ R for every b ∈ S} Example: R S R ÷ S = A B B Α 0 α α 0 0 β β 1 1 α 1 β 1 γ 2 α 46 ¡

  19. Another Division Example ■ Find the actors playing in every movie by Berto � π TITLE, ACTOR (movie ) ÷ π TITLE ( σ DIR=BERTO (movie))

  20. Division by multiple attributes ■ Relations r, s : � r � A � B � C � D � E � D � E � s � a � 1 � α � a � α � a � 1 � b � 1 � α � a � γ � a � 1 � α � a � γ � b � 1 � β � a � γ � a � 1 � β � a � γ � b � 3 � γ � a � γ � a � 1 � γ � a � γ � b � 1 � γ � a � β � b � 1 � ■ r ÷ s : � A � B � C � α � a � γ � γ � a � γ �

  21. Relational Algebra • Note: π is like ∃ “there exists”… ÷ is like ∀ “for all”… • Expressing ÷ using other operators: R ¡ ÷ ¡S ¡= ¡ π A (R) ¡-­‑ ¡ π A (( π A (R) ¡ ⨝ ¡S) ¡-­‑ ¡R) ¡ R A B S B Similar to: ∀ x ϕ (x) ≡ ¬ ∃ x ¬ ϕ (x) 49 ¡

  22. Relational Algebra Join Operators Compute different types of joins R ⨝ S Natural Join R ⟕ S Left Outer Join R ⟖ S Right Outer Join R ⟗ S Full Outer Join R ⨝ θ S Theta Join: Same as σ θ (R × S) 50 ¡

  23. Calculus Vs. Algebra • Theorem: Calculus and Algebra are equivalent • Basic Correspondence: Algebra Operation Calculus Operation π ∃ t(A) comp c σ ∨ ∪ ∧ ⨝ ¬ - ∀ ÷ 51 ¡

  24. Example • “Find theaters showing movies by Bertolucci”: SQL: • SELECT s.theater FROM schedule s, movie m WHERE s.title = m.title AND m.director = ‘Berto’ tuple calculus: • { t: theater | ∃ s ∈ schedule ∃ m ∈ movie [ t(theater) = s(theater) ∧ s(title) = m(title) ∧ m(director) = Berto ] } relational algebra: π theater (schedule ⨝ σ dir = Berto (movie)) Note: ¡number ¡of ¡items ¡in ¡FROM ¡clause ¡= ¡ ¡(number ¡of ¡joins ¡+ ¡1) ¡ 52 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend