Created
May 27, 2010 14:48
-
-
Save clofresh/415884 to your computer and use it in GitHub Desktop.
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
result = db.log.mapReduce( | |
function() { | |
if(this.hash) { | |
emit(this.hash, { | |
'sum': this.duration, | |
'count': 1, | |
'sql': this.sql | |
}) | |
} | |
}, | |
function(key, values) { | |
var sum = 0; | |
var count = 0; | |
var sql = ''; | |
for(var i in values) { | |
sum += values[i].sum; | |
count += values[i].count; | |
} | |
return { | |
'sum': sum, | |
'count': count, | |
'sql': values[0].sql | |
}; | |
}); | |
costs = db[result.result].find().map(function(obj) { | |
return { | |
cost: obj.value.count * obj.value.sum, | |
avg: obj.value.sum / obj.value.count, | |
executions: obj.value.count, | |
total_time: obj.value.sum, | |
sql: obj.value.sql, | |
hash: obj._id | |
} | |
}).sort(function(a, b) { | |
return a.cost - b.cost | |
}); | |
print(tojson(costs)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment