perf(features): Introduce materialized view for Features#subscriptions_count#5889
Open
vankiru wants to merge 5 commits into
Open
perf(features): Introduce materialized view for Features#subscriptions_count#5889vankiru wants to merge 5 commits into
vankiru wants to merge 5 commits into
Conversation
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.
Context
The subscription count on the Features page is currently incorrect. Right now, it only counts subscriptions that get a feature through their plan. It doesn't include subscriptions that have the feature assigned directly on their Entitlements page, and it doesn't account for subscriptions where the feature has been removed.
Description
This PR adds a materialized view that precomputes and stores the subscription count for every non-deleted feature that has at least one associated subscription.
The count is calculated in four steps for each feature:
Count subscriptions that get the feature through their plan. If a subscription has an overridden plan, it's counted towards the parent plan's features, since plan overrides can't have their own entitlements.
Count subscriptions that have the feature assigned directly on their Entitlements page. This step also removes duplicates when a subscription gets the same feature both through its plan and via a direct assignment.
Count subscriptions where the feature has been manually removed.
Calculate the final count as:
plan subscriptions + direct subscriptions - feature removalsRefreshing the Materialized View
On a database with around 2.5M subscriptions and 4M entitlements, refreshing the view takes about 3 seconds. On databases roughly 10× larger, it may take up to 30 seconds.
To balance performance with data freshness, the view is refreshed every 10 minutes. This helps avoid refresh-time spikes while keeping the counts reasonably up to date.