Knowledge graphs · Comparison

Materialise or infer? Three ways to handle hierarchies in a knowledge graph

Almost every useful knowledge graph has hierarchies: product categories, organisational units, diagnoses, regulations. The moment someone asks for everything under a node, you face a design choice that shapes performance, correctness and cost for years: store every ancestor relationship, walk the hierarchy at query time, or let a reasoner infer it. Here is how the three compare, using one of the largest production examples in healthcare.

ComparisonSumanta Mukhopadhyay8 min read5 sources
#knowledge-graph#ontology#architecture
COMPARISON · KNOWLEDGE GRAPHS · COMPARISON24 Nov 2026 · 21:20 GST

The question every hierarchy eventually gets

"Show me everything under this." Every patient with any form of heart disease. Every product in any subcategory of outdoor equipment. Every control that inherits from a regulation's article. Direct parent links answer "what is immediately above this?". They do not answer "what is anywhere below this?" without walking the tree.

There are three ways to provide that answer, and real systems usually combine them. The right mix depends on how often the hierarchy changes, how deep it is, how often deep questions are asked and how many kinds of relationship need to combine.

Materialise
Closure tableevery ancestor pair storedFast readsone lookupCostly changerecompute on update
Compute at query time
Recursive queriesproperty paths, recursive CTEsAlways currentno rebuildSlower deep readswalk each time
Reason
Rule-based inferenceRDFS, OWL 2 RLCombines rulesnot just one hierarchyNeeds disciplineprofiles, testing
Three ways to answer "everything under this", each trading freshness, speed and expressiveness differently.

Option 1: materialise the closure

The OMOP Standardized Vocabularies are a good production example. Direct hierarchical links between clinical concepts are stored as "Is a" and "Subsumes" relationships, and every higher-degree ancestry is inferred and stored in a separate CONCEPT_ANCESTOR table, with the minimum and maximum number of steps between ancestor and descendant1. Every concept is also stored as its own descendant at zero steps, which makes "this concept or anything below it" a single join.

Diagram of the hierarchy above and below atrial fibrillation, from specific types such as persistent and paroxysmal atrial fibrillation up through fibrillation, supraventricular arrhythmia, cardiac arrhythmia and heart disease to disease of the cardiovascular system, with labels for concepts, direct relationships, ancestry relationships and levels of separation
Direct relationships plus a materialised ancestor table with levels of separation: how OMOP answers "everything under this" in one lookup. Source: The Book of OHDSI, Standardized Vocabularies · CC0 1.0

This works because the vocabulary changes in releases, not continuously, and because cohort definitions query descendants constantly. Rebuilding the closure at release time is cheap compared with walking the hierarchy in every analytical query across many databases. The trade-off is that the closure is only as current as the last rebuild, and its size grows with depth and breadth.

Option 2: compute at query time

Graph and relational query languages can walk hierarchies directly. SPARQL 1.1 property paths let a query follow a relationship one or more times, such as every subclass of a class at any depth2. In SQL, a recursive common table expression does the same over a parent-child table5.

The advantage is that answers always reflect the current data, with nothing to rebuild. The cost is paid on every deep query, and it grows with depth, fan-out and concurrency. For a hierarchy of a few levels that changes daily, such as an organisation chart or a product catalogue under active editing, this is often the right default.

Option 3: let a reasoner infer

Reasoning goes further than one hierarchy. RDF Schema semantics already define entailments such as subclass transitivity and inheriting types through subclass relationships4. OWL adds property characteristics, equivalences and more, and its profiles exist to keep reasoning tractable: OWL 2 RL, for example, is designed so that inference can be implemented with rule-based technology3.

This earns its keep when several rules interact: a part-of relationship that is transitive, combined with a subclass hierarchy, combined with an equivalence between two vocabularies. Writing those combinations into queries by hand is where errors creep in. The discipline required is real: choose a profile, test inferences against expected results, and watch for unintended conclusions from loosely modelled axioms. See SKOS versus OWL and SHACL validation.

Side by side

QuestionMaterialiseCompute at query timeReason
Read speed for deep queriesFastestSlows with depthFast if results are materialised
FreshnessAs of last rebuildAlways currentAs of last inference run, or live if computed
Change costRebuild affected closureNoneRe-run inference
StorageGrows with depth and breadthMinimalGrows with inferred facts
ExpressivenessOne relationship type at a timeWhat you write in the queryRules combine across relationships
Best fitReleased vocabularies, analyticsSmall or fast-changing hierarchiesInteracting rules, multiple ontologies

How I choose

  1. Change rateDoes the hierarchy change in releases or continuously?
  2. Query shapeHow often are deep "everything under" questions asked?
  3. Rule countIs it one hierarchy, or several relationships that must combine?
  4. ScaleHow large would the closure or inferred graph become?
  5. HybridMaterialise the hot, stable paths; compute or reason over the rest
  6. TestKeep expected answers as tests, whichever approach you choose
Most production graphs end up hybrid: materialise what is stable and hot, compute what changes.

Whatever you choose, store levels of separation or provenance with inferred links. "Is this a direct child or five steps away?" is a question users ask, and it is expensive to answer after the fact.

The same thinking applies when graphs feed AI systems. A retrieval layer that expands "heart disease" to its descendants before searching documents needs fast, correct closure, which is why materialised hierarchies often sit underneath GraphRAG and semantic layers. For background, see ontology versus knowledge graph versus semantic layer and clinical terminologies in OMOP.

If you are designing a knowledge graph that has to be both correct and fast, I am a fractional CTO and AI architect based in India, the United States and the UAE, working globally. You can analyse your challenge and bring the result to a call.

References
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
FAQ

Common questions

What is a transitive closure table?

A table that stores every ancestor and descendant pair in a hierarchy, not just direct parent links, often with the number of steps between them. Queries for everything under a node become a simple lookup instead of a recursive walk.

When should hierarchies be computed at query time?

When the hierarchy changes often, is small or shallow, or when you rarely need deep traversals. SPARQL property paths and recursive SQL queries make this straightforward, at the cost of slower deep queries.

When is a reasoner worth it?

When inferences come from several rules interacting, such as class hierarchies combined with property characteristics, and you need those rules applied consistently. Use a tractable profile such as OWL 2 RL and materialise its results where performance matters.

Keep reading

Related

Open to advisory, fractional CTO & select builds

Let's talk about what you're building.

The fastest route is a free 30-minute call. Prefer to write? Send a message and I'll reply personally within two business days.

Start a conversation

How would you like to reach me?

Book a 30-min callFastest. Pick any open slot on my calendar.→ Write in GmailOpens a pre-filled draft in your browser.→ Use my email appOutlook, Apple Mail or your default client.→

I reply personally within two business days. Time zones from the Americas and Europe to the Middle East and Asia-Pacific.