Database design and implementation CMPSCI 645. Lectures 1112: Database Theory


 Hugh Small
 8 months ago
 Views:
Transcription
1 Database design and implementation CMPSCI 645 Lectures 1112: Database Theory 1
2 Theory problems in databases } Expressiveness of languages } Any query in L1 can be expressed in L2 } Complexity of languages } Bounds on resources required to evaluate any query in language L } Sta?c analysis of queries (for op?miza?on) } Given q in L: is it minimal? } Given q1 and q2 in L: are they equivalent? } Views 2
3 Crash review of complexity classes } AC 0 } Circuits of O(1) depth and polynomial size } L (LOGSPACE) } Solvable in logarithmic (small) space } NL (NLOGSPACE) } "YES" answers checkable in logarithmic space } NC } Solvable efficiently (in polylogarithmic?me) on parallel computers } P (PTIME) } Solvable in polynomial?me } NP } "YES" answers checkable in polynomial?me } PSPACE } Solvable with polynomial memory 3
4 Rules of thumb } Step 1: check if you can solve the problem in that class } Step 2: if not, check if your problem looks like (is reducible from) the complete problem from the next class. } Of interest: PTIMEcomplete are not efficiently parallelizable. 4
5 Query complexity Given a query Q and a database D, what is the complexity of compu?ng Q(D)? } The answer depends on the query language } Rela?onal algebra, calculus, datalog } Design tradeoff: } High complexity à rich queries } Low complexity à implemented efficiently 5
6 Complexity of query languages Query Q, database D } Data complexity } Fix Q, complexity f(d) } Query complexity } Fix D, complexity f(q) } Combined complexity } Complexity f(d,q) Moshe Vardi 6
7 Conventions } Complexity is usually defined for a decision problem } We study the complexity of Boolean queries } Complexity usually assumes some encoding of the input } We encode instances using binary representa?on 7
8 Boolean queries Defini&on: A Boolean query is a query that returns either true or false. NonBoolean SELECT DISTINCT R.x, S.y FROM R, S WHERE R.z = S.z Q(x,y) : R(x,z), S(z,y) T(x,y) : R(x,y) T(x,y) : T(x,z), R(z,y) Boolean SELECT DISTINCT yes FROM R, S WHERE R.x = a and R.z = S.z and S.y = b Q : R(x,z), S(z,y) T(x,y) : R(x,y) T(x,y) : T(x,z), R(z,y) Answer() : T( a, b ) 8
9 Database encoding } Encode D = (D, R 1D,, R kd ) as follows: } Let n = ADom(D) } If R i has arity k, encode it as a string of n k bits: 2 } 0 means element (a 1, a k ) R i D } 1 means element (a 1, a k ) 2 R D i a a b b c b c b c a
10 Data complexity } Fix a Boolean query Q in the query language. Determine the complexity of the following problem: } Given an input database instance D = (D, R 1D,, R kd ) check if Q(D) = true } This is also known as model checking problem: check if D is a model for Q. 10
11 What is the complexity of relational queries? AC 0 L NL NC PTIME PSPACE all computable 11
12 Example Q = 9z. R( a,z) S(z, b ) Prove that Q is in AC 0 R: a b c a b c S: a b c a b c
13 Example Q = 9z. R( a,z) S(z, b ) Prove that Q is in AC 0 Circuit of depth 2 OR has n inputs Each AND has 2 inputs R: a b c a b c S: a b c a b c
14 What is the complexity of relational queries? All rela?onal queries (expressible in RC) are in AC 0 AC 0 L NL NC PTIME PSPACE all computable 14
15 What is the complexity of datalog queries? AC 0 L NL NC PTIME T(x,y) : R(x,y) T(x,y) : T(x,z), R(z,y) Answer() : T( a, b ) PSPACE all computable 15
16 Datalog is not in AC 0 } Parity is not in AC 0 } We will reduce parity to the reachability problem } Given input (x 1, x 2, x 3, x 4, x 5 ) = (0,1,1,0,1) construct the graph: a 1 a 2 a 3 a 4 a 5 a 6 T(x,y) : R(x,y) T(x,y) : T(x,z), R(z,y) Answer() : T( a 1, b 6 ) b 1 b 2 b 3 b 4 b 5 b 6 The # of 1s is odd iff Answer is true 16
17 Datalog is in PTIME } Fix any Boolean datalog program P. } Given D, check if P(D) = true is in PTIME } Proof argument: } If an IDB has arity k, then it will reach its fixpoint in at most n k itera?ons. 17
18 Conjunctive Queries (CQ) } A subset of FO (first order) } Less expressive } Many queries in prac?ce are conjunc?ve } Some op?mizers only handle CQs } Break larger queries into many CQs } CQs have beler theore?cal proper?es than arbitrary queries 18
19 Conjunctive Queries if variables subgoals P(x,z) : R(x,y) & R(y,z) head body implicit 9 conjunc?on } R: Extensional database (EDB) stored } P: Inten?onal database (IDB) computed 19
20 Conjunctive Queries if variables subgoals P(x,z) : R(x,y) & R(y,z) head body implicit 9 conjunc?on } When facts in the body are true, we infer the head } Consider all possible assignments of variables in the body 20
21 Conjunctive queries } A single datalog rule } Equivalent to SELECTDISTINCTFROMWHERE } Select/project/join in RA } Existen?al/conjunc?ve frac?on of RC Strictly speaking, we are not allowed to have nonequality selec?on predicates 21
22 Example } Find all employees having the same manager as Smith A(x) : ManagedBy( Smith,y) & ManagedBy(x,y) SELECT DISTINCT m2.name FROM ManagedBy m1, ManagedBy m2 WHERE m1.name = Smith AND m1.manager = m2.manager 22
23 Properties of CQ } Sa?sfiability } A query is sa?sfiable if there exists some input rela?on R such that q(r) is nonempty } Every CQ is sa?sfiable } Monotonicity } A query is monotonic if for each instance I, J over the schema, I J implies q(i) q(j) } Every CQ is monotonic 23
24 Satisfiability We can always generate sa?sfying EDB rela?ons from the body of the rule S(x,y,z) : P(x,w) & R(w,y,v) & P(v,z) a b b c d d e S a c e P a b R b c d d e 24
25 Monotonicity ans(u): R 1 (u 1 ) & & R n (u n ) } Consider 2 databases I, J, s.t. I J } Let t q(i) 2 } For some subs?tu?on v: } v(u i ) 2 I(R i ) for each i. } t = v(u) } Since I J, v(u i ) 2J(R i ) for each i. } So t q(j) 2 25
26 Consequence of monotonicity Product (pname, price, cid) Company (cid, cname, city) Q: Find all companies that make only products with price < 100! SELECT!DISTINCT C.cname! FROM!Company C! WHERE!100 > ALL!(SELECT price!!! FROM Product P!!! WHERE P.cid = C.cid)! } This query is not monotone } Therefore, not CQ } It cannot be expressed as a simple SFW query 26
27 Equivalence and containment } Needed for a variety of sta?c analysis tasks } Query op?miza?on } Query rewri?ng using views } Tes?ng for semijoin reduc?ons 27
28 Query equivalence Defini&on: Queries q1 and q2 are equivalent if for every database D, q1(d) = q2(d) Nota?on: q1 q2 28
29 Query containment Defini&on: Query q1 is contained in q2 if for every database D, q1(d) q2(d) Nota?on: q1 q2 Fact: q1 q2 and q2 q1 iff q1 q2 For the case of Boolean queries, containment is logical implica?on 29
30 Examples Is q1 q2? Yes q1(x) : R(x,u), R(u, Smith ) q2(x) : R(x,u), R(u,v) 30
31 Examples Is q1 q2? Yes q1(x) : R(x,u), R(u,v), R(v,w) q2(x) : R(x,u), R(u,v) x u v w x u v 31
32 Examples Is q1 q2? No q1(x) : R(x,u), R(u,v), R(v,x) q2(x) : R(x,u), R(u,x) x u v x u 32
33 Examples Is q1 q2? Yes q1(x) : R(x,u), R(u,y) q2(x) : R(x,u), R(v,u), R(u,y) x u y x u v y 33
34 Examples Is q1 q2? Yes q1(x) : R(x,u), R(u,v) q2(x) : R(x,u), R(x,y), R(u,v), R(u,w) x u v x u v y w 34
35 Examples Is q1 q2? Yes q1(x) : R(x,u), R(u,u) q2(x) : R(x,u), R(u,v), R(v,w) x x u v w u 35
36 Query containment Theorem: The query containment and query equivalence problems for CQ are NPcomplete. Theorem: The query containment and query equivalence problems for Rela?onal Calculus are undecidable. 36
37 Query containment for CQ } Two ways to test } Check if q2 holds on the canonical database of q1 } Check if there exists a homomorphism q2 à q1 37
38 Canonical database } Canonical database for q1 is D = (D, R 1D,, R kd ) } D: all variables and constants in q1 } R 1D,, R kd : the body of q1 } Canonical tuple t q1 is the head of q1 38
39 Example } Canonical database D = (D, R D ) } D = {x,y,u,v} } R D = x v v q1(x,y) : R(x,u), R(v,u), R(v,y) u u y } Canonical tuple t q1 = (x,y) 39
40 Example } Canonical database D = (D, R) } D = {x,u, Smith, Fred } } R= q1(x) : R(x,u), R(u, Smith ), R(u, Fred ), R(u,u) x u u u } Canonical tuple t q1 = (x) u Smith Fred u 40
41 Checking containment using the canonical database } D={x,y,u,v} } R = x q1(x,y) : R(x,u), R(v,u), R(v,y) q2(x,y) : R(x,u), R(v,u), R(v,w), R(t,w), R(t,y) v v u u y q1 is contained in q2 41
42 Query homomorphisms } A homomorphism f: q2 à q1 is a func?on f:var(q2) à var(q1) U const(q1), such that: } f(body(q2)) body(q1) } f(t q1 ) = t q2 42
43 Example var(q1) = {x,u,v,y} var(q2) = {x,u,v,w,t,y} q1(x,y) : R(x,u), R(v,u), R(v,y) q2(x,y) : R(x,u), R(v,u), R(v,w), R(t,w), R(t,y) q1 is contained in q2 43
44 Example var(q1) U const(q1) = {x,u, Smith } var(q2) = {x,u,v,w} q1(x) : R(x,u), R(u, Smith ), R(u, Fred ), R(u,u) q2(x) : R(x,u), R(u,v), R(u, Smith ), R(w,u) q1 is contained in q2 44
45 Complexity Theorem: Checking containment of two CQs is NPcomplete. =( X 3 _ X 1 _ X 4 ) ^ (X 1 _ X 2 _ X 3 ) ^ ( X 2 _ X 3 _ X 1 ) Proof: Reduc?on from 3SAT Given a 3CNF Φ Step 1: construct q1 independently of Φ Step 2: construct q2 from Φ Prove: there exists a homomorphism q2àq1 iff Φ is sa?sfiable 45
46 Proof: Step 1 Construc?ng q1 There are 4 types of clauses in every 3SAT: Type 1: X _ Y _ Z Type 2: X _ Y _ Z Type 3: X _ Y _ Z Type 4: X _ Y _ Z For each type, q1 contains one rela?on with all 7 sa?sfying assignments, where u=0, v=1 R1 (misses v,v,v) R2 (misses v,v,u) R3 (misses v,u,u) R4 (misses u,u,u) u u u u u u u u u u u v u u v u u v u u v u v u u v u u v u u v u u v v u v v u v v u v v v u u v u u v u u v u v v u v v u v v u v v v u v v u v v u v v v v v v v v v 46
47 Proof: Step 2 Construc?ng q2 q2 has one atom for each clause is Φ: Rela?on name is R1, or R2, or R3, or R4 The variables are the same as those in the clause Example: =( X 3 _ X 1 _ X 4 ) ^ (X 1 _ X 2 _ X 3 ) ^ ( X 2 _ X 3 _ X 1 ) q2 = R2(x 3,x 1,x 4 ), R4(x 1,x 2,x 3 ), R2(x 2,x 3,x 1 ) 47
48 Proof } Suppose there is a sa?sfying assignment for Φ: it maps each X i to either 0 or 1 } Define func?on f: Vars(q2)àVars(q1): } If X i = 0 then f(x i ) = u } If X i = 1 then f(x i ) = v } Then f is a homomorphism f: q2àq1 } Suppose there exists a homomorphism f: q2àq1 } Define the assignment: } If f(x i ) = u then X i = 0 } If f(xi) = v then X i = 1 } This is a sa?sfying assignment for Φ 48
49 Beyond CQ } Containment for arbitrary rela?onal queries is undecidable } Any sta?c analysis on rela?onal queries is undecidable } All these results follow from Trakhtenbrot s theorem 49
50 Trakhtenbrot s theorem Defini&on: A sentence φ is called finitely sa?sfiable if there exists a finite database instance D s.t. D = φ Theorem: The following problem is undecidable: Given FO sentence φ, check if φ is finitely sa?sfiable. 50
51 Query containment for UCQ q 1 [ q 2 [ q 3... q 0 1 [ q 0 2 [ q Note: q 1 [ q 2 [ q 3... q q 1 q q 2 q iff and and Theorem: q q1 0 [ q2 0 [ q that q qk 0 iff there exists some k such 51
52 Query minimization Defini&on: A conjunc?ve query q is minimal, if for every other query q such that q q, q has at least as many predicates (subgoals) as q Are these queries minimal? q(x) : R(x,y), R(y,z), R(x,x) q(x) : R(x,y), R(y,z), R(x, Alice ) 52
53 Query minimization } Algorithm: } Choose a subgoal g of q } Remove g: let q be the new query } q q Why? } If q q, then permanently remove g } The order in which we inspect subgoals doesn t maler 53
54 In practice } No database system performs minimiza?on } It s hard } Users usually write minimal queries } Nonminimal queries arise when using views intensely 54
55 Remember Semijoins? R S = Π A1,,An (R S) R S = (R S) S Prove that the following two datalog queries are equivalent: q1(x,y,z) : R(x,y), S(x,z) R1(x,y) : R(x,y), S(x,z) q2(x,y,z) : R1(x,y), S(x,z) q1(x,y,z) : R(x,y), S(x,z) q1(x,y,z) : R(x,y), S(x,z) q2(x,y,z) : R(x,y), S(x,u), S(x,z) q2(x,y,z) : R(x,y), S(x,u), S(x,z) 55
56 Semijoins } Important in distributed databases } Owen combined with Bloom filters } See in the textbook 56
57 Semijoin Reducer } Given a query: Q = R1 R2 Rn } A semijoin reducer for q is: } R i1 = R i1 R j1 } R i2 = R i2 R j2 } } R ip = R ip R jp } Such that the query is equivalent to } Q = R k1 R k2 R kn } In a full reducer, no dangling tuples remain 57
58 Example } Q = R(A,B) S(B,C) } Semijoin reducer: R1(A,B) = R(A,B) S(B,C) } Rewrilen query: Q = R1(A,B) S(B,C) Are there any dangling tuples? 58
59 Example } Q = R(A,B) S(B,C) } Full semijoin reducer: R1(A,B) = R(A,B) S(B,C) S1(B,C) = S(B,C) R1(A,B) } Rewrilen query: Q = R1(A,B) S1(B,C) No more dangling tuples 59
60 Example } More complex: Q = R(A,B) S(B,C) T(C,D,E) } Full reducer: S (B,C) = S(B,C) R(A,B) T (C,D,E) = T(C,D,E) S (B,C) S (B,C) = S (B,C) T (C,D,E) R (A,B) = R(A,B) S (B,C) } Q = R (A,B) S (B,C) T (C,D,E) 60
61 Semijoin Reducer } Example: Q = R(A,B) S(B,C) T(C,A) } No full reducer Theorem: A query has a full reducer iff it is acyclic. 61
62 Expressive power of FO } Let R(x,y) represent a graph } Query path(x,y) = } All x,y such that there is a path from x to y } Theorem: path(x,y) cannot be expressed in FO 62
63 Nonrecursive rules } Graph R(x,y) P(x,y) : R(x,u), R(u,v), R(v,y) A(x,y) : P(x,u), P(u,y) } Can unfold into: A(x,y) : R(x,u), R(u,v), R(v,w), R(w,m), R(m,n), R(n,y) 63
64 Nonrecursive datalog with negation } Expresses FO queries } Negated subgoals } Implicit union } Can evaluate in an order such that all body predicates have been evaluated. 64
65 Recursion Two forms of transi?ve closure Path(x,y) : R(x,y) Path(x,y) : Path(x,u), R(u,y) Path(x,y) : R(x,y) Path(x,y) : Path(x,u), Path(u,y) 65
66 Recursion example } EDB Par(c,p) = p is parent of c } Generalized cousins: people with common ancestors one or more genera?ons back Sib(x,y) : Par(x,p), Par(y,p), x y Cousin(x,y) : Sib(x,y) Cousin(x,y) : Par(x,xp), Par(y,yp), Cousin(xp,yp) 66
67 Definition of recursion } Form a dependency graph whose nodes are IDB predicates } Connect XàY iff there is a rule with X in the head and Y in the body } Cycle = recursion; no cycle = no recursion 67
68 Meaning of datalog rules } Modeltheore?c } Rules define a set of sa?sfying rela?ons } Whenever body is true, head is true } Prooftheore?c } Set of facts derivable from EDB rela?ons by applying the rules. 68
69 Evaluating recursive rules } This works if there is no nega?on } Start with all IDB rela?ons empty } Repeatedly evaluate the rules using the EDB and the previous IDB to get the new IDB } End when there is no change in the IDB rela?ons 69
70 Naïve evaluation algorithm Start: IDB = ; Apply rules to IDB, EDB yes Change to IDB? no done 70
71 Seminaïve evaluation } Since the EDB never changes, on each round we only get new IDB tuples if we use at least one IDB tuple obtained in the previous round } Saves work; lets us avoid rediscovering most known facts } Though a fact can s?ll be derived in more than one way 71
72 Par data: parent above child Round 1 a d Round 2 b c e Round 3 f g h j k i 72
73 Recursion + negation } Naïve evalua?on doesn t work when there are negated subgoals } Nega?on wrapped in recursion makes no sense in general } Even when they are separate, we can have ambiguity about the correct IDB rela?ons 73
74 Stratified negation } Stra?fica?on is a constraint usually placed on datalog with recursion and nega?on } It rules out nega?on wrapped inside recursion 74
75 Example P(x) : R(x) & Q(x) : R(x) & Q(x) P(x) } Suppose R = {(1)} } Two models sa?sfy the rules: } P = { }, Q={1} } P={1}, Q={ } 75
76 Stratum } Intui?vely, the stratum of an IDB predicate P is the maximum number of nega?ons that can be applied to an IDB predicate used in evalua?ng P } Stra?fied nega?on = finite strata 76
77 Stratum graph } Nodes = IDB predicates } Connect Aà B if predicate A depends on B } Label the edge  if the B subgoal is negated } The stratum is the maximum number of  edges on a path leading from that node } A datalog program is stra?fied if all its IDB predicates have finite strata 77
78 Example P(x) : R(x) & Q(x) : R(x) & Q(x) P(x) } Not stra?fied P   Q 78
79 The stratified model } When a datalog program is stra?fied, we can evaluate IDB predicates loweststratumfirst } Once evaluated, treat it as EDB for higher strata 79
80 Summary } Query complexity } Conjunc?ve queries } Containment, equivalence, minimality } Semijoin reduc?ons } Recursive datalog 80