Presentation is loading. Please wait.

Presentation is loading. Please wait.

Containment CSE 590 DB Rachel Pottinger. Outline zIntroduction zMotivation zFormal definition zAlgorithms for different complexities zAn application:

Similar presentations


Presentation on theme: "Containment CSE 590 DB Rachel Pottinger. Outline zIntroduction zMotivation zFormal definition zAlgorithms for different complexities zAn application:"— Presentation transcript:

1 Containment CSE 590 DB Rachel Pottinger

2 Outline zIntroduction zMotivation zFormal definition zAlgorithms for different complexities zAn application: rewriting queries using views

3 Containment, what is it? zFor two queries, Q 1 and Q 2, if all of the answers to Q 1 are a subset of those for Q 2 for all databases, then Q 1 is contained in Q 2. zDenoted as Q 1  Q 2. zFor general datalog, this is undecidable (by reduction from decision problems for context free languages)

4 Why should I care? zContainment is useful in a number of situations, including: yQuery minimization yIndependence of queries using updates yRewriting queries using views  Interesting logic problem

5 More definitions zEquivalence of queries: Q1  Q2 if they return the same answers for all databases. This is the same as Q 1  Q 2 and Q 2  Q 1 zConjunctive query - a query that is formed only of conjunctions of predicates. zQ(X,Y):- e(X,Z),e(Z,Y)

6 Containment Mapping zLet Q 1 and Q 2 be two conjunctive queries yQ 1 : I :- J 1, …, J l yQ 2 : H :- G 1, …, G k zA symbol mapping h is said to be a containment mapping if h turns Q 2 into Q 1 ; that is, h(H)= I, and for each i = 1,2,…,k, there is some j such that h(G i )=J j. There is no requirement that each J j be the target of some G i

7 Proof Sketch zIf there’s a containment mapping from Q 2 to Q 1, then Q 1  Q 2 ySuppose  maps Vars(Q 2 )  Vars(Q 1 ) yLet D be a database and  be an answer y  is a mapping from Vars(Q 1 )  D y   Vars(Q 2 )  D zThe rest of the proof follows later

8 Example of homomorphism rules zQ 1 : fp(X,Y) :- e(Y,X), e(X,Z) zQ 2 : fp(A,B) :- e(B,A), e(C,A),e(A,D) zFor Q 1  Q 2, map from Q 2 to Q 1

9 Test for containment of a conjunctive query (Q 1  Q 2 )  Freeze the body of Q 1, and put this into a canonical database  Apply Q 2 to the canonical database  If Q 1 can be derived from Q 2 on the canonical database, then Q 1  Q 2, otherwise not

10 A chilling example Q 1 : p(X,Z) :- a(X,Y), a(Y,Z) Q 2 : p(X,Z) :- a(X,U), a(V,Z) Canonical Database of Q 1

11 Proof continued zIf Q 1  Q 2,then there is a containment mapping ySince Q 1  Q 2, we know that if we apply Q 2 to the canonical database formed from Q 1, we’ll get back the same fact we got from applying it to Q 1, which makes a mapping from Q 2 to Q 1.

12 Conjunctive queries with negation zNegation in the heads of the subgoals, ie: Q(X,Y):- e(X,Z),  e(Z,Y) zThe Levy and Sagiv test looks at an exponential number of canonical databases, thus is  P 2 complete  Consider all partitions of Q 1 ; form canonical databases for all of them, D 1, … D k Ë For each database D i, see if the database makes all subgoals of Q 1 true. Ì For all D i ’s passing step 2, see if it the head of Q 1 can be derived by applying Q 2 Í If so, then Q 1  Q 2, else not

13 A negative example zQ 1 : p(X,Z):-a(X,Y), a(Y,Z),  a(X,Z) zQ 2 : p(A,C):-a(A,B),a(B,C),  a(A,D)

14 Conjunctive Queries with Arithmetic Comparisons zQ(X,Y):-e(X,Z),e(Z,Y), Z < Y zTreat the same as the negated subgoals, only a check must be made for each ordering of each partition zAlso  P 2 complete for dense domain such as reals

15 Example with arithmetic comparisons zQ1:p(X,Z):-a(X,Y), a(Y,Z), X < Y zQ2:p(A,C):-A(A,B),A(B,C), A < C zfalse, see x = z = 0, y = 1

16 Other complexity results z  queries restricted to queries Q1 and Q2 such that all database predicates have arity at most 2 and every database predicate occurs at most three times in the body of Q1 -  P 2 zConjunctive queries where Q1 is fixed- NP complete zConjunctive queries where Q2 is fixed - polynomial zConjunctive query containment where Q2 is an acyclic query - polynomial time zConjunctive queries where every database predicate occurs at most twice in the body of Q1 - linear time

17 Rewriting Queries Using Views zUseful in query optimization zGood for query minimization zNeeded to make the best use of cached information zNecessary in data integration

18 Views zA view is a relation that is not part of the conceptual model, but is visible to the user. zUseful for common expressions, or protecting data zExample: If you had faculty(name, office, ssn) you may want students to access faculty_office(name, office)

19 Views (con’t.) zViews can be either materialized or virtual zIn data integration, data sources can be thought of as views

20 An example of rewriting queries using views zSuppose you had two databases: yOne has famous people and whether they are right or left handed yOne has the birthdays of famous people zYou want the birthdays of all of the lefties

21 Containment in rewriting zQuery of q(X):-e(X,Y), e(Y,X) zView of v(A,B):- e(A,C),e(C,B)

22 zQ(x,u):-p(x,y),p 0 (y,z),p 1 (x,w),p 2 (w,u) zV 1 (a,b):-p(a,c),p 0 (c,b),p 1 (a,d) zV 2 (a,b):-p 1 (a,b) zV 3 (a,b):-p 2 (a,b) A more complicated example


Download ppt "Containment CSE 590 DB Rachel Pottinger. Outline zIntroduction zMotivation zFormal definition zAlgorithms for different complexities zAn application:"

Similar presentations


Ads by Google