Skip to content

Instantly share code, notes, and snippets.

@clofresh
Created May 27, 2010 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clofresh/415884 to your computer and use it in GitHub Desktop.
Save clofresh/415884 to your computer and use it in GitHub Desktop.
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