Skip to content

Instantly share code, notes, and snippets.

@sudara
Created May 31, 2010 20:00
Show Gist options
  • Select an option

  • Save sudara/420210 to your computer and use it in GitHub Desktop.

Select an option

Save sudara/420210 to your computer and use it in GitHub Desktop.
bloat_fail.rb
## FAIL
## Attempting to iterate (chunkily) over 2000 records and instantiating 1 single object per iteration
## All of the below bloat to > 200MB on rails 2.3.x
# iterates in chunks of 10
User.find_each(:batch_size => 10) do |user|
date = user.posts.find(:first).created_at
end
# iterates in chunks of 100
User.find_each(:batch_size => 10) do |user|
date = user.posts.find(:first).created_at
end
# iterates with will_paginate
User.paginated_each do |user|
date = user.posts.find(:first).created_at
end
# Not going through the relationship doesn't make a difference
User.find_each(:batch_size => 10) do |user|
date = Posts.find(:first, :conditions =>{:user => user}).created_at
end
# Nor does storing the result in a var with a larger scope
date = []
User.find_each(:batch_size => 10) do |user|
date << user.posts.find(:first).created_at
end
# Or heck, not storing the result at all
User.find_each(:batch_size => 10) do |user|
user.posts.find(:first).created_at
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment