Fix N+1 ES queries causing timeout in LinksController#index - #4304
Closed
gumclaw wants to merge 2 commits into
Closed
Fix N+1 ES queries causing timeout in LinksController#index#4304gumclaw wants to merge 2 commits into
gumclaw wants to merge 2 commits into
Conversation
The index action was calling successful_sales_count and total_usd_cents individually on each product when scopes include view_sales/account, resulting in 2N Elasticsearch queries. For users with many products, this caused Rack::Timeout (120s) exceptions. Add batch_successful_sales_counts and batch_total_usd_cents class methods to Product::Stats that use ES terms aggregation on product_id to fetch per-product values in just 2 queries total. The controller preloads these and passes them through as_json_options, with fallback to individual queries for other callers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
Closing as duplicate: shares identical changes to product/stats.rb and product/as_json.rb with #4305. |
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.
What
Batch Elasticsearch queries for
sales_countandsales_usd_centsinApi::V2::LinksController#indexto eliminate N+1 query pattern.batch_successful_sales_countsandbatch_total_usd_centsclass methods toProduct::Statsusing EStermsaggregation onproduct_idLinksController#indexwhen scopes includeview_salesoraccountas_json_for_apiwith fallback to individual queries for backward compatibilityWhy
The
indexaction was callingsuccessful_sales_countandtotal_usd_centsindividually on each product (2 ES queries per product). For users with many products, this causedRack::Timeout::RequestTimeoutExceptionafter 120s. This change reduces 2N ES queries to just 2 total queries using per-producttermsaggregation.Test Results
Added tests for:
batch_successful_sales_countsreturns correct per-product countsbatch_total_usd_centsreturns correct per-product net revenueas_json_for_apiuses preloaded data when providedLinksController#indexcalls batch methods instead of per-product queriesAI disclosure: Built with Claude Opus 4.6. Prompted to fix the N+1 ES query pattern in
Api::V2::LinksController#indexthat was causingRack::Timeout::RequestTimeoutException.