Query
Query
Introduction
Background
Distributed DBMS Architecture
Distributed Database Design
Semantic Data Control
Distributed Query Processing
Distributed DBMS
Page 7-9. 1
Query Processing
high level user query
query
processor
Distributed DBMS
Page 7-9. 2
Query optimization
How do we determine the best execution plan?
Distributed DBMS
Page 7-9. 3
Selecting Alternatives
SELECT
FROM
WHERE
AND
ENAME
EMP,ASG
EMP.ENO = ASG.ENO
DUR > 37
Strategy 1
ENAME(DUR>37EMP.ENO=ASG.ENO(EMP ASG))
Strategy 2
ENAME(EMP
ENO
(DUR>37 (ASG)))
Page 7-9. 4
Site 2
ASG1=ENOE3(ASG)
Site 3
ASG2=ENO>E3(ASG)
EMP1=ENOE3(EMP)
result = EMP1EMP2
Site 3
EMP1=EMP1
Site 1
ENO
Site 4
ASG1
ASG1
ASG1=DUR>37(ASG1)
Distributed DBMS
Site 5
EMP2=ENO>E3(EMP)
Result
Site 5
Site 5
EMP1
Site 4
result2=(EMP1EMP2) ENODUR>37(ASG1ASG1)
EMP2
EMP2=EMP2
Site 2
ENO
ASG2
ASG1
ASG2
EMP1
EMP2
Site 1
Site 2
Site 3
Site 4
ASG2
ASG2=DUR>37(ASG2)
Page 7-9. 5
Cost of Alternatives
Assume:
Strategy 1
Total cost
Strategy 2
Total cost
Distributed DBMS
20
200
40
200
460
4,000
10,000
1,000
8,000
23,000
Page 7-9. 6
Distributed DBMS
Page 7-9. 7
Complexity of Relational
Operations
Operation
Assume
relations of cardinality n
sequential scan
Complexity
Select
Project
(without duplicate elimination)
O(n)
Project
(with duplicate elimination)
Group
O(nlog n)
Join
Semi-join
Division
O(nlog n)
Set Operators
Cartesian Product
Distributed DBMS
O(n2)
Page 7-9. 8
Heuristics
not optimal
regroup common sub-expressions
perform selection, projection first
replace a join by a series of semijoins
reorder operations to reduce intermediate relation size
optimize individual operations
Distributed DBMS
Page 7-9. 9
Distributed DBMS
Page 7-9. 10
error propagation
can amortize over many executions
R*
Dynamic
Hybrid
run time
MERMAID
Distributed DBMS
Page 7-9. 11
Relation
cardinality
size of a tuple
fraction of tuples participating in a join with another
relation
Attribute
cardinality of domain
actual number of distinct values
Common assumptions
domain
Distributed DBMS
Page 7-9. 12
Centralized
database
Distributed
Hybrid
Distributed DBMS
Page 7-9. 13
low bandwidth
low speed
high protocol overhead
cost factors
global schedule to minimize communication cost
local schedules according to centralized query
optimization
Distributed DBMS
Page 7-9. 14
Distributed Query
Processing Methodology
Calculus Query on Distributed
Relations
Query
Query
Decomposition
Decomposition
GLOBAL
GLOBAL
SCHEMA
SCHEMA
Data
Data
Localization
Localization
FRAGMENT
FRAGMENT
SCHEMA
SCHEMA
Fragment Query
Global
Global
Optimization
Optimization
STATS
STATSON
ON
FRAGMENTS
FRAGMENTS
Local
Local
Optimization
Optimization
LOCAL
LOCAL
SCHEMAS
SCHEMAS
Optimized Local
Queries
Distributed DBMS
Page 7-9. 15
Normalization
manipulate query quantifiers and qualification
Analysis
detect and reject incorrect queries
possible for only a subset of relational calculus
Simplification
eliminate redundant predicates
Restructuring
calculus query algebraic query
more than one translation is possible
use transformation rules
Distributed DBMS
Page 7-9. 16
Normalization
(p11p12p1n) (pm1pm2pmn)
Disjunctive normal form
Distributed DBMS
Page 7-9. 17
Analysis
Semantically incorrect
Distributed DBMS
Page 7-9. 18
Analysis Example
SELECT
FROM
WHERE
AND
AND
AND
AND
ENAME,RESP
EMP, ASG, PROJ
EMP.ENO = ASG.ENO
ASG.PNO = PROJ.PNO
PNAME = "CAD/CAM"
DUR 36
TITLE = "Programmer"
Query graph
Join graph
DUR36
EMP.ENO=ASG.ENO
TITLE =
Programmer
EMP
ENAME
Distributed DBMS
ASG
ASG.PNO=PROJ.PNO
RESP
RESULT
PROJ
EMP.ENO=ASG.ENO
EMP
ASG
ASG.PNO=PROJ.PNO
PROJ
PNAME=CAD/CAM
Page 7-9. 19
Analysis
If the query graph is not connected, the query is
wrong.
SELECT
FROM
WHERE
AND
AND
AND
ENAME,RESP
EMP, ASG, PROJ
EMP.ENO = ASG.ENO
PNAME = "CAD/CAM"
DUR 36
TITLE = "Programmer"
ASG
EMP
ENAME
Distributed DBMS
RESP
RESULT
PROJ
PNAME=CAD/CAM
Page 7-9. 20
Simplification
Why simplify?
Remember the example
idempotency rules
p1 ( p1) false
p1 (p1 p2) p1
p1 false p1
application of transitivity
use of integrity rules
Distributed DBMS
Page 7-9. 21
Simplification Example
SELECT
FROM
WHERE
OR
AND
OR
AND
TITLE
EMP
EMP.ENAME = J. Doe
(NOT(EMP.TITLE = Programmer)
(EMP.TITLE = Programmer
EMP.TITLE = Elect. Eng.)
NOT(EMP.TITLE = Elect. Eng.))
SELECT
FROM
WHERE
Distributed DBMS
TITLE
EMP
EMP.ENAME = J. Doe
Page 7-9. 22
Restructuring
ENAME
DUR=12 OR DUR=24
PNAME=CAD/CAM
Select
ENAMEJ. DOE
PNO
Join
ENO
PROJ
Distributed DBMS
Project
ASG
EMP
Page 7-9. 23
Restructuring
Transformation Rules
SS
RSSR
S)
TR
(S
T)
Distributed DBMS
Page 7-9. 24
Restructuring
Transformation Rules
(Aj,Bk)
S) (p(Ai) (R))
(Aj,Bk)
(Aj,Bk)
S) A(R)
(Aj,Bk)
B(S)
Page 7-9. 25
Example
Recall the previous example:
ENAME
PROJ, ASG, EMP
ASG.ENO=EMP.ENO
ASG.PNO=PROJ.PNO
ENAMEJ. Doe
PROJ.PNAME=CAD/CAM
(DUR=12 OR DUR=24)
ENAME
DUR=12 OR DUR=24
PNAME=CAD/CAM
Select
ENAMEJ. DOE
PROJ
Distributed DBMS
Project
PNO
Join
ENO
ASG
EMP
Page 7-9. 26
Equivalent Query
ENAME
PNO ENO
ASG
Distributed DBMS
PROJ
EMP
Page 7-9. 27
Restructuring
ENAME
PNO
PNO,ENAME
ENO
PNO
PNAME = "CAD/CAM"
PROJ
Distributed DBMS
PNO,ENO
PNO,ENAME
ASG
1998 M. Tamer zsu & Patrick Valduriez
EMP
Page 7-9. 28
Localization program
substitute for each global query its materialization
program
optimize
Distributed DBMS
Page 7-9. 29
Example
ENAME
Assume
EMP is fragmented into EMP1, EMP2,
EMP3 as follows:
EMP1=ENOE3(EMP)
EMP2= E3<ENOE6(EMP)
EMP3=ENOE6(EMP)
DUR=12 OR DUR=24
PNAME=CAD/CAM
ENAMEJ. DOE
follows:
ASG1=ENOE3(ASG)
ASG2=ENO>E3(ASG)
PNO
ENO
Distributed DBMS
ASG2
Page 7-9. 30
Provides Parallellism
ENO
EMP1
ENO
ASG1
Distributed DBMS
EMP2
ENO
ASG2
EMP3
ENO
ASG1
EMP3
ASG2
Page 7-9. 31
ENO
EMP1
Distributed DBMS
ENO
ASG1
EMP2
ENO
ASG2
EMP3
ASG2
Page 7-9. 32
*
EMP
ENO=E5
ENO=E5
ENO=E5
EMP1
Distributed DBMS
EMP2
EMP3
EMP2
Page 7-9. 33
(R1 R2)
S (R1 S) (R2 S)
Distributed DBMS
Page 7-9. 34
EMP1
Distributed DBMS
EMP2
EMP3
ASG1
ASG2
Page 7-9. 35
ENO
EMP1
Distributed DBMS
ENO
ASG1
EMP2
ENO
ASG2
EMP3
ASG2
Page 7-9. 36
Reduction for VF
EMP
ENAME
ENAME
ENO
EMP1
Distributed DBMS
EMP2
1998 M. Tamer zsu & Patrick Valduriez
EMP1
Page 7-9. 37
Rule :
Example
Query
SELECT
FROM
WHERE
AND
Distributed DBMS
*
EMP, ASG
ASG.ENO = EMP.ENO
EMP.TITLE = Mech. Eng.
Page 7-9. 38
ENO
TITLE=Mech. Eng.
ASG1
ASG2
EMP1
EMP2
Selections first
ENO
TITLE=Mech. Eng.
ASG1
Distributed DBMS
ASG2
1998 M. Tamer zsu & Patrick Valduriez
EMP2
Page 7-9. 39
ENO
ENO
TITLE=Mech. Eng.
ASG1
TITLE=Mech. Eng.
EMP2
ASG2
EMP2
TITLE=Mech. Eng.
ASG2
Distributed DBMS
EMP2
Page 7-9. 40
Reduction for HF
on vertical fragments;
Distributed DBMS
Page 7-9. 41
Reduction for HF
Example
Consider the following hybrid
fragmentation:
EMP1=ENO"E4" (ENO,ENAME (EMP))
EMP2=ENO>"E4" (ENO,ENAME (EMP))
ENAME
ENAME
ENO=E5
ENO=E5
ENAME
EMP
ENO=E5
EMP2
Distributed DBMS
Page 7-9. 42
Join methods
Distributed DBMS
Page 7-9. 43
Cost-Based Optimization
Solution space
The set of equivalent algebra expressions (query trees).
Search algorithm
How do we move inside the solution space?
Exhaustive search, heuristic algorithms (iterative
Distributed DBMS
Page 7-9. 44
Transformation
Rules
Equivalent QEP
Search
Strategy
Cost Model
Best QEP
Distributed DBMS
Page 7-9. 45
Search Space
ENAME,RESP
EMP, ASG, PROJ
EMP.ENO=ASG.ENO
ASG.PNO=PROJ.PNO
PNO
PROJ
ENO
EMP
ASG
ENO
EMP
PNO
PROJ
ASG
ENO,PNO
PROJ
Distributed DBMS
ASG
EMP
Page 7-9. 46
Search Space
R4
R3
R1
Distributed DBMS
R2
R1
R2
R3
R4
Page 7-9. 47
Search Strategy
Randomized
Distributed DBMS
Page 7-9. 48
Search Strategies
Deterministic
R4
R3
R1
R2
R1
R2
R3
R1
R2
Randomized
R3
R1
Distributed DBMS
R2
R2
R1
R3
Page 7-9. 49
Cost Functions
individually
Response Time
Do as many things as possible in parallel
May increase total time because of increased total activity
Distributed DBMS
Page 7-9. 50
Total Cost
Summation of all cost factors
Total cost = CPU cost + I/O cost + communication cost
CPU cost = unit instruction cost no.of instructions
I/O cost
Distributed DBMS
Page 7-9. 51
minicomputers)
or less equal
ratio = 1:1.6
Distributed DBMS
Page 7-9. 52
Response Time
Elapsed time between the initiation and the completion of
a query
Response time
CPU time
I/O time
Page 7-9. 53
Example
Site 1
x units
Site 3
Site 2
y units
Page 7-9. 54
Optimization Statistics
For joins
SF (R,S) =
Distributed DBMS
card(R
S)
card(R)card(S)
Page 7-9. 55
where
S F(A = value) =
card(A(R))
max(A) value
S F(A > value) =
max(A) min(A)
S F(A < value) =
value max(A)
max(A) min(A)
Page 7-9. 56
Cartesian Product
card(R S) = card(R) card(S)
Union
upper bound: card(R S) = card(R) + card(S)
lower bound: card(R S) = max{card(R), card(S)}
Set Difference
upper bound: card(RS) = card(R)
lower bound: 0
Distributed DBMS
Page 7-9. 57
S;
card(R
A=B
S) = card(S)
More general:
card(R
S) = SF card(R) card(S)
Semijoin
card(R
S) = SF (S.A) card(R)
where
SF (R
Distributed DBMS
S)= SF (S.A) =
card(A(S))
card(dom[A])
Page 7-9. 58
INGRES
dynamic
interpretive
System R
static
exhaustive search
Distributed DBMS
Page 7-9. 59
INGRES Algorithm
Decompose each multi-variable query into a
sizes
Distributed DBMS
Page 7-9. 60
INGRES AlgorithmDecomposition
Detachment
Tuple substitution
q(V1, V2, ... Vn) (q' (t1, V2, V2, ... , Vn), t1 R)
Distributed DBMS
Page 7-9. 61
Detachment
q: SELECT
V2.A2,V3.A3, ,Vn.An
FROM
R1 V1, ,Rn Vn
WHERE
q':SELECT
FROM
R1 V 1
WHERE
P1(V1.A1)
q": SELECT
V2.A2, , Vn.An
FROM
WHERE
Distributed DBMS
Page 7-9. 62
Detachment Example
Names of employees working on CAD/CAM project
q 1:
SELECT
FROM
WHERE
AND
AND
EMP.ENAME
EMP, ASG, PROJ
EMP.ENO=ASG.ENO
ASG.PNO=PROJ.PNO
PROJ.PNAME="CAD/CAM"
q11: SELECT
FROM
WHERE
q':
Distributed DBMS
SELECT
FROM
WHERE
AND
EMP.ENAME
EMP,ASG,JVAR
EMP.ENO=ASG.ENO
ASG.PNO=JVAR.PNO
Page 7-9. 63
q12:
EMP.ENAME
EMP,ASG,JVAR
EMP.ENO=ASG.ENO
ASG.PNO=JVAR.PNO
SELECT
FROM
WHERE
q13:
Distributed DBMS
ASG,JVAR
ASG.PNO=JVAR.PNO
SELECT
FROM
WHERE
EMP.ENAME
EMP,GVAR
EMP.ENO=GVAR.ENO
Page 7-9. 64
Tuple Substitution
q11 is a mono-variable query
q12 and q13 is subject to tuple substitution
Assume GVAR has two tuples only: <E1> and <E2>
Then q13 becomes
q131: SELECT
FROM
WHERE
EMP.ENAME
EMP
EMP.ENO="E1"
q132: SELECT
FROM
WHERE
EMP.ENAME
EMP
EMP.ENO="E2"
Distributed DBMS
Page 7-9. 65
System R Algorithm
Simple (i.e., mono-relation) queries are
Execute joins
2.1 Determine the possible ordering of joins
2.2 Determine the cost of each ordering
2.3 Choose the join ordering with minimal cost
Distributed DBMS
Page 7-9. 66
System R Algorithm
For joins, two alternative algorithms :
Nested loops
for each tuple of external relation (cardinality n1)
for each tuple of internal relation (cardinality n2)
join two tuples if the join predicate is true
end
end
Complexity: n1n2
Merge join
sort relations
merge relations
equijoin
Distributed DBMS
Page 7-9. 67
ASG
ENO
EMP
Distributed DBMS
PNO
PROJ
Page 7-9. 68
Distributed DBMS
Page 7-9. 69
System R Algorithm
Alternatives
ASG
EMP
EMP ASG
pruned
(ASG
EMP
EMP)
PROJ
ASG PROJ
pruned
PROJ
PROJ
(PROJ
ASG
ASG)
PROJ EMP
pruned
EMP
Distributed DBMS
Page 7-9. 70
System R Algorithm
Distributed DBMS
Page 7-9. 71
Ordering joins
Distributed INGRES
System R*
Semijoin ordering
SDD-1
Distributed DBMS
Page 7-9. 72
Join Ordering
Consider
S
if size (R) > size (S)
best one.
Use heuristics
Distributed DBMS
Page 7-9. 73
ASG
PNO
EMP
ENO
Site 2
ASG
ENO
EMP
PNO
PROJ
Site 1
Distributed DBMS
Site 3
Page 7-9. 74
1. EMP Site 2
2. ASG Site 1
Site 2 computes EMP'=EMP ASG
Site 1 computes EMP'=EMP ASG
EMP' Site 3
EMP' Site 3
Site 3 computes EMP PROJ
Site 3 computes EMP PROJ
3. ASG Site 3
4. PROJ Site 2
Site 3 computes ASG'=ASG PROJ Site 2 computes PROJ'=PROJ ASG
ASG' Site 1
PROJ' Site 1
Site 1 computes ASG' EMP
Site 1 computes PROJ'
EMP
5. EMP Site 2
PROJ Site 2
Site 2 computes EMP
Distributed DBMS
PROJ
ASG
Page 7-9. 75
Semijoin Algorithms
Alternatives:
1 Do the join R
S (R
R
Distributed DBMS
(R
A
A
S)
S)
(S
A
A
(S
R)
A
R)
Page 7-9. 76
Semijoin Algorithms
Consider semijoin (R
S)
S' A(S)
S' Site 1
Site 1 computes R' = R
S'
R' Site 2
Site 2 computes R'
Semijoin is better if
size(A(S)) + size(R
Distributed DBMS
Page 7-9. 77
Distributed Query
Processing
Algorithms
Opt.
Timing
Objective
Function
Opt.
Factors
Network
To pology
Dist.
INGRES
Dynamic
Resp.
Msg. Size, General or
time or Proc. Cost Broadcast
To tal time
No
Horizontal
R*
Static
No
1, 2
No
SDD-1
Static
Yes
1,3,4,
5
No
General
Semijoin Stats
Fragments
1: relation cardinality; 2: number of unique values per attribute; 3: join selectivity factor; 4: size
of projection on each join attribute; 5: attribute size and tuple size
Distributed DBMS
Page 7-9. 78
Distributed DBMS
Page 7-9. 79
R* Algorithm
Exhaustive search
Compilation
Distributed DBMS
Page 7-9. 80
R* Algorithm
Performing joins
Ship whole
Fetch as needed
relation)
data transfer per message is minimal
better if relations are large and the selectivity is
good
Distributed DBMS
Page 7-9. 81
R* Algorithm
Vertical Partitioning & Joins
1. Move outer relation tuples to the site of the inner
relation
(a) Retrieve outer tuples
(b) Send them to the inner relation site
(c) Join them as they arrive
Total Cost = cost(retrieving qualified outer tuples)
+ no. of outer tuples fetched
cost(retrieving qualified inner tuples)
Distributed DBMS
Page 7-9. 82
R* Algorithm
Vertical Partitioning & Joins
2. Move inner relation to the site of outer relation
cannot join as they arrive; they need to be stored
Total Cost = cost(retrieving qualified outer tuples)
+ no. of outer tuples fetched
cost(retrieving
matching inner tuples
from temporary
storage)
+ cost(retrieving qualified inner tuples)
Distributed DBMS
in
avg.
Page 7-9. 83
R* Algorithm
Vertical Partitioning & Joins
3. Move both inner and outer relations to another site
Total cost = cost(retrieving qualified outer tuples)
+
+
msg. cost (no. of outer tuples fetched
avg. outer tuple size) / msg. size
+
msg. cost (no. of inner tuples fetched
avg. inner tuple size) / msg. size
+
no. of outer tuples fetched
cost(retrieving inner tuples from
temporary
storage)
Distributed DBMS
Page 7-9. 84
R* Algorithm
Vertical Partitioning & Joins
4. Fetch inner tuples as needed
Distributed DBMS
Page 7-9. 85
SDD-1 Algorithm
Distributed DBMS
Page 7-9. 86
Page 7-9. 87
Distributed DBMS
Page 7-9. 88
TITLE
Relation
(EMP
ENO
(ASG
Size
Site
EMP
PAY
PROJ
ASG
4
4
10
2
3
4
PNO
(PNAME=CAD/CAM(PROJ)))))
Assume:
Distributed DBMS
Page 7-9. 89
Distributed DBMS
Size
8
4
1
10
Site
1
2
3
4
Page 7-9. 90
4 + 10 + 1 = 15
Distributed DBMS
Page 7-9. 91
PAY) Site 4
EMP) Site 4
Distributed DBMS
Page 7-9. 92
= cost(EMPSite 2) + cost((EMP
PAY)Site 4) +
cost(PROJ Site 4)
8 + 8 + 1 = 17
cost(Alternative 2)
= cost(PAYSite 1) + cost((PAY
EMP)Site 4) +
cost(PROJ Site 4)
4 + 8 + 1 = 13
Distributed DBMS
Page 7-9. 93
Distributed DBMS
Page 7-9. 94
SDD-1 Algorithm
Initialization
Step 1: In the execution strategy (call it ES),
include all the local processing
Step 2: Reflect the effects of local processing on
the database profile
Step 3: Construct a set of beneficial semijoin
operations (BS) as follows :
BS =
For each semijoin SJi
BS BS SJi if cost(SJi ) < benefit(SJi)
Distributed DBMS
Page 7-9. 95
R3.C
R1, R2, R3
R1.A = R2.A
R2.B = R3.B
Site 1
R1
Distributed DBMS
Site 2
A
R2
Site 3
B
R3
R1
R2
R3
tuple size
30
100
50
attribute
R1.A
R2.A
R2.B
R3.B
50
30
40
SF
0.3
0.8
1.0
0.4
relation
size
1500
3000
2000
size(attribute)
36
320
400
80
Page 7-9. 96
Beneficial semijoins:
SJ1 = R2
SJ2 = R2
Nonbeneficial semijoins:
SJ3 = R1
R2 , whose benefit is
300 = (1 0.8) 1500 and cost is 320
SJ4 = R3
Distributed DBMS
Page 7-9. 97
SDD-1 Algorithm
Iterative Process
Step 4: Remove the most beneficial SJi from BS
and append it to ES
Step 5: Modify the database profile accordingly
Step 6: Modify BS appropriately
compute new benefit/cost values
check if any new semijoin need to be
included in BS
Page 7-9. 98
Iteration 1:
Iteration 2:
R3,
SJ3 = R1
Add SJ3 to ES
Update statistics
Distributed DBMS
Page 7-9. 99
Iteration 3:
No new beneficial semijoins.
Remove remaining beneficial semijoin SJ2 from
Update statistics
size(R2) = 360 (= 900*0.4)
Note: selectivity of R2 may also change, but not
important in this example.
Distributed DBMS
SDD-1 Algorithm
Assembly Site Selection
Step 8: Find the site where the largest amount of data
resides and select it as the assembly site
Example:
Amount of data stored at sites:
Site 1: 360
Site 2: 360
Site 3: 2000
SDD-1 Algorithm
Postprocessing
Step 9: For each Ri at the assembly site, find the
semijoins of the type
Ri Rj
where the total cost of ES without this semijoin
is smaller than the cost with it and remove the
semijoin from ES.
Note : There might be indirect benefits.
Example: No semijoins are removed.
Distributed DBMS
R1)
R3 to Site 3
R2 to Site 3
Distributed DBMS
Cost model
Optimization/reoptimization interval
reoptimization is necessary
Distributed DBMS