PHP 8.5.0 Beta 1 available for testing

Voting

: five minus two?
(Example: nine)

The Note You're Voting On

ekalashnikov at gmail dot com
7 years ago
This is how you can test and terminate slow query with polling.

<?php
// Slow SQL Query
$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 so we can kill the query
ignore_user_abort(true);
$MaxTime = 5; // seconds
$Overtime = false;
$StartTime = time();
do
{
// Poll MySQL
$links = $errors = $reject = array($link);
$poll = mysqli_poll($links, $errors, $reject, 0, 500000);
// Check if the connection is aborted and the query was killed
if (connection_aborted() && mysqli_kill($link, $thread_id)) {
die();
}
$EndTime = time();
// Check overtime, kill if detected overtime
if ($EndTime - $StartTime > $MaxTime)
{
mysqli_kill($link, $thread_id);
$Overtime = true;
echo
'Error: Query took over '.$Overtime.'.';
}
} while (!
$poll && $Overtime == false);
?>

<< Back to user notes page

To Top