Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic kill/timeout slow queries in MySQL

Is there some configuration that can be done at MySQL side to automatically kill or timeout queries that are extremely slow, say 100 seconds.

like image 524
Stewie Avatar asked Nov 15 '25 16:11

Stewie


1 Answers

You can list all your MySQL queries by the following command:

$ mysqladmin processlist

so you can run some script which will parse that list and it'll kill the specific query.

In example, you can run some script in any language via cron to periodically check for the long queries, e.g.:

$result = mysql_query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($result)) {
  $process_id=$row["Id"];
  if ($row["Time"] > 200 ) {
    $sql="KILL $process_id";
    mysql_query($sql);
  }
}

Another example:

mysql> select concat('KILL ',id,';') from information_schema.processlist
where user='root' and time > 200 into outfile '/tmp/a.txt';

mysql> source /tmp/a.txt;

Related:

  • How do I kill all the processes in Mysql "show processlist"?

Read more:

  • http://www.mysqlperformanceblog.com/
like image 90
kenorb Avatar answered Nov 18 '25 10:11

kenorb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!