]> git.joonet.de Git - adminer.git/commitdiff
Simplify running slow queries
authorJakub Vrana <jakub@vrana.cz>
Fri, 9 Mar 2018 17:06:19 +0000 (18:06 +0100)
committerJakub Vrana <jakub@vrana.cz>
Fri, 9 Mar 2018 17:19:14 +0000 (18:19 +0100)
adminer/drivers/mysql.inc.php
adminer/drivers/pgsql.inc.php
adminer/drivers/simpledb.inc.php
adminer/include/driver.inc.php
adminer/include/functions.inc.php
changes.txt

index 71e1a825020b2df3ba6988b4e4309aa6bb5ccb48..f995cc52b0a09a1974efda649feafb15d41f82be 100644 (file)
@@ -298,6 +298,16 @@ if (!defined("DRIVER")) {
                        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) {
                        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) . ")"
index 52aac65f7e99604d638ca0983b44dda0a3deaa3f..39a1a6b61b1037a1a95f5cf9f3ee8831125c4943 100644 (file)
@@ -193,6 +193,11 @@ if (isset($_GET["pgsql"])) {
                        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) {
                        return (preg_match('~char|text'
                                        . (!preg_match('~LIKE~', $val["op"]) ? '|date|time(stamp)?' . (is_numeric($val["val"]) ? '|' . number_type() : '') : '')
index 63ca486971dbf88379870492ef69dbae0c91f2b1..040309c5d99e71dc487adc1620c35b8f1f97a776 100644 (file)
@@ -19,6 +19,7 @@ if (isset($_GET["simpledb"])) {
                                        $params['NextToken'] = $this->next;
                                }
                                $result = sdb_request_all('Select', 'Item', $params, $this->timeout); //! respect $unbuffered
+                               $this->timeout = 0;
                                if ($result === false) {
                                        return $result;
                                }
@@ -236,6 +237,11 @@ if (isset($_GET["simpledb"])) {
                function rollback() {
                        return false;
                }
+               
+               function slowQuery($query, $timeout) {
+                       $this->_conn->timeout = $timeout;
+                       return $query;
+               }
 
        }
 
index 5ca8f951dca0d8f7a622a699562b7fcce3c3af92..893d16677f54e2d549fb3e59730ccc833333e0f1 100644 (file)
                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
        * @param string escaped column name
        * @param array array("op" => , "val" => )
index 4742414e1391c260a612ed8e844fed56a8c1883a..4a7bd6a4a378585051b1189f6877dacf08c0df14 100644 (file)
@@ -393,19 +393,16 @@ function get_vals($query, $column = 0) {
 /** Get keys from first column and values from second
 * @param string
 * @param Min_DB
-* @param float
 * @param bool
 * @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;
        if (!is_object($connection2)) {
                $connection2 = $connection;
        }
        $return = array();
-       $connection2->timeout = $timeout;
        $result = $connection2->query($query);
-       $connection2->timeout = 0;
        if (is_object($result)) {
                while ($row = $result->fetch_row()) {
                        if ($set_keys) {
@@ -1306,10 +1303,11 @@ function count_rows($table, $where, $is_group, $group) {
 * @return array of strings
 */
 function slow_query($query) {
-       global $adminer, $token;
+       global $adminer, $token, $driver;
        $db = $adminer->database();
        $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
                ?>
 <script<?php echo nonce(); ?>>
@@ -1324,7 +1322,7 @@ var timeout = setTimeout(function () {
        }
        ob_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) {
                echo script("clearTimeout(timeout);");
                ob_flush();
index 33844fdd35ae8f31fe6142ffca633f61f3a866f4..43222c1b776dd6bba92308f79bdd3c17fd732ca2 100644 (file)
@@ -1,5 +1,6 @@
 Adminer 4.6.3-dev:
 Stop session before connecting
+Simplify running slow queries
 Fix displaying info about non-alphabetical objects (bug #599)
 PDO: Support binary fields download
 MySQL: Use CONVERT() only when searching for non-ASCII (bug #603)