3 QueryProcessing
3 QueryProcessing
Query Processing
query
processor
Page 1
Query Processing Components
n Query language that is used
Selecting Alternatives
SELECT ENAME
FROM EMP,ASG
WHERE EMP.ENO = ASG.ENO
AND RESP = “Manager”
Strategy 1
ΠENAME(σRESP=“Manager” ∧ EMP.ENO=ASG.ENO (EMP ⋅ ASG))
Strategy 2
ΠENAME(EMP ⋈ENO (σRESP=“Manager” (ASG)))
Strategy 2 avoids Cartesian product, so is “better”
Page 2
What is the Problem?
Site 1 Site 2 Site 3 Site 4 Site 5
ASG1=σENO≤“E3”(ASG) ASG2=σENO>“E3”(ASG) EMP1=σENO≤“E3”(EMP) EMP2=σENO>“E3”(EMP) Result
Site 5 Site 5
' '
result = EMP ∪ EMP
1 2 result= (EMP1 ∪ EMP2) ENOσRESP=“Manager”(ASG1 ∪ ASG2)
' '
EMP 1 EMP 2
Site 3 Site 4 ASG1 ASG2 EMP1 EMP2
' ' ' '
EMP = EMP1
1 ENO ASG 1 EMP = EMP2
2 ENO ASG 2 Site 1 Site 2 Site 3 Site 4
ASG1' ASG'2
Site 1 Site 2
ASG1' = σRESP="Manager" ASG1 ASG'2 = σRESP="Manager" ASG2
Cost of Alternatives
n Assume:
l size(EMP) = 400, size(ASG) = 1000
l tuple access cost = 1 unit; tuple transfer cost = 10 units
n Strategy 1
produce ASG': (10+10)∗tuple access cost 20
transfer ASG' to the sites of EMP: (10+10)∗tuple transfer cost 200
produce EMP': (10+10) ∗tuple access cost∗2 40
transfer EMP' to result site: (10+10) ∗tuple transfer cost 200
Total cost 460
n Strategy 2
transfer EMP to site 5: 400∗tuple transfer cost 4,000
transfer ASG to site 5 :1000∗tuple transfer cost 10,000
produce ASG':1000∗tuple access cost 1,000
join EMP and ASG':400∗20∗tuple access cost 8,000
Total cost 23,000
CS742 – Distributed & Parallel DBMS M. Tamer Özsu Page 3. 6
Page 3
Query Optimization Objectives
Minimize a cost function
I/O cost + CPU cost + communication cost
These might have different weights in different
distributed environments
Wide area networks
l communication cost will dominate
u low bandwidth
u low speed
u high protocol overhead
l most algorithms ignore all other cost components
Page 4
Query Optimization Issues –
Optimization Granularity
Page 5
Query Optimization Issues –
Statistics
n Relation
l Cardinality
l Size of a tuple
l Fraction of tuples participating in a join with another
relation
n Attribute
l Cardinality of domain
l Actual number of distinct values
n Common assumptions
l Independence between different attribute values
l Uniform distribution of attribute values within their
domain
n Centralized
l Single site determines the “best” schedule
l Simple
l Need knowledge about the entire distributed database
n Distributed
l Cooperation among sites to determine the schedule
l Need only local information
l Cost of cooperation
n Hybrid
l One site determines the global schedule
l Each site optimizes the local subqueries
Page 6
Query Optimization Issues –
Network Topology
n Wide area networks (WAN) – point-to-point
l Characteristics
u Low bandwidth
u Low speed
u High protocol overhead
l Communication cost will dominate; ignore all other cost
factors
l Global schedule to minimize communication cost
l Local schedules according to centralized query
optimization
n Local area networks (LAN)
l Communication cost not that dominant
l Total cost function should be considered
l Broadcasting can be exploited (joins)
l Special algorithms exist for star networks
Distributed Query
Processing Methodology
Calculus Query on Distributed
Relations
Query GLOBAL
Decomposition SCHEMA
Fragment Query
Global STATS ON
Optimization FRAGMENTS
Local LOCAL
LOCAL
Optimization SCHEMAS
SITES
Optimized Local
Queries
Page 7
Step 1 – Query Decomposition
n Input : calculus query on global relations
n Normalization
l Manipulate query quantifiers and qualification
n Analysis
l Detect and reject “incorrect” queries
l Possible for only a subset of relational calculus
n Simplification
l Eliminate redundant predicates
n Restructuring
l Calculus query ⇒ algebraic query
l More than one translation is possible
l Use transformation rules
Restructuring
n Convert relational calculus to
relational algebra ΠENAME Project
n Make use of query trees
n Example σDUR=12 ∧ DUR=24
Find the names of employees other than
J. Doe who worked on the CAD/CAM
project for either 1 or 2 years.
σPNAME=“CAD/CAM” Select
SELECT ENAME
FROM EMP, ASG, PROJ
WHERE EMP.ENO = ASG.ENO σENAME≠“J. DOE”
AND ASG.PNO = PROJ.PNO
AND ENAME ≠ “J. Doe” ⋈PNO
AND PNAME = “CAD/CAM”
AND DUR = 12 or DUR = 24)
⋈ENO Join
Page 8
Restructuring –
Transformation Rules
n Commutativity of binary operations
l R × S ⇔ S × R
l R ⋈S ⇔ S ⋈R
l R ∪ S ⇔ S ∪ R
n Associativity of binary operations
l ( R × S) × T ⇔ R × (S × T)
l (R ⋈S) ⋈T ⇔ R ⋈ (S ⋈T)
n Idempotence of unary operations
l ΠA’(Π A’(R)) ⇔ Π A’(R)
l σp (σp2(A2)(R)) ⇔ σp1(A1)∧p2(A2)(R)
1(A1)
where R[A] and A' ⊆ A, A" ⊆ A and A' ⊆ A"
n Commuting selection with projection
⋈PNO
×
⋈ENO
PROJ ASG EMP EMP PROJ ASG
Page 9
Restructuring
ΠENAME
⋈PNO
ΠPNO,ENAME
⋈ENO
Page 10
Example
Assume ΠENAME
l EMP is fragmented into EMP1, EMP2,
EMP3 as follows: σDUR=12 ∧DUR=24
u EMP1= σENO≤“E3”(EMP)
σPNAME=“CAD/CAM”
u EMP2= σ“E3”<ENO≤“E6”(EMP)
u EMP3= σENO≥“E6”(EMP)
σENAME≠“J. DOE”
l ASG fragmented into ASG1 and ASG2 as
follows:
⋈PNO
u ASG1= σENO≤“E3”(ASG)
u ASG2= σENO>“E3”(ASG) ⋈ENO
Replace EMP by (EMP1 ∪ EMP2 ∪
EMP3) PROJ ∪
∪
and ASG by (ASG1 ∪ ASG2) in any
query EMP1EMP2 EMP3 ASG1 ASG2
CS742 – Distributed & Parallel DBMS M. Tamer Özsu Page 3. 21
Provides Parallellism
Page 11
Eliminates Unnecessary
Work
Page 12
Reduction for PHF
n Reduction with join
l Possible if fragmentation is done on join attribute
Page 13
Reduction for VF
n Find useless (not empty) intermediate relations
Relation R defined over attributes A = {A1, ..., An} vertically
fragmented as Ri =ΠA'(R) where A'⊆ A:
ΠD,K(Ri) is useless if the set of projection attributes D is not in A'
Example: EMP1=ΠENO,ENAME (EMP); EMP2=ΠENO,TITLE (EMP)
SELECT ENAME
FROM EMP
ΠENAME ΠENAME
⋈ENO
Page 14
Reduction for DHF
Generic query ⋈ENO
σTITLE=“Mech. Eng.”
∪
∪
∪
σTITLE=“Mech. Eng.”
⋈ENO ⋈ENO
σTITLE=“Mech. Eng.”
ASG2 EMP2
CS742 – Distributed & Parallel DBMS M. Tamer Özsu Page 3. 30
Page 15