Algorithm Search⌘K

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 Q(xˉ) :- R1(uˉ1),…,Rn(uˉn)Q(\bar x) \text{ :- } R_1(\bar u_1), \ldots, R_n(\bar u_n) over a schema, in AHV notation, and a set V\mathcal V of views, each 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) over the same schema. Set semantics holds. For a query PP over the view names, the expansion PexpP^{exp} replaces each view atom by the body of its view, with new variables for the existential variables of the view.

Rewritings.

  • PP is an equivalent rewriting of QQ if Pexp≡QP^{exp} \equiv Q: on every database, PP evaluated on the views gives QQ.
  • PP is a contained rewriting if Pexp⊆QP^{exp} \subseteq Q: it never gives a wrong answer.
  • PP is a maximally contained rewriting in a query language L\mathcal L if P∈LP \in \mathcal L is contained and every contained rewriting in L\mathcal L is contained in PP.

Containment is tested by the Chandra-Merlin theorem when PP is a conjunctive query or a union of them.

Certain answers. A second form of the problem fixes a view instance II, a set of tuples for each view. Under the open world assumption, a database DD is possible if I(V)⊆V(D)I(V) \subseteq V(D) for each view, and under the closed world assumption, if I(V)=V(D)I(V) = V(D). The certain answers are the tuples that QQ returns on every possible DD. 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 Q(x) :- cites(x,y),cites(y,x),sameTopic(x,y)Q(x) \text{ :- } \text{cites}(x, y), \text{cites}(y, x), \text{sameTopic}(x, y) and the views

  • V4(a) :- cites(a,b),cites(b,a)V_4(a) \text{ :- } \text{cites}(a, b), \text{cites}(b, a),
  • V5(c,d) :- sameTopic(c,d)V_5(c, d) \text{ :- } \text{sameTopic}(c, d),
  • V6(f,h) :- cites(f,g),cites(g,h),sameTopic(f,g)V_6(f, h) \text{ :- } \text{cites}(f, g), \text{cites}(g, h), \text{sameTopic}(f, g).
candidate expansion status
Q′(x) :- V6(x,x)Q'(x) \text{ :- } V_6(x, x) cites(x,g)(x, g), cites(g,x)(g, x), sameTopic(x,g)(x, g) equivalent to QQ
Q′(x) :- V4(x),V5(x,y)Q'(x) \text{ :- } V_4(x), V_5(x, y) cites(x,b)(x, b), cites(b,x)(b, x), sameTopic(x,y)(x, y) not contained: nothing says y=by = b
Q′(x) :- V6(x,x),V4(x)Q'(x) \text{ :- } V_6(x, x), V_4(x) the first one plus cites(x,b′)(x, b'), cites(b′,x)(b', x) equivalent too, but the V4V_4 atom is redundant

The first rewriting is equivalent to QQ, so it is also a maximally contained rewriting among unions of conjunctive queries. The third one maps onto the first with b′↦gb' \mapsto g, so dropping its V4V_4 atom keeps it equivalent.

For the chain Q(x,w) :- e(x,y),e(y,z),e(z,w)Q(x, w) \text{ :- } e(x, y), e(y, z), e(z, w) with only the view V1(a,c) :- e(a,b),e(b,c)V_1(a, c) \text{ :- } e(a, b), e(b, c), 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 QQ cannot map into it. The maximally contained rewriting is the empty union. Adding V2(a,b) :- e(a,b)V_2(a, b) \text{ :- } e(a, b) gives the equivalent rewriting Q′(x,w) :- V2(x,y),V2(y,z),V2(z,w)Q'(x, w) \text{ :- } V_2(x, y), V_2(y, z), V_2(z, w). The rewritings Q′(x,w) :- V1(x,y),V2(y,w)Q'(x, w) \text{ :- } V_1(x, y), V_2(y, w) and Q′(x,w) :- V2(x,y),V1(y,w)Q'(x, w) \text{ :- } V_2(x, y), V_1(y, w) 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 nn 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.
AlgorithmTimeSpacePropertiesCode
Bucket algorithmO(a n (k m)^(2n) (n m)^(3n)) worstO(a n (k m)^n (n m)^n) extradeterministicexactnot in placeofflineiterative1 language
Inverse rules algorithmO(N^(r(b+1))) worstO(N^r) extradeterministicexactnot in placeofflineiterative1 language
MiniCon algorithmO(a n k (m+1)^n (n k m+1)^n) worstO(a n k (m+1)^n) extradeterministicexactnot in placeofflinerecursive1 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 nn 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 nn.
  • 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 ≠\ne, 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 V(Yˉ)→∃zˉ body(V)V(\bar Y) \to \exists \bar z\, \text{body}(V), 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

  1. 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
  2. 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
  3. 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
  4. 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.

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)
WritingW2 basic
  • a contract.yaml