Documente Academic
Documente Profesional
Documente Cultură
Q
Queries
Query Processing (1)
Example
Select B,D
From R,S
RS
Where R.A = “c” S.E = 2 R.C = S.C
Answer B D
2 x
CMPT 454: Database Systems II – Query Processing (1) 4 / 32
How Do We Execute Query?
- Do Cartesian Product
- Select tuples
One idea - Do p
projection
j
X
R S
Ex: Plan I
B,D
X
R S
Plan II B,D
natural join
R.A = “c” S.E = 2
R S
A B C ((R) ((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
next tuple:
<c,7,15>
{P1,P2,…..}
CMPT 454: Database Systems II – Query Processing (1) 20 / 32
Parsing
Parse Trees
Nodes correspond to either atoms (terminal symbols)
or syntactic categories (non
(non-terminal
terminal symbols)
symbols).
An atom is a lexical element such as a keyword, name of an
attribute or relation, constant, operator, parenthesis.
A syntactic
y category
g y denotes a familyy of q
query
y subparts
p that
all play the same role within a query, e.g. Condition.
Syntactic categories are enclosed in triangular brackets
brackets, e
e.g.
g
<Condition>.
SELECT title
FROM StarsIn
WHERE starName IN (
SELECT name
FROM MovieStar
WHERE birthdate LIKE ‘%1960’
);
StarsIn
St I ((movieTitle,
i Titl movieYear,
i Y starName)
t N )
MovieStar (name, address, gender, birthdate)
<Query>
<Attribute> <Relation>
starName <Query>
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
title
starName=name
StarsIn name
birthdate LIKE ‘%1960’
MovieStar
Fig.
g 7.18: Applying
pp y g the rule for IN conditions
title
Question:
starName=name
starName name Push project
j to
StarsIn?
StarsIn name
birthdate LIKE ‘%1960’
MovieStar
Fig.
g 7.20: An improvement
p on fig.
g 7.18.
StarsIn
MovieStar
L.Q.P
P1 P2 …. Pn
C1 C2 …. Cn
Pick best!
StarsIn MovieStar
Chapter 5 and 15
5 Algebra for queries [bags vs sets]
- Select,
Select project,
project join,
join …. [project list
a,a+b->x,…]
- Duplicate elimination, grouping, sorting
15.1 Physical operators
- Scan,sort, …
15.2 - 15.6 Implementing operators +
estimating their cost