Skip to content
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

Merged
merged 2 commits into from
May 18, 2023

Conversation

Matt-Yorkley
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley commented May 11, 2023

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 between Exchange and Variant. Previously this code was triggering an additional INSERT query for every variant associated to the newly cloned exchange. Some exchanges have ~3000 variants! The code now associates them in bulk in a single INSERT 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:

Screenshot from 2023-05-11 20-23-46

2.8 seconds, 1176 queries, ~2 billion memory allocations!

After, cloning the same order cycle with the same data:

Screenshot from 2023-05-11 20-22-30

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.

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.
@Matt-Yorkley Matt-Yorkley self-assigned this May 11, 2023
Copy link
Member

@mkllnk mkllnk left a 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.

mkllnk added a commit to mkllnk/ofn-install that referenced this pull request May 12, 2023
This should be reset after the cloning has been made more efficient:
openfoodfoundation/openfoodnetwork#10832
@mkllnk
Copy link
Member

mkllnk commented May 12, 2023

The build fail seems to be in master.

@drummer83 drummer83 added the pr-staged-au staging.openfoodnetwork.org.au label May 18, 2023
@drummer83 drummer83 self-assigned this May 18, 2023
@drummer83 drummer83 added pr-staged-fr staging.coopcircuits.fr and removed pr-staged-fr staging.coopcircuits.fr labels May 18, 2023
@drummer83
Copy link
Contributor

Hi @Matt-Yorkley,
Wow, this one is impressive! Look at the reduced time for cloning a huge order cycle with more than 6000 exchanges!!!

I worked with two order cycles:
image

Action Before PR After PR
Cloning huge order cycle ~105 s ~ 3 s 😲 ⚡
Delete huge order cycle ~45 s ~45 s

Also I have verified that

  • Incoming products are cloned identically (respecting the checkbox to include them in the order cycle or not) ✔️
  • Outgoing products are cloned identically (respecting the checkbox to include them in the order cycle or not) ✔️
  • Checkout options are cloned identically (respecting the checkbox to include them in the order cycle or not) ✔️
  • 'Ready for' instructions are cloned identically ✔️

I think this one is ready for merging. I will do that!
Thanks again! 💪 🚀

@drummer83 drummer83 merged commit 39bb1f6 into openfoodfoundation:master May 18, 2023
@drummer83 drummer83 removed the pr-staged-au staging.openfoodnetwork.org.au label May 18, 2023
@drummer83 drummer83 linked an issue Jun 2, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Order Cycle Clone Performance
4 participants