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 in AHV notation, and each view is a conjunctive query . There are no comparison predicates, and set semantics holds. A rewriting of using the views is a conjunctive query whose body atoms are view atoms. Its expansion 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. is an equivalent rewriting if , that is, and .
Theorem (Levy, Mendelzon, Sagiv and Srivastava). Let have subgoals.
- Bounded size. If has an equivalent rewriting using the views, then it has one with at most view atoms.
- Complexity. The problem "given and the views, does 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: for conjunctive queries if and only if there is a containment mapping from to , a map on variables that sends the head of to the head of and each body atom of to a body atom of .
Lemma 1 (bounded size). Let be an equivalent rewriting of . Then has a sub-rewriting with at most view atoms that is also equivalent to .
Proof. Since , there is a containment mapping from to . Each of the subgoals of goes to an atom of the expansion of one view atom of . Let keep the head of and only these at most view atoms. The head of is , whose variables occur in the image atoms, so is safe. Then is also a containment mapping from to , so . The body of is a subset of the body of , so the identity on is a containment mapping into , and . Together, .
The rewriting has at most atoms, each with at most arguments for the largest view arity , 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 with at most view atoms, a containment mapping from to , and a containment mapping from to . The expansion has at most atoms, where 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.
Lemma 3 (NP-hardness with one view). Containment of Boolean conjunctive queries reduces to the problem in polynomial time.
Proof. Let and be Boolean conjunctive queries. Rename the variables of apart from those of , giving . Let and let the only view be . A rewriting is with one or more atoms, and the expansions of all of them are equivalent to , since copies of a Boolean conjunction add nothing. Always . A containment mapping from to exists if and only if one exists from to , since the two parts share no variables and the identity handles the part . So has an equivalent rewriting if and only if . Containment of Boolean conjunctive queries is NP-hard: a graph is 3-colorable if and only if the query "there is a triangle of colors" with the edges of in both directions is contained in the query that has one atom per edge of .
Proof of the theorem. Part 1 is Lemma 1. Part 2 is Lemmas 2 and 3, and Lemma 3 uses one view.
Consequences
- A finite search. Lemma 1 gives the brute-force algorithm: try all sets of at most view atoms over the variables of , 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 is tight. Let with views . Each occurs in only one view, so each equivalent rewriting needs at least one atom for each , that is, 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 for still needs 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag PG4SYW Edit this page History Report an error Markdown