CPS216: Data-Intensive

Computing Systems

Query Processing
(Overview)
Shivnath Babu

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)

B.C > 10 .C): Select B From R Where R. or Select-Project-Join Queries Select <attribute list> From <relation list> Where <condition list> Example Filter Query over R(A.A = “c”  R.SQL Primer We will focus on SPJ.

A = “c”  S.SQL Primer (contd.C . or Select-Project-Join-Queries Select <attribute list> From <relation list> Where <condition list> Example Join Query over R(A. S Where R.C = S.D.C) and S(C.E): Select B.B.E = 2  R.) We will focus on SPJ. D From R.

E = 2  R.D Answer From R.R A B C a 1 10 b 1 c D E 10 x 2 20 20 y 2 2 10 30 z 2 d 2 35 40 x 1 e 3 45 50 y 3 Select B.A = “c”  S.C=S.S Where R.C S C B 2 D x .

E = 2  R.C One idea .Select tuples .• How do we execute this query? Select B.D From R.A = “c”  S.Do Cartesian product .S Where R.Do projection .C=S.

2 10 10 x 2 From R.E = 2  R.A = “c”  S.D R.D S.C=S.E a 1 10 10 x 2 a .A R. .C S. ..B R.C S.C Bingo! Got one.. 1 10 20 y 2 c .S Where R.RXS Select B. .

can be used to describe plans Ex: Plan I B.E=2  R.C X R S .Relational Algebra .C=S.D sR.A=“c” S.

GMUW) Select: sR.A=“c” R.Relational Algebra Primer (Chapter 5.C=10 Project: B.D Cartesian Product: R X S Natural Join: R S .

C=S.Relational Algebra .D [ sR.C = S.C X R S OR: B.E=2  R.E=2  R.A=“c” S.A=“c” S.D sR.can be used to describe plans Ex: Plan I B.C (RXS)] .

A = “c” Select B.C=S.A = “c”  S.D.C sS.E = 2 S(C.C) From R.S Where R.E) natural join .Another idea: Plan II B.D R(A.E = 2  R.D sR.B.

C=S.C .D From R.E = 2  R.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.A = “c”  S.S Where R.

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

2> check=2? output: <2.7.15> .R A B C a 1 10 b 1 20 A=“c” I2 <c.2.x.10> <10.x> e 3 45 c 7 15 C I1 c 2 10 d 2 35 S C D E 10 x 2 20 y 2 30 z 2 40 x 1 50 y 3 next tuple: <c.

Overview of Query Processing SQL query parse parse tree Query rewriting statistics Query Optimization logical query plan Physical plan generation physical query plan execute result Query Execution .

C .S Where R.A = “c”  R.C=S.Example Query Select B.D From R.

S Where R.C = S.C = “c” <Attr> <Op> <Attr> R.A = “c”  R.C=S.D From R.C .A Select B.Example: Parse Tree <Query> <SFW> SELECT <SelList> FROM <FromList> WHERE <Cond> <Attribute> <SelList> <RelName> <FromList> <Cond> AND B <Attribute> R <Cond> <RelName> <Attr> <Op> <Const> D S R.

Along with Parsing … • Semantic checks – Do the projected attributes exist in the relations in the From clause? – Ambiguous attributes? – Type checking. ex: R.5 • Expand views .A > 17.

SQL query parse parse tree Query rewriting statistics Initial logical plan Rewrite rules Logical plan logical query plan Physical plan generation physical query plan execute result “Best” logical plan .

D From R.C = S.A = “c”  R.D [ sR.S Where R.C (RXS)] .A=“c” R.C=S.C = S.C X R S Relational Algebra: B.D Select B.C sR.A = “c” Λ R.Initial Logical Plan B.

A = “c” Λ R.D [ sR.D B.Apply Rewrite Rule (1) B.C sR.A = “c” X R S X R S B.D sR.C=S.C = S.C = S.A=“c”(R X S)]] .C [sR.C sR.

Apply Rewrite Rule (2) B.C [sR.C sR.C sR.D B.C = S.C = S.A=“c”(R)] X S] .D [ sR.A = “c” X X R S sR.D sR.A = “c” S R B.C=S.

D [[sR.A = “c” R Natural join S B.D sR.A = “c” X sR.Apply Rewrite Rule (3) B.C sR.C = S.A=“c”(R)] R S] S .D B.

SQL query parse parse tree Query rewriting statistics Initial logical plan Rewrite rules Logical plan logical query plan Physical plan generation physical query plan execute result “Best” logical plan .