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.
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.

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
| Question | Materialise | Compute at query time | Reason |
|---|---|---|---|
| Read speed for deep queries | Fastest | Slows with depth | Fast if results are materialised |
| Freshness | As of last rebuild | Always current | As of last inference run, or live if computed |
| Change cost | Rebuild affected closure | None | Re-run inference |
| Storage | Grows with depth and breadth | Minimal | Grows with inferred facts |
| Expressiveness | One relationship type at a time | What you write in the query | Rules combine across relationships |
| Best fit | Released vocabularies, analytics | Small or fast-changing hierarchies | Interacting rules, multiple ontologies |
How I choose
- Change rateDoes the hierarchy change in releases or continuously?
- Query shapeHow often are deep "everything under" questions asked?
- Rule countIs it one hierarchy, or several relationships that must combine?
- ScaleHow large would the closure or inferred graph become?
- HybridMaterialise the hot, stable paths; compute or reason over the rest
- TestKeep expected answers as tests, whichever approach you choose
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.
- 1
- 2
- 3
- 4
- 5WITH Queries (Common Table Expressions)PostgreSQL documentation