Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 4432query processing1 CS4432: Database Systems II.

Similar presentations


Presentation on theme: "CS 4432query processing1 CS4432: Database Systems II."— Presentation transcript:

1 CS 4432query processing1 CS4432: Database Systems II

2 CS 4432query processing2 Query Processing Query in SQL  Query Plan in Algebra

3 CS 4432query processing3 Example Data: relation R (A, B, C) relation S (C, D, E) Query: SELECT B, D FROM R, S WHERE R.A = “c” and S.E = 2 and R.C=S.C

4 CS 4432query processing4 RABC S CDE a11010x2 b12020y2 c21030z2 d23540x1 e34550y3 AnswerB D 2 x SELECT B, D FROM R, S WHERE R.A = “c” and S.E = 2 and R.C=S.C

5 CS 4432query processing5 How do we execute query? - Form Cartesian product of all tables in FROM-clause - Select tuples that match WHERE-clause - Project columns that occur in SELECT-clause One idea

6 CS 4432query processing6 R X SR.AR.BR.CS.CS.DS.E a 1 10 10 x 2 a 1 10 20 y 2. C 2 10 10 x 2. Bingo! Got one...

7 CS 4432query processing7 But ? Performance would be unacceptable! We need a better approach to reasoning about queries, their execution orders and their respective costs

8 CS 4432query processing8 Formal Relational Query Languages Two mathematical Query Languages form basis for “real” languages (e.g. SQL), and for implementation: –Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non-operational, declarative.) –Relational Algebra: More operational, very useful for representing execution plans.

9 CS 4432query processing9 Relational Algebra Tuple : ordered set of data values Relation: a set of tuples Algebra: formal mathematical system consisting of a set of objects and operations on those objects Relational algebra: Algebra whose objects are relations and operators transform relations into other relations

10 CS 4432query processing10 Relational Algebra ? Query: SELECT B, D FROM R, S WHERE R.A = “c” and S.E = 2 and R.C=S.C

11 CS 4432query processing11 Relational Algebra - can be used to describe plans... Ex: Plan I  B,D  R.A =“c”  S.E=2  R.C=S.C  X RS OR:  B,D [  R.A=“c”  S.E=2  R.C = S.C (RXS)]

12 CS 4432query processing12 Example Instances R1 S1 S2 “Sailors” and “Reserves” relations

13 CS 4432query processing13 Relational Algebra Basic operations: – Selection ( ) Selects a subset of rows from relation. – Projection ( ) Deletes unwanted columns from relation. – Cross-product ( ) Allows us to combine two relations. – Set-difference ( ) Tuples in reln. 1, but not in reln. 2. – Union ( ) Tuples in reln. 1 and in reln. 2. Additional operations: – Intersection, join, division, renaming: Not essential ! Algebra is “closed”: Since each operation returns a relation, operations can be composed !

14 CS 4432query processing14 Projection

15 CS 4432query processing15 Selection

16 CS 4432query processing16 Union, Intersection, Set Difference Operate on two union- compatible relations: – Same number of fields. – `Corresponding’ fields have same type.

17 CS 4432query processing17 Cross-Product Each row of S1 is paired with each row of R1.  Conflict: Both S1 and R1 have a field called sid.  Renaming operator :

18 CS 4432query processing18 Joins Condition Join : Result schema same as that of cross-product.

19 CS 4432query processing19 Joins Equi-Join: condition contains only equalities. Result schema only one copy of fields for which equality is specified. Natural Join: Equijoin on all common fields.

20 CS 4432query processing20 Division Not primitive operator, but useful: Find sailors who have reserved all boats. A has 2 fields x and y; B has only field y: – A/B = – i.e., A/B contains all x tuples (sailors) such that for every y tuple (boat) in B, there is an xy tuple in A.

21 CS 4432query processing21 Examples of Division A/B A B1 B2 B3 A/B1A/B2A/B3

22 CS 4432query processing22 Expressing A/B Using Basic Operators Division is useful shorthand. Idea: For A/B, compute all x values that are not `disqualified’ by some y value in B. Disqualified x values: A/B: all disqualified tuples

23 CS 4432query processing23 Find names of sailors who’ve reserved boat #103  Solution 2 :  Solution 3 : Solution 1 :

24 CS 4432query processing24 Find names of sailors who’ve reserved a red boat Information about boat color only available in Boats; so need an extra join:  A more efficient solution: A query optimizer can find this, given the first solution!

25 CS 4432query processing25 Find 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:  Can also define Tempboats using union! (How?)  What happens if is replaced by in this query?

26 CS 4432query processing26 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 (sid is a key for Sailors):

27 CS 4432query processing27 Find names of sailors who’ve reserved all boats Uses division; schemas of input relations to / must be carefully chosen:  To find sailors who’ve reserved all ‘Interlake’ boats:.....

28 CS 4432query processing28 Relational Algebra representation used to describe plans...

29 CS 4432query processing29 Example Data: relation R (A, B, C) relation S (C, D, E) Query: SELECT B, D FROM R, S WHERE R.A = “c” and S.E = 2 and R.C=S.C

30 CS 4432query processing30 RABC S CDE a11010x2 b12020y2 c21030z2 d23540x1 e34550y3 AnswerB D 2 x

31 CS 4432query processing31 Relational Algebra - to describe plan Ex: Plan I  B,D  R.A =“c”  S.E=2  R.C=S.C  X RS OR:  B,D [  R.A=“c”  S.E=2  R.C = S.C (RXS)]

32 CS 4432query processing32 Another idea:  B,D  R.A = “c”  S.E = 2 R S Plan II natural join

33 CS 4432query processing33 R S A B C  ( R )  ( S ) C D E a 1 10 A B C C D E 10 x 2 b 1 20c 2 10 10 x 2 20 y 2 c 2 10 20 y 2 30 z 2 d 2 35 30 z 2 40 x 1 e 3 45 50 y 3 SELECT B,D FROM R,S WHERE R.A = “c” and S.E = 2 and R.C=S.C

34 CS 4432query processing34 Yet another idea:  B,D  S.E = 2  R.A = “c” R S Plan III natural join

35 CS 4432query processing35 Plan III Use R.A and S.C Indexes (1) Use R.A index to select R tuples with R.A = “c” (2) For each R.C value found, use S.C index to find matching tuples (3) Eliminate S tuples S.E  2 (4) Join matching R,S tuples, project B,D attributes and place in result

36 CS 4432query processing36 R S A B C C D E a 1 10 10 x 2 b 1 20 20 y 2 c 2 10 30 z 2 d 2 35 40 x 1 e 3 45 50 y 3 AC I1I1 I2I2 =“c” check=2? output: next tuple:

37 CS 4432query processing37 Overview of Query Optimization

38 CS 4432query processing38 parse convert apply laws estimate result sizes consider physical plans estimate costs pick best execute {P1,P2,…..} {(P1,C1),(P2,C2)...} Pi answer SQL query parse tree logical query plan “improved” l.q.p l.q.p. +sizes statistics

39 CS 4432query processing39 Example: SQL query Query : Find the movies with stars born in 1960 SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ );

40 CS 4432query processing40 Example: Parse Tree SELECT FROM WHERE IN title StarsIn ( ) starName SELECT FROM WHERE LIKE name MovieStar birthDate ‘%1960’

41 CS 4432query processing41 Example: Generating Relational Algebra  title  StarsIn IN  name  birthdate LIKE ‘%1960’ starName MovieStar Fig. 16.14: An expression using a two-argument , midway between a parse tree and relational algebra

42 CS 4432query processing42 Example: Logical Query Plan  title  starName=name StarsIn  name  birthdate LIKE ‘%1960’ MovieStar Fig. 16.16: Applying the rule for IN conditions 

43 CS 4432query processing43 Example: Improved Logical Query Plan  title starName=name StarsIn  name  birthdate LIKE ‘%1960’ MovieStar Fig. 16.16: An improvement on prev fig Question: Push project to StarsIn?

44 CS 4432query processing44 Example: Estimate Result Sizes Need expected size StarsIn MovieStar 

45 CS 4432query processing45 Example: One Physical Plan Parameters: join order, memory size, project attributes,... Hash join SEQ scanindex scan Parameters: Select Condition,... StarsInMovieStar

46 CS 4432query processing46 Example: Estimate costs L.Q.P P1 P2 …. Pn C1 C2 …. Cn Pick best!

47 CS 4432query processing47 Query Optimization Relational algebra level … Detailed query plan level … –Estimate costs –Generate and compare plans


Download ppt "CS 4432query processing1 CS4432: Database Systems II."

Similar presentations


Ads by Google