home shape

Killing Long-Running Queries in ArangoDB

Suppose there is an AQL query that’s executing in the server for a long time already and you want to get rid of it. What can be done to abort that query?

If a connection to the server can still be established, the easiest is to use the ArangoShell to fetch the list of currently executing AQL queries and send a kill command to the server for the correct query.

To start, we can fetch the list of all running queries and print their ids, query strings and runtimes. This is only inspection and does not abort any query:

var queries = require("org/arangodb/aql/queries"); queries.current();

Here’s an example result for the list of running queries:

[ 
  { 
    "id" : "190", 
    "query" : "RETURN SLEEP(1000)", 
    "started" : "2016-01-26T22:41:24Z", 
    "runTime" : 218.49146389961243 
  } 
]

To now kill a query from the list, we can pass the query’s id to kill:

var queries = require("org/arangodb/aql/queries");
queries.kill("190");  /* insert actual query id here */

If a query was actually killed on the server, that call should return without an error, and the server should have logged a warning in addition.

If we wanted to abort one or many queries from the list solely by looking at query string patterns or query runtime, we could iterate over the list of current queries and kill each one that matches a predicate.

For example, the following snippet will abort all currently running queries that contain the string SLEEP anywhere inside their query string:

var queries = require("org/arangodb/aql/queries");

queries.current().filter(function(query) { 
  return query.query.match(/SLEEP/);    /* predicate based on query string */ 
}).forEach(function(query) { 
  print("killing query: ", query);      /* print what we're killing */ 
  queries.kill(query.id);               /* actually kill query */ 
});

Filtering based on current query runtime is also simple, by adjusting the predicate. To abort all queries that ran longer than 30 seconds use:

var queries = require("org/arangodb/aql/queries");

queries.current().filter(function(query) { 
  return query.runTime > 30;            /* predicate based on query runtime */ 
}).forEach(function(query) { 
  print("killing query: ", query);      /* print what we're killing */ 
  queries.kill(query.id);               /* actually kill query */ 
});

Please make sure the predicates are correct so only the actually intended queries get aborted!

To test a predicate without killing a query, use the above code without the forEach part that did the killing.

Jan Steemann

Jan Steemann

After more than 30 years of playing around with 8 bit computers, assembler and scripting languages, Jan decided to move on to work in database engineering. Jan is now a senior C/C++ developer with the ArangoDB core team, being there from version 0.1. He is mostly working on performance optimization, storage engines and the querying functionality. He also wrote most of AQL (ArangoDB’s query language).

1 Comment

  1. Avi Vainshtein on November 29, 2017 at 11:56 am

    I’m using ArangoDB 3.1.22.
    Have a long-running AQL query, but not able to kill it : neither from the GUI, nor from arangosh.
    Messages like “WARNING killing AQL query 94758” appear in the log file, but the query is still in state-executing.
    Is there any way to “kill force” a running query ? Any other solution/workaround ?
    regards
    Avi

Leave a Comment





Get the latest tutorials, blog posts and news: