Simplify running slow queries

This commit is contained in:
Jakub Vrana 2018-03-09 18:06:19 +01:00
parent 67c2a91c67
commit 665fafb297
6 changed files with 35 additions and 7 deletions

View file

@ -298,6 +298,16 @@ if (!defined("DRIVER")) {
return queries($prefix . implode(",\n", $values) . $suffix); return queries($prefix . implode(",\n", $values) . $suffix);
} }
function slowQuery($query, $timeout) {
if (min_version('5.7.8', '10.1.2')) {
if (preg_match('~MariaDB~', $this->_conn->server_info)) {
return "SET STATEMENT max_statement_time=$timeout FOR $query";
} elseif (preg_match('~^(SELECT\b)(.+)~is', $query, $match)) {
return "$match[1] /*+ MAX_EXECUTION_TIME(" . ($timeout * 1000) . ") */ $match[2]";
}
}
}
function convertSearch($idf, $val, $field) { function convertSearch($idf, $val, $field) {
return (preg_match('~char|text|enum|set~', $field["type"]) && !preg_match("~^utf8~", $field["collation"]) && preg_match('~[\x80-\xFF]~', $val['val']) return (preg_match('~char|text|enum|set~', $field["type"]) && !preg_match("~^utf8~", $field["collation"]) && preg_match('~[\x80-\xFF]~', $val['val'])
? "CONVERT($idf USING " . charset($this->_conn) . ")" ? "CONVERT($idf USING " . charset($this->_conn) . ")"

View file

@ -193,6 +193,11 @@ if (isset($_GET["pgsql"])) {
return true; return true;
} }
function slowQuery($query, $timeout) {
// BEGIN, COMMIT - automatically wrapped into a transaction by pg_query but not by PDO
return "BEGIN; SET LOCAL statement_timeout = " . (1000 * $timeout) . "; $query; COMMIT";
}
function convertSearch($idf, $val, $field) { function convertSearch($idf, $val, $field) {
return (preg_match('~char|text' return (preg_match('~char|text'
. (!preg_match('~LIKE~', $val["op"]) ? '|date|time(stamp)?' . (is_numeric($val["val"]) ? '|' . number_type() : '') : '') . (!preg_match('~LIKE~', $val["op"]) ? '|date|time(stamp)?' . (is_numeric($val["val"]) ? '|' . number_type() : '') : '')

View file

@ -19,6 +19,7 @@ if (isset($_GET["simpledb"])) {
$params['NextToken'] = $this->next; $params['NextToken'] = $this->next;
} }
$result = sdb_request_all('Select', 'Item', $params, $this->timeout); //! respect $unbuffered $result = sdb_request_all('Select', 'Item', $params, $this->timeout); //! respect $unbuffered
$this->timeout = 0;
if ($result === false) { if ($result === false) {
return $result; return $result;
} }
@ -236,6 +237,11 @@ if (isset($_GET["simpledb"])) {
function rollback() { function rollback() {
return false; return false;
} }
function slowQuery($query, $timeout) {
$this->_conn->timeout = $timeout;
return $query;
}
} }

View file

@ -113,6 +113,14 @@
return queries("ROLLBACK"); return queries("ROLLBACK");
} }
/** Return query with a timeout
* @param string
* @param int seconds
* @return string or null if the driver doesn't support query timeouts
*/
function slowQuery($query, $timeout) {
}
/** Convert column to be searchable /** Convert column to be searchable
* @param string escaped column name * @param string escaped column name
* @param array array("op" => , "val" => ) * @param array array("op" => , "val" => )

View file

@ -393,19 +393,16 @@ function get_vals($query, $column = 0) {
/** Get keys from first column and values from second /** Get keys from first column and values from second
* @param string * @param string
* @param Min_DB * @param Min_DB
* @param float
* @param bool * @param bool
* @return array * @return array
*/ */
function get_key_vals($query, $connection2 = null, $timeout = 0, $set_keys = true) { function get_key_vals($query, $connection2 = null, $set_keys = true) {
global $connection; global $connection;
if (!is_object($connection2)) { if (!is_object($connection2)) {
$connection2 = $connection; $connection2 = $connection;
} }
$return = array(); $return = array();
$connection2->timeout = $timeout;
$result = $connection2->query($query); $result = $connection2->query($query);
$connection2->timeout = 0;
if (is_object($result)) { if (is_object($result)) {
while ($row = $result->fetch_row()) { while ($row = $result->fetch_row()) {
if ($set_keys) { if ($set_keys) {
@ -1306,10 +1303,11 @@ function count_rows($table, $where, $is_group, $group) {
* @return array of strings * @return array of strings
*/ */
function slow_query($query) { function slow_query($query) {
global $adminer, $token; global $adminer, $token, $driver;
$db = $adminer->database(); $db = $adminer->database();
$timeout = $adminer->queryTimeout(); $timeout = $adminer->queryTimeout();
if (support("kill") && is_object($connection2 = connect()) && ($db == "" || $connection2->select_db($db))) { $slow_query = $driver->slowQuery($query, $timeout);
if (!$slow_query && support("kill") && is_object($connection2 = connect()) && ($db == "" || $connection2->select_db($db))) {
$kill = $connection2->result(connection_id()); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL $kill = $connection2->result(connection_id()); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL
?> ?>
<script<?php echo nonce(); ?>> <script<?php echo nonce(); ?>>
@ -1324,7 +1322,7 @@ var timeout = setTimeout(function () {
} }
ob_flush(); ob_flush();
flush(); flush();
$return = @get_key_vals($query, $connection2, $timeout, false); // @ - may be killed $return = @get_key_vals(($slow_query ? $slow_query : $query), $connection2, false); // @ - may be killed
if ($connection2) { if ($connection2) {
echo script("clearTimeout(timeout);"); echo script("clearTimeout(timeout);");
ob_flush(); ob_flush();

View file

@ -1,5 +1,6 @@
Adminer 4.6.3-dev: Adminer 4.6.3-dev:
Stop session before connecting Stop session before connecting
Simplify running slow queries
Fix displaying info about non-alphabetical objects (bug #599) Fix displaying info about non-alphabetical objects (bug #599)
PDO: Support binary fields download PDO: Support binary fields download
MySQL: Use CONVERT() only when searching for non-ASCII (bug #603) MySQL: Use CONVERT() only when searching for non-ASCII (bug #603)