Query Processing and Relational Algebra in Database Systems
"Explore the execution of queries through concepts like Cartesian product, projection, and relational algebra. Learn how to optimize query plans and understand query processing in database systems."
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Query Processing Q Query Plan Example: R(A,B,C), S(C,D,E) Select B,D From R,S Where R.A= c S.E=2 R.C=S.C 1
How do we execute query? - Do Cartesian product - Select tuples - Do projection One idea 2
Relational Algebra - can be used to Ex: Plan I B,D R.A= c S.E=2 R.C=S.C describe plans... R X S OR: B,D [ R.A= c S.E=2 R.C = S.C (RXS)] 3
Another idea: Plan II B,D natural join R.A = c S.E = 2 R S 4
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 5
R S = c A C I1 C D E A B C I2 a 1 10 10 x 2 <c,2,10> b 1 20 20 y 2 c 2 10 30 z 2 d 2 35 40 x 1 e 3 45 50 y 3 6
R S = c A C I1 C D E A B C I2 a 1 10 10 x 2 <c,2,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 7
R S = c A C I1 C D E A B C I2 a 1 10 10 x 2 <c,2,10><10,x,2> b 1 20 20 y 2 check=2? c 2 10 30 z 2 d 2 35 40 x 1 output: <2,x> e 3 45 50 y 3 8
SQL query parse parse tree convert answer logical query plan execute apply laws statistics Pi improved l.q.p pick best estimate result sizes {(P1,C1),(P2,C2)...} l.q.p. +sizes estimate costs consider physical plans {P1,P2, ..} 10
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) 11
Example: Parse Tree <Query> <SFW> SELECT <SelList> FROM <FromList> WHERE <Condition> <Attribute> <RelName> <Tuple> IN <Query> title StarsIn <Attribute> ( <Query> ) starName <SFW> SELECT <SelList> FROM <FromList> WHERE <Condition> <Attribute> <RelName> <Attribute> LIKE <Pattern> name MovieStar birthDate %1960 12
Example: Generating Relational Algebra title StarsIn <condition> <tuple> IN name <attribute> birthdate LIKE %1960 starName MovieStar Fig. 7.15: An expression using a two-argument , midway between a parse tree and relational algebra 13
Example: Logical Query Plan title starName=name StarsIn name birthdate LIKE %1960 MovieStar Fig. 7.18: Applying the rule for IN conditions 14
Example: Improved Logical Query Plan title Question: Push project to StarsIn? starName=name StarsIn name birthdate LIKE %1960 MovieStar Fig. 7.20: An improvement on fig. 7.18. 15
Example: Estimate Result Sizes Need expected size StarsIn MovieStar 16
Example: One Physical Plan Parameters: join order, memory size, project attributes,... Hash join Parameters: Select Condition,... SEQ scan index scan StarsIn MovieStar 17
Example: Estimate costs L.Q.P P1 P2 . Pn C1 C2 . Pick best! Cn 18
Query Optimization Relational algebra level Detailed query plan level Estimate Costs without indexes with indexes Generate and compare plans 19
Which are good transformations? p1 p2 (R) p1 [ p2 (R)] p (R S) [ p (R)] S R S S R x [ p(R)] x { p [ xz(R)]} 20
Conventional wisdom: do projects early Example: R(A,B,C,D,E) x={E} P: (A=3) (B= cat ) x { p(R)} vs. E { p{ ABE(R)}} Usually good: early selections 21
Outline - Query Processing Relational algebra level transformations good transformations Detailed query plan level estimate costs generate and compare plans 22
Estimating cost of query plan (1) Estimating size of results (2) Estimating # of IOs 23