Map reduce is way faster than looping over a really fast query for this kind of work!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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