Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Created January 19, 2012 23:48
Show Gist options
  • Select an option

  • Save xjamundx/1643807 to your computer and use it in GitHub Desktop.

Select an option

Save xjamundx/1643807 to your computer and use it in GitHub Desktop.
mongo map reduce vs query speed test
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment