Bulk update in rails with mongodb -


i have scenario need update 7k records in mongodb. query

document.all.each |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 

it looks query take more 2 hrs!! 1 update takes around 1.6 sec. way optimize this?

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}}) 

you can do

document.all.each |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 

this speed little bit because no callbacks or validations run.

if 1 time activity, consider doing on mongodb without using mongoid


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -