Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Last active December 17, 2017 10:59
Show Gist options
  • Save StevenJL/b5335cce9895a4422e08e5a309ec11aa to your computer and use it in GitHub Desktop.
Save StevenJL/b5335cce9895a4422e08e5a309ec11aa to your computer and use it in GitHub Desktop.
# Selecting all users
User.all.each do |user|
notify(user)
end
# Generates this query:
# SELECT * FROM users;
# Selecting all users with `find_each` will query by batches of size
# determined by the :batch_size option (defaults to 1000)
User.all.find_each(batch_size: 5000) do |user|
notify(user)
end
# Generates the queries
# SELECT * FROM users ORDER BY users.id ASC LIMIT 5000;
# SELECT * FROM users WHERE users.id > 5000 ORDER BY users.id ASC LIMIT 5000;
# SELECT * FROM users WHERE users.id > 10000 ORDER BY users.id ASC LIMIT 5000;
# ....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment