I'm working in Rails 2.3.11 environment.
I want to seed a social_activities table like so:
votes = Vote.find(:all, :limit => 10000, :order => 'created_at DESC')
for vote in votes do
act = vote.activity_for!
act.created_at = vote.created_at
act.save!
end
comments = Comment.find(:all, :limit => 10000, :order => 'created_at DESC')
for comment in comments do
act = comment.activity_for!
act.created_at = comment.created_at
act.save!
end
...and...so on...
As you can 开发者_开发技巧see, I'm processing a lot of records. How can I do so in the most memory- and performance-efficient way?
Instead of fetching 10000 records at a time, you can reduce the number of objects in memory by making this number smaller (say 100), and using find_each
to work your way through all the records.
Vote.find_each(:order => 'created_at DESC', :batch_size => 100) do |vote|
act = vote.activity_for!
act.created_at = vote.created_at
act.save!
end
Comment.find_each(:order => 'created_at DESC', :batch_size => 100) do |comment|
act = comment.activity_for!
act.created_at = comment.created_at
act.save!
end
Now records will only be fetched 100 at a time, reducing the memory footprint.
Active Record isn't great for importing or moving large datasets around. This looks like something you should do with an SQL statement directly.
In SQL I think you would do this with an update across an inner join or something similiar. Either way the SQL server would run your query directly, and this would be dramatically faster than Active Record.
精彩评论