Friday, July 20, 2012

Speed up updates of large datasets/records in Rails

Suppose we have 10,0000 users. We can do the following

User.find_each |user|
  user.update_attributes(status: 'active')
end
But that takes a long to complete. Executing raw sql over ActiveRecord speeds things up tremendously.

User.connection.execute("update users set status = 'active'")

No comments: