Skip to content

Instantly share code, notes, and snippets.

@oojikoo-gist
Created March 31, 2015 14:47
Show Gist options
  • Save oojikoo-gist/0e368f96476f9a51b692 to your computer and use it in GitHub Desktop.
Save oojikoo-gist/0e368f96476f9a51b692 to your computer and use it in GitHub Desktop.
rails: Giant queries

Giant queries loading everything into memory

You need to fix some data so you’ll just iterate through it all and fix it, right?

User.has_purchased(true).each do |customer|
  customer.grant_role(:customer)
end

You have an ecommerce site with a million customers. Let’s say each User object takes 500 bytes. This code will take 500MB of memory at runtime! Better:

User.has_purchased(true).find_each do |customer|
  customer.grant_role(:customer)
end

find_each uses find_in_batches to pull in 1000 records at a time, dramatically lowering the runtime memory requirements.

Bonus points: use update_all or raw SQL to perform the mass update. SQL takes time to learn well but the benefits are even more tremendous: you’ll see a 100x improvement in the performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment