第 2 页
Query Processing Declarative SQL Query → Query Plan NOTE: You will not be tested on how well you know SQL. Understanding the SQL introduced in class will be sufficient (a primer follows). SQL is described in Chapter 6, GMUW. Focus: Relational System (i.e., data is organized as tables, or relations)
第 3 页
SQL Primer We will focus on SPJ, or Select-Project-Join Queries Select <attribute list> From <relation list> Where <condition list> Example Filter Query over R(A,B,C): Select B From R Where R.A = “c” R.C > 10
第 4 页
SQL Primer (contd.) We will focus on SPJ, or Select-Project-Join-Queries Select <attribute list> From <relation list> Where <condition list> Example Join Query over R(A,B,C) and S(C,D,E): Select B, D From R, S Where R.A = “c” S.E = 2 R.C = S.C
第 5 页
R A B C S 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 Select B,D Answer B D From R,S Where R.A = “c” 2 x S.E = 2 R.C=S.C
第 6 页
• How do we execute this query? Select B,D From R,S Where R.A = “c” S.E = 2 R.C=S.C - Do Cartesian product One idea - Select tuples - Do projection
第 7 页
R X S R.A R.B R.C S.C S.D S.E Select B,D a 1 10 10 x 2 From R,S a 1 10 20 y 2 Where R.A = “c” S.E = 2 . R.C=S.C . Bingo! c 2 10 10 x 2 Got one... . .
第 8 页
Relational Algebra - can be used to describe plans Ex: Plan I B,D s =“c” R.A S.E=2 R.C=S.C X R S
第 9 页
Relational Algebra Primer (Chapter 5, GMUW) Select: s =“c” R.A R.C=10 Project: B,D Cartesian Product: R X S Natural Join: R S
第 10 页
Relational Algebra - can be used to describe plans Ex: Plan I B,D s =“c” R.A S.E=2 R.C=S.C X R S OR: [ s (RXS)] B,D R.A=“c” S.E=2 R.C = S.C
第 11 页
Another idea: Plan II B,D natural join s s R.A = “c” S.E = 2 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
第 12 页
R S A B C s (R) s(S) C D E a 1 10 A B C C D E 10 x 2 b 1 20 c 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” S.E = 2 R.C=S.C
第 13 页
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
第 14 页
R S A=“c” C A B C I1 I2 C D E a 1 10 10 x 2 <c,2,10> <10,x,2> b 1 20 20 y 2 c 2 10 check=2? 30 z 2 d 2 35 output: <2,x> 40 x 1 e 3 45 50 y 3 c 7 15 next tuple: <c,7,15>
第 15 页
Overview of SQL query Query Processing parse parse tree Query rewriting Query statistics logical query plan Optimization Physical plan generation physical query plan execute Query result Execution
第 17 页
Example: Parse Tree <Query> <SFW> SELECT <SelList> FROM <FromList> WHERE <Cond> <Attribute> <SelList> <RelName> <FromList> <Cond> AND <Cond> B <Attribute> R <RelName> <Attr> <Op> <Const> D S R.A = “c” Select B,D From R,S <Attr> <Op> <Attr> Where R.A = “c” R.C=S.C R.C = S.C
第 18 页
Along with Parsing … • Semantic checks – Do the projected attributes exist in the relations in the From clause? – Ambiguous attributes? – Type checking, ex: R.A > 17.5 • Expand views
第 19 页
SQL query Initial logical plan parse parse tree Rewrite rules Query rewriting Logical plan statistics logical query plan Physical plan generation “Best” logical plan physical query plan execute result
第 20 页
Initial Logical Plan B,D Select B,D s From R,S R.A = “c” Λ R.C = S.C Where R.A = “c” R.C=S.C X R S Relational Algebra: [ s (RXS)] B,D R.A=“c” R.C = S.C
评论 (0)
暂无评论。快来抢沙发吧!