使用轮询检查慢查询的改进版本,
"KILL #ID" 如果 myqsli 连接的 id 与杀死它的连接相同,则不起作用,
因此您必须从 mysqli 断开连接,并使用新的连接重新连接到
杀死 #ID。
<?php
$SelectSql = 'SELECT * FROM SLOW_QUERY';
$link = mysqli_connect('localhost','user','pass','database');
mysqli_query($SelectSql, MYSQLI_ASYNC);
$thread_id = mysqli_thread_id($link);
ignore_user_abort(true);
$MaxTime = 5; $Overtime = false;
$StartTime = time();
do
{
$links = $errors = $reject = array($link);
$poll = mysqli_poll($links, $errors, $reject, 0, 500000);
if (connection_aborted()) {
$link_new = mysqli_connect('localhost','user','pass','database');
mysqli_kill($link_new, $thread_id);
$kill = mysqli_kill($link_new, $thread_id);
if ($kill)
{
die();
}
}
$EndTime = time();
if ($EndTime - $StartTime > $MaxTime)
{
$link_new = mysqli_connect('localhost','user','pass','database');
mysqli_kill($link_new, $thread_id);
$Overtime = true;
echo 'Error: Query took over '.$Overtime.'.';
}
} while (!$poll && $Overtime == false);
?>