]> git.joonet.de Git - adminer.git/commitdiff
Simplify slow queries
authorJakub Vrana <jakub@vrana.cz>
Mon, 20 Aug 2012 04:55:00 +0000 (21:55 -0700)
committerJakub Vrana <jakub@vrana.cz>
Mon, 20 Aug 2012 04:55:00 +0000 (21:55 -0700)
adminer/drivers/mysql.inc.php
adminer/include/functions.inc.php
adminer/script.inc.php
adminer/select.inc.php

index e02f818991aa5da63d8e6865e9fe8a22604df42c..425268eeb9bfb08619c88bd85a719ae2c672524d 100644 (file)
@@ -270,14 +270,11 @@ if (!defined("DRIVER")) {
                // SHOW DATABASES can take a very long time so it is cached
                $return = get_session("dbs");
                if ($return === null) {
-                       $kill = ($flush ? kill_timeout() : 0);
-                       $return = @get_vals("/* Adminer $kill */ " . ($connection->server_info >= 5 // @ - may be killed
+                       $query = ($connection->server_info >= 5
                                ? "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA"
                                : "SHOW DATABASES"
-                       )); // SHOW DATABASES can be disabled by skip_show_database
-                       if ($flush) {
-                               cancel_kill_timeout();
-                       }
+                       ); // SHOW DATABASES can be disabled by skip_show_database
+                       $return = ($flush ? slow_query($query) : get_vals($query));
                        restart_session();
                        set_session("dbs", $return);
                        stop_session();
@@ -935,11 +932,10 @@ if (!defined("DRIVER")) {
        }
        
        /** Get process list
-       * @param bool
        * @return array ($row)
        */
-       function process_list($full = true) {
-               return get_rows("SHOW" . ($full ? " FULL" : "") . " PROCESSLIST");
+       function process_list() {
+               return get_rows("SHOW FULL PROCESSLIST");
        }
        
        /** Get status variables
index 90a270379943ece4a98ca910de7836ba56188688..aa5b36a970f07551af9317d6f9033075b3b7e739 100644 (file)
@@ -899,14 +899,15 @@ function is_url($string) {
        return (preg_match("~^(https?)://($domain?\\.)+$domain(:\\d+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string, $match) ? strtolower($match[1]) : ""); //! restrict path, query and fragment characters
 }
 
-/** Launch timeout after which the query will be killed
-* @return int kill token
+/** Run query which can be killed by AJAX call after timing out
+* @param string
+* @return Min_Result
 */
-function kill_timeout() {
+function slow_query($query) {
        global $adminer, $token;
-       $kill = mt_rand();
-       if (support("kill")) {
-       ?>
+       if (support("kill") && is_object($connection2 = connect()) && (DB == "" || $connection2->select_db(DB))) {
+               $kill = $connection2->result("SELECT CONNECTION_ID()"); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL
+               ?>
 <script type="text/javascript">
 var timeout = setTimeout(function () {
        ajax('<?php echo js_escape(ME); ?>script=kill', function () {
@@ -914,28 +915,18 @@ var timeout = setTimeout(function () {
 }, <?php echo 1000 * $adminer->queryTimeout(); ?>);
 </script>
 <?php
+       } else {
+               $connection2 = null;
        }
        ob_flush();
        flush();
-       return $kill;
-}
-
-/** Cancel kill query timeout
-* @return null
-*/
-function cancel_kill_timeout() {
-       global $connection;
-       if (support("kill")) {
+       $return = @get_key_vals($query, $connection2); // @ - may be killed
+       if ($connection2) {
                echo "<script type='text/javascript'>clearTimeout(timeout);</script>\n";
                ob_flush();
                flush();
-               if ($connection->errno == 2006) { // 2006 - CR_SERVER_GONE_ERROR
-                       $connection2 = connect();
-                       if (is_object($connection2)) {
-                               $connection = $connection2;
-                       }
-               }
        }
+       return array_keys($return);
 }
 
 /** Callback registered to erase output buffer in AJAX calls
index ed9c09d7e5cea245813776400996f75ad3ab7c1c..c5ee1fbd1ca6bcef7270d0dea95d221bf29113a3 100644 (file)
@@ -33,11 +33,7 @@ if ($_GET["script"] == "db") {
        json_row("");
 
 } elseif ($_GET["script"] == "kill") {
-       foreach (process_list(false) as $process) {
-               if (ereg('^/\* Adminer ' . (+$_POST["kill"]) . ' \*/', $process["Info"])) {
-                       $connection->query("KILL $process[Id]");
-               }
-       }
+       $connection->query("KILL " . (+$_POST["kill"]));
 
 } else { // connect
        foreach (count_tables($adminer->databases()) as $db => $val) {
index c08ab143bd7b52e5181dcf54caa3fc36a1ecfa43..efc2c0549dcb15cf52d1e611bd12848d1a4f8737 100644 (file)
@@ -405,9 +405,7 @@ if (!$columns) {
                                $found_rows = found_rows($table_status, $where);
                                if ($found_rows < max(1e4, 2 * ($page + 1) * $limit)) {
                                        // slow with big tables
-                                       $kill = kill_timeout();
-                                       $found_rows = @$connection->result("/* Adminer $kill */ SELECT COUNT(*) FROM " . table($TABLE) . ($where ? " WHERE " . implode(" AND ", $where) : "")); // @ - may be kill
-                                       cancel_kill_timeout();
+                                       $found_rows = reset(slow_query("SELECT COUNT(*) FROM " . table($TABLE) . ($where ? " WHERE " . implode(" AND ", $where) : "")));
                                } else {
                                        $exact_count = false;
                                }