Algorithm Search⌘K

Theoremadvanced

NP-completeness of rewriting queries using views

Also called Levy-Mendelzon-Sagiv-Srivastava theorem.

Deciding whether a conjunctive query has an equivalent rewriting using conjunctive views is NP-complete, and a rewriting needs no more view atoms than the query has subgoals.

Levy, Mendelzon, Sagiv and Srivastava proved in 1995 that deciding whether a conjunctive query can be computed exactly from a set of conjunctive views is NP-complete. The key step is a size bound: if an equivalent rewriting exists, one exists with at most as many view atoms as the query has subgoals. The theorem is about equivalent rewritings, the ones used to optimize queries with materialized views. The algorithms for data integration, such as MiniCon, look for maximally contained rewritings instead, which always exist, so for them the question is how large they are, not whether they exist.

Statement

A query is Q(xˉ) :- R1(uˉ1),…,Rn(uˉn)Q(\bar x) \text{ :- } R_1(\bar u_1), \ldots, R_n(\bar u_n) in AHV notation, and each view is a conjunctive query V(Yˉ) :- S1(wˉ1),…,Sr(wˉr)V(\bar Y) \text{ :- } S_1(\bar w_1), \ldots, S_r(\bar w_r). There are no comparison predicates, and set semantics holds. A rewriting of QQ using the views is a conjunctive query PP whose body atoms are view atoms. Its expansion PexpP^{exp} replaces each view atom by the body of the view, with the head variables replaced by the arguments of the atom and the existential variables replaced by new variables. PP is an equivalent rewriting if Pexp≡QP^{exp} \equiv Q, that is, Pexp⊆QP^{exp} \subseteq Q and Q⊆PexpQ \subseteq P^{exp}.

Theorem (Levy, Mendelzon, Sagiv and Srivastava). Let QQ have nn subgoals.

  1. Bounded size. If QQ has an equivalent rewriting using the views, then it has one with at most nn view atoms.
  2. Complexity. The problem "given QQ and the views, does QQ have an equivalent rewriting using them?" is NP-complete in combined complexity. It stays NP-hard with a single view.

The paper also studies partial rewritings, which may mix view atoms with atoms of the base relations. This page proves the statement for rewritings that use views only.

In plain words

You want to answer a question using only some stored answers to other questions. The theorem says two things. First, if it can be done at all, you never need more stored tables than the question has parts, so the search space is finite and small to describe. Second, the search is as hard as the hardest problems whose answers are easy to check: a proposed plan can be checked quickly, but finding one can take time that grows exponentially in the size of the question. This is no worse than checking whether two questions always give the same answers, which is already this hard.

Proof

The proof uses the Chandra-Merlin theorem: A⊆BA \subseteq B for conjunctive queries if and only if there is a containment mapping from BB to AA, a map on variables that sends the head of BB to the head of AA and each body atom of BB to a body atom of AA.

Lemma 1 (bounded size). Let PP be an equivalent rewriting of QQ. Then PP has a sub-rewriting P′P' with at most nn view atoms that is also equivalent to QQ.

Proof. Since Pexp⊆QP^{exp} \subseteq Q, there is a containment mapping τ\tau from QQ to PexpP^{exp}. Each of the nn subgoals of QQ goes to an atom of the expansion of one view atom of PP. Let P′P' keep the head of PP and only these at most nn view atoms. The head of PP is τ(xˉ)\tau(\bar x), whose variables occur in the image atoms, so P′P' is safe. Then τ\tau is also a containment mapping from QQ to P′expP'^{exp}, so P′exp⊆QP'^{exp} \subseteq Q. The body of P′P' is a subset of the body of PP, so the identity on P′expP'^{exp} is a containment mapping into PexpP^{exp}, and Pexp⊆P′expP^{exp} \subseteq P'^{exp}. Together, Q⊆Pexp⊆P′exp⊆QQ \subseteq P^{exp} \subseteq P'^{exp} \subseteq Q. □\square

The rewriting P′P' has at most nn atoms, each with at most aa arguments for the largest view arity aa, so after renaming its variables it has size polynomial in the input.

Lemma 2 (membership in NP). The problem is in NP.

Proof. A certificate is a rewriting P′P' with at most nn view atoms, a containment mapping from QQ to P′expP'^{exp}, and a containment mapping from P′expP'^{exp} to QQ. The expansion has at most nmn m atoms, where mm bounds the size of a view body, so all three objects have polynomial size, and checking that two maps are containment mappings takes polynomial time. By Lemma 1, a certificate exists whenever an equivalent rewriting exists. □\square

Lemma 3 (NP-hardness with one view). Containment of Boolean conjunctive queries reduces to the problem in polynomial time.

Proof. Let AA and BB be Boolean conjunctive queries. Rename the variables of BB apart from those of AA, giving B′B'. Let Q() :- body(A)Q() \text{ :- } \text{body}(A) and let the only view be V() :- body(A),body(B′)V() \text{ :- } \text{body}(A), \text{body}(B'). A rewriting is Q′() :- V(),…,V()Q'() \text{ :- } V(), \ldots, V() with one or more atoms, and the expansions of all of them are equivalent to A∧B′A \wedge B', since copies of a Boolean conjunction add nothing. Always A∧B′⊆AA \wedge B' \subseteq A. A containment mapping from A∧B′A \wedge B' to AA exists if and only if one exists from B′B' to AA, since the two parts share no variables and the identity handles the part AA. So QQ has an equivalent rewriting if and only if A⊆BA \subseteq B. Containment of Boolean conjunctive queries is NP-hard: a graph GG is 3-colorable if and only if the query "there is a triangle of colors" with the edges of K3K_3 in both directions is contained in the query that has one atom per edge of GG. □\square

Proof of the theorem. Part 1 is Lemma 1. Part 2 is Lemmas 2 and 3, and Lemma 3 uses one view. ■\blacksquare

Consequences

  • A finite search. Lemma 1 gives the brute-force algorithm: try all sets of at most nn view atoms over the variables of QQ, and test each for equivalence. The bucket algorithm and MiniCon organize this search.
  • Hardness is inherited from containment. The problem is no harder than query containment, and Lemma 3 shows it is no easier. Classes of queries where containment is in polynomial time, such as acyclic queries, are natural places to look for easier cases.
  • Minimal rewritings. The rewriting of Lemma 1 can be further minimized by dropping atoms while equivalence holds, as for any conjunctive query.
  • View selection. Choosing which views to store so that a workload has equivalent rewritings, the view selection problem, builds on this test.

Tightness

  • The size bound nn is tight. Let Q(x1,…,xn) :- p1(x1),…,pn(xn)Q(x_1, \ldots, x_n) \text{ :- } p_1(x_1), \ldots, p_n(x_n) with views Vi(x) :- pi(x)V_i(x) \text{ :- } p_i(x). Each pip_i occurs in only one view, so each equivalent rewriting needs at least one ViV_i atom for each ii, that is, nn atoms.
  • No smaller bound in terms of the views. The bound does not depend on the number or the size of the views, and it cannot be improved to the number of views: in the previous example a single view V(x) :- p(x)V(x) \text{ :- } p(x) for Q(x1,…,xn) :- p(x1),…,p(xn)Q(x_1, \ldots, x_n) \text{ :- } p(x_1), \ldots, p(x_n) still needs nn atoms.
  • Comparisons and other languages. With comparison predicates, unions or recursion in the query or the views, the bound and the complexity change, and the proof above does not apply as stated.

History

Chandra and Merlin proved in 1977 that containment and equivalence of conjunctive queries are decidable by homomorphisms and NP-complete. Levy, Mendelzon, Sagiv and Srivastava studied the use of views for query answering at PODS 1995. They proved the bound on the size of rewritings and the NP-completeness of finding rewritings, for rewritings that use only views and for partial ones. Halevy's 2001 survey made the result the starting point of the theory of answering queries using views.

References

  1. Alon Y. Levy, Alberto O. Mendelzon, Yehoshua Sagiv and Divesh Srivastava, Answering queries using views, PODS 1995, pages 95-104, the bound on the size of a rewriting and the NP-completeness of finding complete and partial rewritings
  2. Alon Y. Halevy, Answering queries using views: a survey, The VLDB Journal 10(4), 2001, pages 270-294, the section on the complexity of finding rewritings
  3. Ashok K. Chandra and Philip M. Merlin, Optimal implementation of conjunctive queries in relational data bases, STOC 1977, pages 77-90, containment of conjunctive queries by homomorphisms and its NP-completeness

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/PG4SYW/text.yaml