I have an extremely large query to a MySQL db that takes on the order of 20s (gathering data from millions of rows). The query is run through an AJAX call which then waits for a response from the php script gathering the data.
JS query:
$.ajax({
url: "../largeQuery.php",
type: "POST",
success: function (data) {
alert(data);
}
});
largeQuery.php:
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); //these are set elsewhere
$largeQuery = 'I am some super long query';
$response = $mysqli->query($largeQuery);
print_r($response);
However, if a user changes the content of the page and the $response data is no longer needed, the next page is locked up because MySQL is still fetching $largeQuery and can't handle another query until it finishes $largeQuery.
I understand that I am able to get the thread id from $thread_id = $mysqli->thread_id, and then kill it by $mysqli->kill($thread_id), but how can I get JS to issue an update to currently running PHP code?
session_write_closeafter you started the query to free it up., you should also cancel theXMLHttpRequestif its no longer neededsession_write_closebefore query, because after the query session will be closed anyway, and no effect will occur.