Repository navigation
perf: fix O(N²) lifecycle reinforce-promote query - #286
Merged
Merged
Conversation
The reinforce-promote policy used a correlated subquery that scanned the entire facts table for every row — O(N²) at 11K+ facts. With our production DB (11,886 facts), this caused cortex lifecycle run to hang indefinitely. Fix: replace the correlated subquery with a CTE that pre-aggregates source_count via GROUP BY, then JOIN. Same semantics, O(N) instead of O(N²). Before: infinite hang (>30s timeout, killed) After: 3.4 seconds for 25,798 fact scans All tests pass.
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
cortex lifecycle runhangs indefinitely on production databases. Root cause: the reinforce-promote policy has a correlated subquery that does a full table scan offactsfor every row — O(N²) complexity.With 11,886 facts:
11,886 × 11,886 = ~141 millionrow comparisons.Fix
Replace the correlated subquery with a CTE that pre-aggregates
source_countviaGROUP BY, thenLEFT JOIN. Same semantics, O(N) instead of O(N²).Results
Testing
All 890+ tests pass across 17 packages.