Sunteți pe pagina 1din 32

Query Processing and Advanced

Q
Queries
Query Processing (1)
Example

Select B,D
From R,S
RS
Where R.A = “c”  S.E = 2  R.C = S.C

CMPT 454: Database Systems II – Query Processing (1) 2 / 32


Example (cont.)
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

CMPT 454: Database Systems II – Query Processing (1) 3 / 32


Example (cont.)
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

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

CMPT 454: Database Systems II – Query Processing (1) 5 / 32


One Idea
RXS R.A R.B R.C S.C S.D S.E
a 1 10 10 x 2
a 1 10 20 y 2
.
.
C 2 10 10 x 2
.
.

CMPT 454: Database Systems II – Query Processing (1) 6 / 32


One Idea
RXS R.A R.B R.C S.C S.D S.E
a 1 10 10 x 2
a 1 10 20 y 2
.
.
Bingo!
C 2 10 10 x 2
Got one... .
.

CMPT 454: Database Systems II – Query Processing (1) 7 / 32


Relational Algebra
can be used to dscribe plans...
Ex: Plan I
B,D

R.A=“c” S.E=2  R.C=S.C

X
R S

CMPT 454: Database Systems II – Query Processing (1) 8 / 32


Relational Algebra (cont.)

Ex: Plan I
B,D

R.A=“c” S.E=2  R.C=S.C

X
R S

OR: B,D [ R.A=“c” S.E=2  R.C = S.C (RXS)]

CMPT 454: Database Systems II – Query Processing (1) 9 / 32


Another Idea (1)

Plan II B,D

natural join
R.A = “c” S.E = 2
R S

CMPT 454: Database Systems II – Query Processing (1) 10 / 32


Another Idea (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

CMPT 454: Database Systems II – Query Processing (1) 11 / 32


Plan III (1)
Use R.A and S.C Indexes
(1) U
Use RR.A
A iindex
d to select
l R tuples
l
with R.A = “c”
(2) For each R.C value found, use S.C
index to find matching tuples

CMPT 454: Database Systems II – Query Processing (1) 12 / 32


Plan III (2)
Use R.A and S.C Indexes
(1) U
Use RR.A
A iindex
d to select
l R tuples
l
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) JJoin
i matching
t hi R R,S
S ttuples,
l project
j t
B,D
, attributes and place
p in result

CMPT 454: Database Systems II – Query Processing (1) 13 / 32


Plan III (3)
R S
A C
A B C I1 I2 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

CMPT 454: Database Systems II – Query Processing (1) 14 / 32


Plan III (4)
R S
A=“c” C
A B C I1 I2 C D E
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

CMPT 454: Database Systems II – Query Processing (1) 15 / 32


Plan III (5)
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 30 z 2
d 2 35 40 x 1
e 3 45 50 y 3

CMPT 454: Database Systems II – Query Processing (1) 16 / 32


Plan III (6)
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
check=2?
check 2?
c 2 10 30 z 2
d 2 35 output: <2,x>
output , 40 x 1
e 3 45 50 y 3

CMPT 454: Database Systems II – Query Processing (1) 17 / 32


Plan III (7)
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
check=2?
check 2?
c 2 10 30 z 2
d 2 35 output: <2,x>
output , 40 x 1
e 3 45 50 y 3

next tuple:
<c,7,15>

CMPT 454: Database Systems II – Query Processing (1) 18 / 32


Overview of Query Processing
The Query Processing translates an SQL query into
a physical query plan
plan, which can be executed
executed, in
three steps:
Th query iis parsed
The d and t d as a parse
d represented
tree.
The parse tree is converted into a relational
algebra expression tree (logical query plan).
The logical query plan is refined into a physical
queryy p
q plan, which also specifies
p the algorithms
g
used in each step and the way in which data is
obtained.

CMPT 454: Database Systems II – Query Processing (1) 19 / 32


Query Processing
SQL query
parse
parse tree
con e t
convert
answer
logical query plan
execute
apply laws statistics
Pi
p
“improved” l.q.p
qp
pick best
estimate result sizes
l.q.p.
q p +sizes
s es {(P1,C1),(P2,C2)...}

consider physical plans estimate costs

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

CMPT 454: Database Systems II – Query Processing (1) 21 / 32


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)

StarsIn
St I ((movieTitle,
i Titl movieYear,
i Y starName)
t N )
MovieStar (name, address, gender, birthdate)

CMPT 454: Database Systems II – Query Processing (1) 22 / 32


Example: Parse Tree

<Query>

SELECT <SelList> FROM <FromList> WHERE <Condition>

<Attribute> <Relation>

title StarsIn <Attribute> IN ( )

starName <Query>

SELECT <SelList> FROM <FromList> WHERE <Condition>

<Attribute> <Relation> <Attribute> LIKE <Pattern>

name MovieStar birthDate ‘%1960’

CMPT 454: Database Systems II – Query Processing (1) 23 / 32


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

CMPT 454: Database Systems II – Query Processing (1) 24 / 32


Example: Logical Query Plan

title
starName=name

StarsIn name
birthdate LIKE ‘%1960’
MovieStar
Fig.
g 7.18: Applying
pp y g the rule for IN conditions

CMPT 454: Database Systems II – Query Processing (1) 25 / 32


Example: Improved Logical Query Plan

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

CMPT 454: Database Systems II – Query Processing (1) 26 / 32


Example: Estimate Result Sizes

Need expected size

StarsIn


MovieStar

CMPT 454: Database Systems II – Query Processing (1) 27 / 32


Example: Estimate costs

L.Q.P

P1 P2 …. Pn

C1 C2 …. Cn

Pick best!

CMPT 454: Database Systems II – Query Processing (1) 28 / 32


Example: One Physical Plan

Parameters: join order,


Hash join
memory size, project attributes,...

SEQ scan index scan Parameters:


Select Condition,...

StarsIn MovieStar

CMPT 454: Database Systems II – Query Processing (1) 29 / 32


Textbook Outline

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

CMPT 454: Database Systems II – Query Processing (1) 30 / 32


Textbook Outline (cont.)
Chapter 16
16 1
16.1 Parsing
16.2 Algebraic laws
16.3 Parse tree -> logical query
plan
16.4 Estimating result sizes
16 5
16.5 Cost based optimization

CMPT 454: Database Systems II – Query Processing (1) 31 / 32


Topics to Be Covered…
Relational algebra level
D t il d query plan
Detailed l llevell
– Estimate Costs
• without indexes
• with indexes
– Generate and compare plans

CMPT 454: Database Systems II – Query Processing (1) 32 / 32

S-ar putea să vă placă și