In the ideal vision of the Semantic Web, a user asks a high-level question about their domain, and an intelligent system translates that question into a precise database query. This paradigm, known as Ontology-Based Data Access (OBDA) or Ontology-Mediated Querying (OMQ), allows us to query data through a conceptual "map" (an ontology) that accounts for background knowledge, such as "every professor is a faculty member" or "every heart is part of a human body."
However, a massive gap exists between the elegant logic of ontologies and the practical reality of SQL databases. When we try to "compile" an ontological query into standard SQL, the resulting query often experiences an exponential size explosion. This "blowup" makes the queries impossible for standard database engines to execute, even when the underlying data complexity is theoretically low.
The Mechanism of Explosion
Why does a simple query become a monster when an ontology is involved? The culprit is usually the existential rule. Consider an ontology for a medical database that includes the rule: Every Human has a Heart. If a user asks for a list of all "objects that have a heart," a standard SQL query would just look for explicit entries in a HasHeart table.
But in OBDA, the system must also return every person in the Human table, even if they aren't explicitly linked to a heart in the data. To handle this, a "pure" query rewriter must expand the original query to include every possible way the ontology could satisfy the request. As Carsten Lutz, David Toman, and Frank Wolter note in Conjunctive Query Answering in EL using a Database System, incorporating the entire ontology into the query itself leads to a combinatorial explosion of sub-queries, often resulting in a Union of Conjunctive Queries (UCQ) that standard optimizers simply cannot process.
The Succinctness Frontier
Researchers have spent the last decade mapping the "succinctness landscape" to determine when a manageable rewriting is even possible. Stanislav Kikot, Roman Kontchakov, and Michael Zakharyaschev proved in Long Rewritings, Short Rewritings that for certain common ontologies (like OWL 2 QL), "pure" rewritings into First-Order logic (SQL) are doomed to be exponentially large.
This isn't just a matter of inefficient algorithms; it's a fundamental limit tied to Boolean circuit complexity. In Ontology-Mediated Queries: Combined Complexity and Succinctness of Rewritings via Circuit Complexity, the authors demonstrate that the size of a rewritten query is often linked to the depth of the ontology. Once an ontology involves even moderate chaining of rules (a depth of two or more), the resulting SQL can grow triple-exponentially relative to the input, as explored by Meghyn Bienvenu and others in Deciding FO-Rewritability in EL.
Beyond SQL: Datalog and Structure Sharing
If SQL is too rigid to hold these complex rewritings, what is the alternative? One promising direction is Datalog, the logical foundation of recursive SQL.
Datalog allows for "impure" rewritings that use recursion and intermediate variables to represent the ontology more compactly. Ian Horrocks and Boris Motik proposed a two-step technique in Small Datalog Query Rewritings for EL that separates the rewriting of the ontology from the user query. This separation prevents the combinatorial explosion that occurs when trying to rewrite both simultaneously.
Another strategy is structure sharing. Just as a programmer uses functions to avoid repeating code, a query rewriter can use Directed Acyclic Graphs (DAGs) to represent common sub-expressions. Peng Xiao and Kewen Wang demonstrated in Practical Datalog Rewriting for Existential Rules that by sharing graph-based structures, their system Drewer could produce rewritings significantly smaller than traditional methods.
Pruning and Data-Dependent Optimization
While theoretical work focuses on the worst-case scenario, practical systems often find that real-world data is much simpler. Modern research is shifting toward data-dependent optimization.
Instead of generating a massive query that is valid for any possible database, systems can use "database summaries" to prune irrelevant parts of the query. As described in Query Optimization for Ontology-Mediated Query Answering, this approach can identify branches of a query that will never return results for a specific dataset, allowing the system to discard them before execution. This pruning can lead to speedups of up to three orders of magnitude, finally making the dream of semantic querying practical for large-scale systems.