Lecture09 Optimization Structural
Lecture09 Optimization Structural
Principles of Database
Management Systems
Fall 2016
Lecture 9 – Structural query optimization
Conjunctive Queries
• Definition:
Q(X) :- R1(X1), R2(X2), ..., Rm(Xm)
• Same as a single datalog rule
• Terminology:
– Atoms
– Head variables
– Existential variables
• Example of CQ
q(x,y) = ∃z.(R(x,z) ∧ ∃u.(R(z,u) ∧ R(u,y)))
3
Types of CQ
• With aggregates:
Q(x,count(*)) :- R(x,y),S(y,z),T(z,u)
Q(x, sum(u)) :- R(x,y),S(y,z),T(z,u)
6
Complexity of Query Evaluation
• The query evaluation problem is this:
given a query Q and a database D, compute Q(D)
7
Complexity of Query Evaluation
• The query evaluation problem is this:
given a query Q and a database D, compute Q(D)
8
Complexity of Query Evaluation
• The query evaluation problem is this:
given a query Q and a database D, compute Q(D)
9
Complexity of Query Evaluation
• The query evaluation problem is this:
given a query Q and a database D, compute Q(D)
• Q(x,w) :- R(x,y),S(y,z),T(z,u),K(u,v),L(v,w)
• Assume |R|=|S|=|T|=|K|=|L| = N
• Assume |R|=|S|=|T|=|K|=|L| = N ⨝
⨝ T
• Assume |R|=|S|=|T|=|K|=|L| = N ⨝
⨝ K(u,v)
Πxz
T(z,u)
⨝
R(x,y) S(y,z)
Law of Semijoins
• Input: R(A1,…An), S(B1,…,Bm)
• Output: T(A1,…,An)
Definition: the semi-join operation is
R ⋉ S = Π A1,…,An (R ⨝ S)
R ⨝ S = (R ⋉ S) ⨝ S
CSE 544 - Fall 2016 15
Laws with Semijoins
• Example:
Q = R(A,B) ⨝ S(B,C)
Q = R1(A,B) ⨝ S(B,C)
18
Semijoin Reducer
19
Semijoin Reducer
• Example:
22
Acyclic Queries
R(x,y)
K(z,v) R(x,y)
S(y,z,u)
S(y,z)
L(v,m) T(z,x)
T(y,z,w) R(x,y),S(y,z),T(z,x)
Acyclic CSE 544 - Fall 2016 23
is cyclic
Yannakakis Algorithm
S(y,z,u)
• With aggregate: Q(x,sum(m)) :- ... L(v,m)
K(z,v) R(x,y),
S(y,z,u) A(y,s),B(x,s) ρ*=2
K(z,v),F(v,q)
L(v,m) S(y,z,u)
ρ*=1 ρ*=1
T(y,z,w)
ρ*=2
L(v,m)
T(y,z,w),
O(n + |Output|) C(z,t),D(t,p),E(p,y)
O(n2 + |Output|)
ftw = max(ρ*) = 2
Next lecture...