I am using Rails 4.0.0 and Ruby 2.0.0. My Post model (like on blogs) is associated with a user with a username combination of user_name, first_name, last_name. I would like to migrate the data so that the messages are associated with users with a foreign key, which is the user ID.
I have about 11 million posts in the posts table.
I am running the code below to transfer data using the rake task on a Linux server. However, my task continues to βkillβ the wound, presumably because of the rake task, in particular, below the code, consuming too much memory.
I found that decreasing batch_size to 20 and increasing sleep(10) to sleep(60) allows the task to work longer, updating more records in general, without being killed, but taking significantly longer.
How can I optimize this code for speed and memory usage?
Post.where(user_id: nil).find_in_batches(batch_size: 1000) do |posts| puts "*** Updating batch beginning with post #{posts.first.id}..." sleep(10)
source share