-
-
Notifications
You must be signed in to change notification settings - Fork 729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone all ExchangeVariant objects in bulk #10832
Conversation
An ExchangeVariant is a simple representation of a join table between Exchange and Variant. Previously this code was triggering an additional INSERT query for every variant added to the newly cloned exchange. Some exchanges have ~3000 variants! The code now creates them in bulk in a single INSERT statement. When cloning large order cycles this can improve performance by ~1000% or so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent! So much better than increasing timeouts. We may still need to do that short-term.
This should be reset after the cloning has been made more efficient: openfoodfoundation/openfoodnetwork#10832
The build fail seems to be in master. |
Hi @Matt-Yorkley, I worked with two order cycles:
Also I have verified that
I think this one is ready for merging. I will do that! |
What? Why?
Fixes server-melting performance issues when cloning large order cycles 👉 openfoodfoundation/ofn-install#873 (comment)
Review Notes
An
ExchangeVariant
is a simple representation of a join table betweenExchange
andVariant
. Previously this code was triggering an additionalINSERT
query for every variant associated to the newly cloned exchange. Some exchanges have ~3000 variants! The code now associates them in bulk in a singleINSERT
statement. When cloning large order cycles this can radically improve the performance.Hot tip: using the
ActiveRecord
#insert_all
method is not advised on any objects that have callbacks, as it skips them.ExchangeVariant
has no callbacks 👍Before, production data and cloning an order cycle with ~180 variants and multiple exchanges:
2.8 seconds, 1176 queries, ~2 billion memory allocations!
After, cloning the same order cycle with the same data:
0.2 seconds, 56 queries, ~127 thousand memory allocations.
What should we test?
Cloning order cycles. Make sure all the variants are there in the exchanges for the newly cloned OC as expected.
Release notes
Changelog Category: Technical changes
The title of the pull request will be included in the release notes.