Problemadvanced
Answering queries using views
Also called query rewriting using views, view-based query answering.
Rewrite a query over a schema into one that uses only a given set of views, either equivalently or as a maximally contained rewriting.
Answering queries using views asks how to compute a query when you can read only the stored results of some other queries, the views, and not the base relations. This page states the problem and its variants. The NP-completeness theorem covers the hardness of the equivalent version, and the pages on the bucket algorithm, MiniCon and inverse rules give the methods for the contained version. The problem is the core of local-as-view data integration, where each source is described as a view over a global schema, and of query optimization with materialized views.
In plain words
A database keeps a few tables of answers to earlier questions, and a new question comes in. Can the new question be answered from the stored tables alone? Sometimes the stored tables give exactly the right answers. Sometimes they give only part of them, because a table forgot a value that the question needs.
In query optimization you want the exact answers, so the question is whether an exact plan from the stored tables exists. In data integration, the stored tables are the sources and they are all you have, so you want the plan that returns as many right answers as the sources allow, and never a wrong one.
Definition
Input. A query over a schema, in AHV notation, and a set of views, each a conjunctive query over the same schema. Set semantics holds. For a query over the view names, the expansion replaces each view atom by the body of its view, with new variables for the existential variables of the view.
Rewritings.
- is an equivalent rewriting of if : on every database, evaluated on the views gives .
- is a contained rewriting if : it never gives a wrong answer.
- is a maximally contained rewriting in a query language if is contained and every contained rewriting in is contained in .
Containment is tested by the Chandra-Merlin theorem when is a conjunctive query or a union of them.
Certain answers. A second form of the problem fixes a view instance , a set of tuples for each view. Under the open world assumption, a database is possible if for each view, and under the closed world assumption, if . The certain answers are the tuples that returns on every possible . Here the output is a set of tuples, not a query.
Variants
- Equivalent or maximally contained. The first is the goal of query optimization, the second the goal of data integration.
- Complete or partial. A complete rewriting uses only views. A partial one may also use base relations, which matters in optimization, where the base relations are still there.
- The query language. The query and the views may be conjunctive queries, with or without comparisons, unions of them, or Datalog programs. The language of the rewriting matters too: for a recursive query, the maximally contained rewriting can need recursion.
- Open or closed world. The sources may hold only some of the tuples of their definition, or exactly those tuples.
- Access patterns. A source may answer only when some arguments are given, like a web form.
- Bag semantics. SQL counts duplicates, and rewritings under bags need stronger conditions. This page treats sets only.
Examples
Take and the views
- ,
- ,
- .
| candidate | expansion | status |
|---|---|---|
| cites, cites, sameTopic | equivalent to | |
| cites, cites, sameTopic | not contained: nothing says | |
| the first one plus cites, cites | equivalent too, but the atom is redundant |
The first rewriting is equivalent to , so it is also a maximally contained rewriting among unions of conjunctive queries. The third one maps onto the first with , so dropping its atom keeps it equivalent.
For the chain with only the view , no contained rewriting exists. In an expansion, each edge joins a view argument to a fresh middle variable, so every walk between two head variables has even length, and the path of length 3 in cannot map into it. The maximally contained rewriting is the empty union. Adding gives the equivalent rewriting . The rewritings and are equivalent too, and the bucket algorithm returns all three.
Known algorithms
- Bounded search for equivalent rewritings. By the theorem of Levy, Mendelzon, Sagiv and Srivastava, it suffices to try rewritings with at most view atoms and test each for equivalence.
- Bucket algorithm. For each subgoal, collect the views that can cover it, then test each combination for containment.
- MiniCon. Form, for each view, the smallest sets of subgoals it must cover together, then combine disjoint sets. No containment test is needed at the end.
- Inverse rules. Turn the views into Datalog rules with Skolem terms and evaluate the query. This handles recursive queries and gives the certain answers under the open world assumption.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Bucket algorithm | O(a n (k m)^(2n) (n m)^(3n)) worst | O(a n (k m)^n (n m)^n) extra | deterministicexactnot in placeofflineiterative | 1 language |
| Inverse rules algorithm | O(N^(r(b+1))) worst | O(N^r) extra | deterministicexactnot in placeofflineiterative | 1 language |
| MiniCon algorithm | O(a n k (m+1)^n (n k m+1)^n) worst | O(a n k (m+1)^n) extra | deterministicexactnot in placeofflinerecursive | 1 language |
Bounds
All results are for conjunctive views. Each is a theorem, and each says whether it is in data or combined complexity.
- Existence of an equivalent rewriting. NP-complete in combined complexity for conjunctive queries and views, with rewritings of at most view atoms (Levy, Mendelzon, Sagiv and Srivastava).
- Maximally contained rewriting. For a conjunctive query, it always exists among unions of conjunctive queries and can be computed, for example by MiniCon. The number of disjuncts can be exponential in .
- Certain answers, open world. For a conjunctive or Datalog query, polynomial time in data complexity, by the inverse rules or by evaluating the maximally contained rewriting (Duschka and Genesereth; Abiteboul and Duschka). For a conjunctive query with inequalities , coNP-complete in data complexity (Abiteboul and Duschka).
- Certain answers, closed world. For conjunctive queries and views, coNP-complete in data complexity (Abiteboul and Duschka).
Reductions
- From containment. Containment of Boolean conjunctive queries reduces to the existence of an equivalent rewriting with one view, which gives the NP-hardness.
- To containment. In the other direction, the bounded search reduces the existence of an equivalent rewriting to a polynomial-size guess and two containment checks.
- To data exchange. A local-as-view setting is a data exchange setting with the source-to-target dependencies , read in the open world. The certain answers are the same, and the instance built by the inverse rules is, after the Skolem terms are read as labeled nulls, the result of the chase.
- To certain answers. For conjunctive queries and views under the open world assumption, evaluating the maximally contained rewriting on the view instance gives exactly the certain answers.
Applications
- Data integration. Systems such as the Information Manifold described each web source as a view over a global schema and used the bucket algorithm to plan queries.
- Query optimization. Database systems use materialized views to answer queries faster, which needs equivalent rewritings, often partial ones.
- View selection. Choosing which views to store for a workload needs to know which queries the views can answer.
- Semantic caching. A client keeps answers of earlier queries and answers new queries from them when it can.
References
- Alon Y. Halevy, Answering queries using views: a survey, The VLDB Journal 10(4), 2001, pages 270-294, the definitions of equivalent and maximally contained rewritings, the algorithms and the complexity results
- Alon Y. Levy, Alberto O. Mendelzon, Yehoshua Sagiv and Divesh Srivastava, Answering queries using views, PODS 1995, pages 95-104, equivalent rewritings and their NP-completeness
- Serge Abiteboul and Oliver M. Duschka, Complexity of answering queries using materialized views, PODS 1998, the complexity of certain answers under the open and the closed world assumption
- AnHai Doan, Alon Halevy and Zachary Ives, Principles of Data Integration, Morgan Kaufmann, 2012, Chapter 2, manipulating query expressions, and Chapter 3, describing data sources
Where this leads
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 | W2 basic |
|
Tag M2VGDE Edit this page History Report an error Markdown