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

Replace each with find_each for ActiveRecord queries #1557

Merged
merged 1 commit into from Jan 15, 2019
Merged

Replace each with find_each for ActiveRecord queries #1557

merged 1 commit into from Jan 15, 2019

Conversation

rhymes
Copy link
Contributor

@rhymes rhymes commented Jan 15, 2019

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Documentation Update

Description

Using .each on an ActiveRecord relation object means that AR is going to load all objects in memory, which is almost always not the intended behavior. It can also be problematic if such queries load a lot of objects in memory when executed.

I've replaced it with .find_each which by default loads only 1000 rows at a time, and then moves on to the next 1000.

This scales well when there are just too many rows to process all at once.

Related Tickets & Documents

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

Added to documentation?

  • docs.dev.to
  • readme
  • no documentation needed

@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Jan 15, 2019
Copy link
Contributor

@benhalpern benhalpern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. This is mostly stuff where the total number of returned instances is low enough that each is sort of fine, but find_each definitely makes sense here, especially as raw numbers in the app grow.

Thinking outloud: What about further optimizing in terms of lowering the batch_size in some of these cases where large objects are loaded into memory?

@pr-triage pr-triage bot added PR: reviewed-approved bot applied label for PR's where reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Jan 15, 2019
@benhalpern benhalpern merged commit dee7429 into forem:master Jan 15, 2019
@pr-triage pr-triage bot added PR: merged bot applied label for PR's that are merged and removed PR: reviewed-approved bot applied label for PR's where reviewer approves changes labels Jan 15, 2019
@rhymes
Copy link
Contributor Author

rhymes commented Jan 15, 2019

@benhalpern it makes sense, altough you might need a profiler to know exactly how the app is behaving. Something like rack-mini-profiler.

@rhymes rhymes deleted the rhymes/replace-each-with-find-each branch January 15, 2019 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants