我有一个场景,我需要更新mongodb中的一些7k记录。查询是
Document.all.each do |d|
d.pages_enabled_count = d.pages.where(:disabled => false).count
d.pages_enabled_to_query_count = d.pages.where(:disabled => false , :to_query => true).count
d.pages_enabled_to_rescan_count = d.pages.where(:disabled => false , :to_rescan => true).count
d.pages_enabled_to_retag_count = d.pages.where(:disabled => false , :to_retag => true).count
d.save
end
看起来这个查询需要2个多小时!!一次更新需要大约1.6秒。优化这个的方法是什么?
MONGODB (388.3ms) test['$cmd'].find({"count"=>"pages", "query"=>{:deleted_at=>nil,
"document_id"=>BSON::ObjectId('51cd1c435329b64899000035'), :disabled=>false}, "fields"=>nil}).limit(-1)
MONGODB (388.5ms) test['$cmd'].find({"count"=>"pages", "query"=>{:deleted_at=>nil, "document_id"=>BSON::ObjectId('51cd1c435329b64899000035'), :disabled=>false, :to_query=>true}, "fields"=>nil}).limit(-1)
MONGODB (408.0ms) test['$cmd'].find({"count"=>"pages", "query"=>{:deleted_at=>nil, "document_id"=>BSON::ObjectId('51cd1c435329b64899000035'), :disabled=>false, :to_rescan=>true}, "fields"=>nil}).limit(-1)
MONGODB (390.9ms) test['$cmd'].find({"count"=>"pages", "query"=>{:deleted_at=>nil, "document_id"=>BSON::ObjectId('51cd1c435329b64899000035'), :disabled=>false, :to_retag=>true}, "fields"=>nil}).limit(-1)
MONGODB (0.9ms) test['users'].find({"owned_document_id"=>BSON::ObjectId('51cd1c435329b64899000035')}).limit(-1).sort([[:_id, :asc]])
MONGODB (0.1ms) test['documents'].update({"_id"=>BSON::ObjectId('51cd1c435329b64899000035')}, {"$set"=>{"pages_enabled_count"=>18, "pages_enabled_to_query_count"=>0, "pages_enabled_to_rescan_count"=>0, "pages_enabled_to_retag_count"=>0, "updated_at"=>2013-08-20 04:35:47 UTC}})
答案 0 :(得分:0)
你可以做到
Document.all.each do |d|
d.update({ :pages_enabled_count => d.pages.where(:disabled => false).count,
:pages_enabled_to_query_count => d.pages.where(:disabled => false, :to_query => true).count,
:pages_enabled_to_rescan_count => d.pages.where(:disabled => false, :to_rescan => true).count,
:pages_enabled_to_retag_count => d.pages.where(:disabled => false, :to_retag => true).count
})
end
这样可以加快速度,因为不会运行callbacks
或validations
。
如果这是一次性活动,您可以考虑在Mongodb
上执行,而不使用mongoid