Download presentation
Presentation is loading. Please wait.
1
CS 4432query processing - lecture 121 CS4432: Database Systems II Lecture #12 Query Processing Professor Elke A. Rundensteiner
2
CS 4432query processing - lecture 122 Query Processing Query in SQL Query Plan in Algebra
3
CS 4432query processing - lecture 123 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 processing - lecture 124 RABC S CDE a11010x2 b12020y2 c21030z2 d23540x1 e34550y3 AnswerB D 2 x
5
CS 4432query processing - lecture 125 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 processing - lecture 126 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)]
7
CS 4432query processing - lecture 127 Another idea: B,D R.A = “c” S.E = 2 R S Plan II natural join
8
CS 4432query processing - lecture 128 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
9
CS 4432query processing - lecture 129 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
10
CS 4432query processing - lecture 1210 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:
11
CS 4432query processing - lecture 1211 Overview of Query Optimization
12
CS 4432query processing - lecture 1212 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
13
CS 4432query processing - lecture 1213 Example: SQL query SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ ); (Find the movies with stars born in 1960)
14
CS 4432query processing - lecture 1214 Example: Parse Tree SELECT FROM WHERE IN title StarsIn ( ) starName SELECT FROM WHERE LIKE name MovieStar birthDate ‘%1960’
15
CS 4432query processing - lecture 1215 Example: Generating Relational Algebra title StarsIn IN name birthdate LIKE ‘%1960’ starName MovieStar Fig. 7.15: An expression using a two-argument , midway between a parse tree and relational algebra
16
CS 4432query processing - lecture 1216 Example: Logical Query Plan title starName=name StarsIn name birthdate LIKE ‘%1960’ MovieStar Fig. 7.18: Applying the rule for IN conditions
17
CS 4432query processing - lecture 1217 Example: Improved Logical Query Plan title starName=name StarsIn name birthdate LIKE ‘%1960’ MovieStar Fig. 7.20: An improvement on fig. 7.18. Question: Push project to StarsIn?
18
CS 4432query processing - lecture 1218 Example: Estimate Result Sizes Need expected size StarsIn MovieStar
19
CS 4432query processing - lecture 1219 Example: One Physical Plan Parameters: join order, memory size, project attributes,... Hash join SEQ scanindex scan Parameters: Select Condition,... StarsInMovieStar
20
CS 4432query processing - lecture 1220 Example: Estimate costs L.Q.P P1 P2 …. Pn C1 C2 …. Cn Pick best!
21
CS 4432query processing - lecture 1221 Query Optimization Relational algebra level (NEXT) … Detailed query plan level –Estimate Costs without indexes with indexes –Generate and compare plans
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.