Thursday, January 19, 2012

Mongo Map Reduce Vs. Query

Just had something come up today where I had to take a bunch of users from an existing collection and put some of their data in a new collection with new IDs. Here are two approaches that do the same thing, with wildly different speeds.


Map reduce is way faster than looping over a really fast query for this kind of work!

// map reduce way is slow for querying a count, but fast for creating a new table (~20s)
map = function() {
if (this.alerts && this.alerts.length > 0 && this.apns) {
emit(new ObjectId(), {alerts: this.alerts, uuid: this.uuid, hiddenChannels: this.hiddenChannels, provider: this.provider})
}
}
reduce = function(){}
options = {out:{replace:"alertingUsers"}}
db.users.mapReduce(map, reduce, options)
db.alertingUsers.count() // 34554
// really fast queries / counts (~1s)
db.users.count({alerts:{$exists:true, $not: { $size: 0 }}, apns:{$exists:true}}) // 34554
// but makes for really slow inserts (~5 minutes)
db.users.find({alerts:{$exists:true, $not: { $size: 0 }}, apns:{$exists:true}}, {hiddenChannels: 1, apns: 1, _id: 0, alerts: 1, uuid: 1, provider: 1}).forEach(function(user) {
db.alertingUsersSlow.insert(user)
})
db.alertingUsersSlow.count() // 34554

No comments:

Post a Comment