Materialization and Virtualization: Two Ways to Query Source Data Through an Ontology
#data-virtualization#semantic-web#knowledge-graph#materialization#sparql
Contents
Assume a relational database has customer and order tables.
| Table | Columns |
|---|---|
customers | id, name, tier |
orders | id, 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.
- Materialization: convert the data into RDF ahead of time according to the mapping, store it, and run the query on the stored RDF.
- 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.
| Approach | When the graph is built | Where the data lives | Typical tools |
|---|---|---|---|
| Materialization | at load time | triple store (RDF copy) | RML engine (Morph-KGC, etc.) + triple store |
| Virtualization | at query time | source DB | Ontop |
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.
2. How Materialization Works
- Run the mapping: the mapping engine reads the source DB and generates triples according to the mapping.
- Load: the triples are exported to a file such as N-Triples and loaded into the triple store.
- 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
- Receive the query: the VKG engine receives a SPARQL query.
- Query rewriting: each pattern in the query is looked up in the mapping and turned into SQL.
ex:tier "VIP"becomes the conditiontier = 'VIP', andex:placedBybecomes the joinorders.customer_id = customers.id. - Run: the source DB runs the generated SQL.
- Convert results: the
idvalues 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';
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 | Virtualization | |
|---|---|---|
| Where data lives | RDF copy in the triple store | only in the source DB |
| Point in time of results | last load | query time |
| Query speed | depends on the triple store; good for repeated queries | depends on the generated SQL and the source DB |
| Reasoning | triple store reasoning over all data | limited to what can be turned into SQL (Ontop: RDFS, OWL 2 QL) |
| Load on the source | at load time | on every query |
| Storage | extra space for the RDF copy | mapping definitions only |
| Source outage | queries still work on loaded data (not current) | queries fail |
| Mapping changes | take effect after reloading | take 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.
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.
| Question | Materialization side | Virtualization side |
|---|---|---|
| How often is the same query run? | often, heavily | occasionally |
| How fresh must results be? | can lag by the load interval | must 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? | yes | no (regulation, ownership, size) |
| Does the mapping change often? | stable | often (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.