37M 行のテーブルに新しい列を追加しようとしています。列には関連付け ID が保持されます。
単純なモデル:
class SeenEpisode < ActiveRecord::Base
#show_id is the new column
attr_accessible :user_id, :season_id, :episode_id, :show_id
belongs_to :episode
belongs_to :season
end
これは私が思いつく最速の方法です:
seen_episodes = SeenEpisode.where("show_id IS NULL")
seen_episodes.find_in_batches do |batch| #batch size is 1000
batch.group_by(&:season_id).each do |season_id, seen_episodes|
#all seen_episodes with the same season_id, ensures the same show_id
show_id = seen_episodes.first.episode.show_id
seen_episodes.each do |seen_episode|
seen_episode.update_column(:show_id, show_id) #skip validations and callbacks
end
end
end
開発に関する現在のテストでは、10.000 レコードの入力に約 2 分かかることが示されています。
ハードウェアと mysql の構成が改善されているため、本番環境で 1 分かかるとしましょう。それでも、100 万レコードあたり 100 分かかります。なんと60時間。
これについてもっと速い方法がある可能性はありますか?