Materialization and Virtualization: Two Ways to Query Source Data Through an Ontology

architecture7 min한국어

#data-virtualization#semantic-web#knowledge-graph#materialization#sparql

Assume a relational database has customer and order tables.

TableColumns
customersid, name, tier
ordersid, customer_id, amount

You want to query this data using ontology concepts and relations such as ex:Customer, ex:Order and ex:placedBy.

A mapping is a set of rules that says which ontology concepts and relations the rows and columns of a table become.

Here is the customer mapping written in R2RML, the W3C standard.

# Each row of the customers table becomes one ex:Customer
<#CustomerMap> a rr:TriplesMap ;
  rr:logicalTable [ rr:tableName "customers" ] ;
  rr:subjectMap [ rr:template "http://example.org/customer/{id}" ; rr:class ex:Customer ] ;
  rr:predicateObjectMap [ rr:predicate ex:tier ; rr:objectMap [ rr:column "tier" ] ] .

With the mapping, you can query “orders from VIP customers” with SPARQL.

PREFIX ex: <http://example.org/>
SELECT ?order ?amount WHERE {
  ?customer a ex:Customer ; ex:tier "VIP" .
  ?order ex:placedBy ?customer ; ex:amount ?amount .
}

There are two ways to answer this query.

  1. Materialization: convert the data into RDF ahead of time according to the mapping, store it, and run the query on the stored RDF.
  2. Virtualization: leave the source as it is, rewrite the query into SQL, and run it on the source DB.

1. Materialization and Virtualization

1.1 Materialization: Convert and Load in Advance

The mapping is executed to convert source data into RDF triples, which are loaded into a triple store.
SPARQL queries run on the loaded triples.
It is called materialization because a graph that existed only as a definition is turned into actual stored data.

A triple store is a database that stores RDF triples and runs SPARQL queries. Examples include GraphDB, Virtuoso and Stardog.

1.2 Virtualization: Query the Source Directly

The source data is not moved; the mapping only defines a “virtual RDF graph”.
When a SPARQL query arrives, it is rewritten into SQL for the source using the mapping, and the source DB runs it.
It is called virtualization because the graph is never actually stored and exists only as a definition.
Systems built this way are called virtual knowledge graphs (VKG) or OBDA (Ontology-Based Data Access).

Virtualization here has nothing to do with server virtualization (VMs). It means making a single graph appear to exist through definitions alone, without copying data.

ApproachWhen the graph is builtWhere the data livesTypical tools
Materializationat load timetriple store (RDF copy)RML engine (Morph-KGC, etc.) + triple store
Virtualizationat query timesource DBOntop

Morph-KGC is an open-source mapping engine from the Ontology Engineering Group at Universidad Politécnica de Madrid. It reads sources such as relational databases, CSV and JSON according to R2RML or RML mappings and generates RDF.

Ontop is an open-source virtual knowledge graph system that started at the Free University of Bozen-Bolzano in Italy. It presents a relational database as a knowledge graph through R2RML mappings, and rewrites incoming SPARQL queries into SQL that runs on the source DB.

ontology + mapping (R2RML) Materialization Virtualization source DB mapping engine generates RDF triple store RDF copy SPARQL load graph is built at load time SPARQL VKG engine rewrites to SQL source DB SQL rewritten to SQL at query time
Both approaches use the same ontology and mapping. Materialization queries pre-built RDF in a triple store; virtualization rewrites SPARQL into SQL and queries the source DB.

2. How Materialization Works

  1. Run the mapping: the mapping engine reads the source DB and generates triples according to the mapping.
  2. Load: the triples are exported to a file such as N-Triples and loaded into the triple store.
  3. Query: the triple store runs SPARQL queries directly.

If customer 7 is a VIP and placed order 1001, the mapping engine generates these triples.

# abbreviated with prefixes (rdf:, ex:, xsd:)
<http://example.org/customer/7> rdf:type ex:Customer .
<http://example.org/customer/7> ex:tier "VIP" .
<http://example.org/order/1001> ex:placedBy <http://example.org/customer/7> .
<http://example.org/order/1001> ex:amount "120000"^^xsd:integer .

Once loading is done, queries can be answered without the source DB.
The triple store’s reasoning features can also be applied to all of the data.

3. How Virtualization Works

  1. Receive the query: the VKG engine receives a SPARQL query.
  2. Query rewriting: each pattern in the query is looked up in the mapping and turned into SQL. ex:tier "VIP" becomes the condition tier = 'VIP', and ex:placedBy becomes the join orders.customer_id = customers.id.
  3. Run: the source DB runs the generated SQL.
  4. Convert results: the id values in the SQL result are put into the mapping’s IRI templates and returned as SPARQL results.

The SPARQL query above is rewritten into roughly this SQL.

SELECT o.id, o.amount
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE c.tier = 'VIP';
SPARQL patterns SQL ?customer a ex:Customer FROM customers c ?customer ex:tier "VIP" WHERE c.tier = 'VIP' ?order ex:placedBy ?customer JOIN orders o ON o.customer_id = c.id mapping SPARQL result: order/1001 SQL result: id = 1001 IRI template
Each SPARQL pattern becomes a table, condition or join in SQL through the mapping. The source DB runs the SQL, and the results are turned back into IRIs.

Since the source DB just runs ordinary SQL, nothing needs to be configured or installed on it.
The results come from the source DB’s live data, so there is nothing to refresh.

4. How Materialization and Virtualization Differ

The biggest difference is who reads the source DB, and when.

Materialization source DB mapping engine generates RDF triple store RDF copy SPARQL SQL query read once at load time on every query no requests reach the source DB at query time Virtualization source DB runs SQL VKG engine stores no triples SPARQL query sends rewritten SQL the source DB computes on every query
The source DB is read at different times. In materialization, the mapping engine reads it once at load time and the triple store handles queries. In virtualization, the source DB runs SQL on every query.
MaterializationVirtualization
Where data livesRDF copy in the triple storeonly in the source DB
Point in time of resultslast loadquery time
Query speeddepends on the triple store; good for repeated queriesdepends on the generated SQL and the source DB
Reasoningtriple store reasoning over all datalimited to what can be turned into SQL (Ontop: RDFS, OWL 2 QL)
Load on the sourceat load timeon every query
Storageextra space for the RDF copymapping definitions only
Source outagequeries still work on loaded data (not current)queries fail
Mapping changestake effect after reloadingtake effect from the next query
  • Materialization: spends time and storage on conversion and loading, in exchange for fast queries and unrestricted reasoning.
  • Virtualization: always sees current data because there is no copy, but the source DB does the work on every query.

5. Drawback of Materialization: Stale Data

When the source changes, the loaded triples become stale(old data that does not yet reflect changes in the source) until they are loaded again.
So using materialization starts with deciding when and how to reload.

  • Full reload: run the mapping from scratch and replace the whole graph. Simple, but slow when the data is large.
  • Incremental load: convert and apply only the rows that changed. Fast, but finding and deleting the triples of rows removed from the source needs separate handling.
  • Replace by named graph: if each source or load batch goes into its own named graph, you can drop and reload just that graph.
time value source, virtualization materialization reload reload source change source change
The materialization result (steps) follows the source only when reloaded. Between loads it lags behind the source.

6. Drawback of Virtualization: It Depends on Query Rewriting and Source DB Performance

  • Query rewriting results: when one concept is mapped from several tables, the generated SQL gets more UNIONs and joins. Complex generated SQL slows down the source DB.
  • Dependence on the source: if the source is slow or unresponsive, queries slow down or fail. Analytical queries put load directly on production databases.
  • Reasoning scope: only reasoning that can be turned into SQL is available. That is why OBDA uses lightweight ontologies such as OWL 2 QL, which is designed so queries can be rewritten.
  • Multiple sources: with several source databases, one SQL query is not enough. Federation is needed to split the query across sources and combine the results.

7. Combining the Two

The same R2RML mapping can be used for both approaches.
Ontop is a virtualization engine, but its ontop materialize command can also produce RDF files from the same mapping.

So in practice, some designs share the mapping and split the scope between what is loaded and what is queried from the source DB.

QuestionMaterialization sideVirtualization side
How often is the same query run?often, heavilyoccasionally
How fresh must results be?can lag by the load intervalmust reflect the source at query time
Is heavy reasoning needed?yes (beyond OWL 2 QL)light reasoning is enough
Can the source DB take the load?no (production DB)yes (replica, analytics DB)
Can the source be copied?yesno (regulation, ownership, size)
Does the mapping change often?stableoften (early design)
  • Start with virtualization. While refining the mapping, virtualization is easier because nothing needs reloading.
  • Load only the slow or frequently used parts. Move to materialization only the parts where the generated SQL is slow or heavy reasoning is needed.
  • Compare the results. When switching approaches, check that the same SPARQL query returns the same results both ways.

References