Kill Long Running Queries

Share us

To Kill Long Running Queries:-

Many people asked me how can we kill long running queries in one shot, as they have to find the long running queries one by one and then they kill, which is time taking.

I’ve written this Java Script, it actually find the slow queries and kill those slow queries. Make sure you have admin access in MongoDB. Log-in with admin access user and execute the below java script on Mongo Shell.

It will kill all queries which took >= 4 secs to execute.

//To Kill Long running Queries
//Seconds to consider a query a long running
var secondsLong=4;
//Operations type to kill(query, command, none)
var optype='query';
db.currentOp().inprog.forEach(
  function(long_query) { 
    if(long_query.secs_running > secondsLong && long_query.op == optype ) {
      print(['Killing Query: ', long_query.op_id, 'which was running since: ', long_query.secs_running, 'seconds'].join(''));
      db.killOp(long_query.op_id);
     }
  }  
)

Kindly share your valuable feedback & suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.