我有一个 rake 任务,它将几个表中的所有信息汇总到一个表中,每个表大约有 200 000 行:
task :migrate_all_info_tables_to_one => :environment do
profiles = Profile.all
profiles.each do |profile|
basic_info_items = profile.basic_info_items
basic_info_items.each do |item|
info = InfoItem.find_or_initialize_by_kind_and_value_and_link_and_description(:kind => 'info', :value => item.parameter, :link => item.link != 'none' ? item.link : nil, :description => item.value)
profile.info_items << info
end
cities = profile.cities
cities.each do |city|
city = InfoItem.find_or_initialize_by_kind_and_value_and_link_and_description(:kind => 'city', :value => city.title, :link => city.link != 'none' ? city.link : nil, :description => city.description)
profile.info_items << city
end
histories = profile.histories
histories.each do |item|
info = InfoItem.find_or_initialize_by_kind_and_value_and_link_and_description(:kind => 'history', :value => item.year, :link => item.link != 'none' ? item.link : nil, :description => item.event)
profile.info_items << info
end
contacts = profile.contacts
contacts.each do |item|
info = InfoItem.find_or_initialize_by_kind_and_value_and_link_and_description(:kind => 'contact', :value => item.parameter, :link => item.link != 'none' ? item.link : nil, :description => item.value)
profile.info_items << info
end
end
end
我在表中添加了所有必要的索引(使用 mysql 5.1.6),形成 mysql 每个单独的查询需要 0.00 秒,也来自 ruby 控制台。但是当我运行这个 rake 任务时,它每分钟只处理 100 个配置文件记录,为什么或者我做错了什么?
PS:我这样做是因为我在我的项目数据库架构中犯了一个错误。