The Hybrid Hierarchy Model - The Right Database Design for Pharma T&T Systems
After many years working with production T&T systems across multiple markets, I've arrived at a two-table hybrid model that separates operational state from structural relationships. Each table does one job and does it fast.
Modeling aggregation hierarchy in a pharma track and trace system sounds straightforward. Unit inside carton, carton inside case, case on pallet.
The complexity is in the operations that need to run against that hierarchy, at scale, in real time.
After many years working with production T&T systems across multiple markets, I've arrived at a two-table hybrid model that separates operational state from structural relationships. Each table does one job and does it fast.
Table 1: Top Parent (Operational Anchor)
This table stores one record per top-level entity, typically the pallet or shipment unit. It carries all operational metadata: status, shipment file reference, location, dispatch timestamps.
Every operation that doesn't need to traverse the hierarchy hits this table directly. Shipping a pallet, updating status, grouping serials for regulatory reporting, marking a batch as dispatched, all of these are single indexed queries on Table 1. No joins, no traversal, no recursion.
This is the high-frequency surface of the system. It needs to be fast, flat, and clean.
Table 2: Hierarchy Closure (Structural Layer)
This table stores every ancestor/descendant relationship in the hierarchy, at every depth. Not just direct parent/child, every possible pair, with a depth value indicating how many levels apart they are.
This is the Closure Table pattern. It makes hierarchy traversal a single indexed JOIN regardless of depth. Finding every unit on a pallet, reconstructing the full chain of custody for an audit, or handling disaggregation, all of these become straightforward queries with no recursion.
Why this combination works
Most hierarchy designs force a tradeoff: optimize for reads or optimize for writes. The hybrid model avoids that tradeoff by separating concerns completely.
Operational queries never touch Table 2. Structural queries never need Table 1's operational columns. Each table is optimized for exactly what it does.
The result is that every operation in the system has a clean, fast path.
The one rule that cannot be broken
Every write operation must update both tables inside a single database transaction. No exceptions.
If an aggregation event inserts into the closure table but fails before updating the top parent record, your structural and operational data are now inconsistent. In a regulated environment, that inconsistency will surface during an audit at the worst possible moment.
The transaction boundary is not an implementation detail. It is the architectural contract that holds the entire model together.
When this matters most
At low volumes, any hierarchy model works. The difference becomes visible at scale: millions of serialized units, hundreds of concurrent packaging lines, multi-market deployments where verification and reporting queries run continuously.
At that scale, the design decisions made on day one either protect you or punish you.
Prepared by:
Ahmed Gamal
Engineering Director & Solutions Architect
Origin Technologغ