Query Processing (16) (Xử lý truy vấn trong hệ CSDL quan hệ) (Tiếng Anh)

UN
Uni24h
0 téléchargements
49
0

Contenu du document

Page 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)

Page 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

Page 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

Page 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

Page 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

Page 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... . .

Page 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

Page 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

Page 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

Page 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

Page 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

Page 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

Page 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>

Page 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

Page 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

Page 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

Page 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

Page 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

Affichage des 20 premières pages. Téléchargez le document pour le voir en entier.

Commentaires (0)

Se connecter pour rejoindre la discussion

Aucun commentaire pour le moment. Soyez le premier !