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

Potential peformance improvements with Field::BelongsTo #2600

Open
paayaw0 opened this issue May 31, 2024 · 0 comments
Open

Potential peformance improvements with Field::BelongsTo #2600

paayaw0 opened this issue May 31, 2024 · 0 comments

Comments

@paayaw0
Copy link

paayaw0 commented May 31, 2024

This issue maybe have some overlaps with this and this

I was working on a project that uses administrate and noticed a performance issue with the Field::BelongsTo, where the associated record took too long to load up in the edit form. For more context the association in question is as follows:

class Employee
  belongs_to :company
end

class Company
  has_many :employees
end

So in the Employee edit form, I need to select a company that a particular employee belongs to. As the number of companies created increased I noticed a considerable delay in the edit form to the point where the page was unusable for a minute or so. Tracking down the issue led me to this line of code here

In a situation where thousands of associated objects need to be iterated upon one could imagine how this will overtime cause a significant lag in performance.

My current resolution was to create a custom field as explained in your documentation, implementing the .find_each query method and immediately noticed a significant improvement.

What I propose is the use of the active record method.find_each as defined here. This is a memory-friendly approach to iterating over large number of records.

Hopefully during the weekend I'll have the time to submit my suggestion upstream as PR with screenshots of benchmarked improvements but given that my solution is pretty simple I believe anyone with the time on their hands can easily implement this upstream.

@paayaw0 paayaw0 changed the title Potential peformance improvements with Field:: Potential peformance improvements with Field::BelongsTo May 31, 2024
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

No branches or pull requests

1 participant