Why is Model.collection.find({}).to_a more than 10x faster than Model.all in MongoMapper?
What is the recommended way to manipulate large datasets in MongoMapper? In this example Model
has roughly 10,000 records.
ruby-1.8.7-p302 >开发者_开发技巧; 3.times { puts Benchmark.measure { Model.all } }
13.560000 0.040000 13.600000 ( 13.670868)
13.480000 0.040000 13.520000 ( 13.562469)
13.500000 0.030000 13.530000 ( 13.576461)
=> 3
ruby-1.8.7-p302 > 3.times { puts Benchmark.measure { Model.collection.find({}).to_a } }
1.580000 0.010000 1.590000 ( 1.603868)
1.240000 0.030000 1.270000 ( 1.268826)
1.060000 0.010000 1.070000 ( 1.072450)
=> 3
Because Model.all in mongo mapper is loading all the records into memory first and then building up objects for each of those records it is quite slow.
You can probably mitigate the latency a little bit by using MyModel.find_each to iterate records using a cursor, instead of loading them all at once, or using the :fields modifier in your query to limit the data returned, but operating on large numbers of documents using MongoMapper can be pretty painful.
MyModel.find_each(:fields => [:include_this, :include_that]) do |mydoc|
puts mydoc.include_this
end
If you're running some kind of batch operation, I'd try to get away with using the driver directly if you can.
精彩评论