Skip to content

Instantly share code, notes, and snippets.

@nirebu
Last active January 28, 2019 09:45
Show Gist options
  • Save nirebu/d6fda7dc8e9e34de8587cb6cd20ef156 to your computer and use it in GitHub Desktop.
Save nirebu/d6fda7dc8e9e34de8587cb6cd20ef156 to your computer and use it in GitHub Desktop.
[Mongo DB long queries aggregation] Aggregate for queries running on shards to detect if there are too many or too long queries slowing the system down
use admin
var threshold = 500;
db.aggregate([
{ $currentOp : { allUsers: true } },
{ $match: { "secs_running" : { $gt: threshold } , "ns" : { $not : /oplog/ } } },
{ $group : {
_id: '$shard',
"n_query": { $sum: 1 },
"avg_secs_running": { $avg: '$secs_running' }
,"operations" : { $push : { "opid" : '$opid' , "secs_running" : '$secs_running' } }
}
},
{ $sort : { "n_query": 1 } }
]).pretty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment