Presentation is loading. Please wait.

Presentation is loading. Please wait.

R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY.

Similar presentations


Presentation on theme: "R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY."— Presentation transcript:

1 R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY

2 22

3 33

4 44 R ELATIONAL A LGEBRA - D IVISION

5 55

6 66

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

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

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

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

11 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 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 13 D IVISION E XAMPLE Step 5) Subtract from Completed π Student (Completed) - π Student ( π Student (Completed) × DBProject-Completed) Completed/DBProject=

14 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 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

16 D ATABASE S YSTEMS I Q UERY O PTIMIZATION

17 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 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 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 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 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 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 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 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 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.

26 D ATABASE S YSTEMS I R ELATIONAL C ALCULUS

27 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 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 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 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 31 S ELECTION AND P ROJECTION Find all sailors with rating above 7 {S |S  Sailors  S.rating > 7}

32 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 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 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.


Download ppt "R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY."

Similar presentations


Ads by Google