开发者

Memory- and performance-efficient way to find records and seed into a table

开发者 https://www.devze.com 2023-03-06 14:47 出处:网络
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\')

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消