R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
Advertisements

Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Relational Algebra Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
D ATABASE S YSTEMS I A DMIN S TUFF. 2 Mid-term exam Tuesday, Oct 2:30pm Room 3005 (usual room) Closed book No cheating, blah blah No class on Oct.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus.
1 Lecture 5: Relational calculus
Relational Calculus CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth   We will occasionally use this arrow notation unless there is danger of.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Relational Calculus R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham Elements of Ramsey.
Introduction to Database Systems 1 Relational Calculus Relational Model : Topic 2.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Calculus CS 186, Fall 2003, Lecture 6 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
Rutgers University Relational Calculus 198:541 Rutgers University.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Database Management Systems,1 Relational Calculus.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Relational Calculus R&G, Chapter 4. Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus.
Relational Calculus CS 186, Spring 2005, Lecture 9 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
1 Relational Algebra. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational model supports.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra.
Relational Algebra Chapter 4 - part I.
Relational Calculus.
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B 7/1/2019.
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY

22

33

44 R ELATIONAL A LGEBRA - D IVISION

55

66

77 Queries that give the same result as division are not replacements for division

88 D IVISION E XAMPLE Want to perform Completed/DBProject to find all students who completed all projects.

99 D IVISION E XAMPLE Step 1) Project Completed onto it’s unique attributes π Student (Completed)

10 D IVISION E XAMPLE Step 2) Perform Cartesian product with DBProject π Student (Completed) × DBProject Every student is combined with every task.

11 D IVISION E XAMPLE Step 3) Subtract Completed π Student (Completed) × DBProject-Completed We have the possible combinations that "could have" been, but weren't.

12 D IVISION E XAMPLE Step 4) Project onto the unique attributes of Completed π Student ( π Student (Completed) × DBProject-Completed) All students who have not completed all assignments.

13 D IVISION E XAMPLE Step 5) Subtract from Completed π Student (Completed) - π Student ( π Student (Completed) × DBProject-Completed) Completed/DBProject=

14 Find sailors who’ve reserved a red and a green boat. 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): E XAMPLE Q UERIES

15 Find the names of sailors who’ve reserved all boats. Uses division; schemas of the input relations must be carefully chosen: To find sailors who’ve reserved all ‘Interlake’ boats: Book has lots of examples. E XAMPLE Q UERIES

D ATABASE S YSTEMS I Q UERY O PTIMIZATION

17 P RINCIPLES OF Q UERY O PTIMIZATION 1. Display the minimum number of fields in a query. 2. Use primary key or indexes wherever possible. 3. Use numeric rather than text primary keys. 4. Use non blank unique fields. 5. Avoid domain aggregate functions such as Dlookup(). 6. Use between and equal to, rather than > or <. It will speed up the queries. 7. Use count(*) rather than count(column). 8. Short table and field names run faster than long name. 9. Normalize the tables. 10. Avoid the use of distinct row queries.

18 Q UERY O PTIMIZATION A user of a commercial DBMS formulates SQL queries The query optimizer translates this query into an equivalent RA query, i.e. an RA query with the same result In order to optimize the efficiency of query processing, the query optimizer can re-order the individual operations within the RA query Re-ordering has to preserve the query semantics and is based on RA equivalences Just like Math operations can be reordered, so can Relational Algebra operations

19 Q UERY O PTIMIZATION Why can re-ordering improve the efficiency? Different orders can imply different sizes of the intermediate results The smaller the intermediate results, the more efficient Example: much (!) more efficient than Why?

20 R ELATIONAL A LGEBRA E QUIVALENCES The most important RA equivalences are commutative and associative laws. A commutative law about some operation states that the order of (two) arguments does not matter. An associative law about some (binary) operation states that (more than two) arguments can be grouped either from the left or from the right. If an operation is both commutative and associative, then any number of arguments can be (re-)ordered in an arbitrary manner.

21 R ELATIONAL A LGEBRA E QUIVALENCES The following (binary) RA operations are commutative and associative: For example, we have: Proof method: show that each tuple produced by the expression on the left is also produced by the expression on the right and vice versa. (R S) (S R) (Commutative) R (S T) (R S) T (Associative)  

22 R ELATIONAL A LGEBRA E QUIVALENCES Selections are crucial from the point of view of query optimization, because they typically reduce the size of intermediate results by a significant factor. Laws for selections only: (Splitting) (Commutative)

23 R ELATIONAL A LGEBRA E QUIVALENCES Laws for the combination of selections and other operations: if R has all attributes mentioned in c if S has all attributes mentioned in c The above laws can be applied to “push selections down” as much as possible in an expression, i.e. performing selections as early as possible.

24 R ELATIONAL A LGEBRA E QUIVALENCES A projection commutes with a selection that only uses attributes retained by the projection. Selection between attributes of the two arguments of a Cartesian product converts Cartesian product to a join. Similarly, if a projection follows a join R S, we can ‘push’ it by retaining only attributes of R (and S) that are needed for the join or are kept by the projection.

25 S UMMARY Several ways of expressing a given query; a query optimizer chooses the most efficient version. Query optimization exploits RA equivalencies to re-order the operations within an RA expression. Optimization criterion is to minimize the size of intermediate relations.

D ATABASE S YSTEMS I R ELATIONAL C ALCULUS

27 R ELATIONAL C ALCULUS Nonprocedural Describes the set of answers without saying how they should be computed Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus has variables, constants, comparison ops, logical connectives and quantifiers.

28 T UPLE R ELATIONAL C ALCULUS Query has the form: {T | p(T)} p(T) denotes a formula in which tuple variable T appears. Answer is the set of all tuples T for which the formula p(T) evaluates to true. Formula is recursively defined: start with simple atomic formulas (get tuples from relations or make comparisons of values) build bigger and better formulas using the logical connectives.

29 D OMAIN R ELATIONAL C ALCULUS Query has the form: Answer includes all tuples that make the formula be true. Formula is recursively defined, starting with simple atomic formulas (getting tuples from relations or making comparisons of values), and building bigger and better formulas using the logical connectives.

30 TRC F ORMULAS An Atomic formula is one of the following: R  Rel ( R is a tuple in relation Rel ) R.a op S.b(comparing two fields) R.a op constant(comparing field to constant) op is one of A formula can be: an atomic formula where p and q are formulas where variable R is a tuple variable

31 S ELECTION AND P ROJECTION Find all sailors with rating above 7 {S |S  Sailors  S.rating > 7}

32 J OINS Find sailors rated > 7 who’ve reserved boat #103 {S | S  Sailors  S.rating > 7   R(R  Reserves  R.sid = S.sid  R.bid = 103)} Note the use of  to find a tuple in Reserves that ‘joins with’ the Sailors tuple under consideration.

33 U NSAFE Q UERIES, E XPRESSIVE P OWER It is possible to write syntactically correct calculus queries that have an infinite number of answers! Such queries are called unsafe. e.g.,

34 S UMMARY Relational calculus is non-operational, and users define queries in terms of what they want, not in terms of how to compute it. (Declarativeness.) Algebra and safe calculus have same expressive power, leading to the notion of relational completeness.