From ea6d6e0d44741eabb6e623c51d98d98580807538 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Sun, 19 Aug 2012 21:55:00 -0700 Subject: [PATCH] Simplify slow queries --- adminer/drivers/mysql.inc.php | 14 +++++-------- adminer/include/functions.inc.php | 33 +++++++++++-------------------- adminer/script.inc.php | 6 +----- adminer/select.inc.php | 4 +--- 4 files changed, 19 insertions(+), 38 deletions(-) diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index e02f8189..425268ee 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -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 diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 90a27037..aa5b36a9 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -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 + ?> clearTimeout(timeout);\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 diff --git a/adminer/script.inc.php b/adminer/script.inc.php index ed9c09d7..c5ee1fbd 100644 --- a/adminer/script.inc.php +++ b/adminer/script.inc.php @@ -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) { diff --git a/adminer/select.inc.php b/adminer/select.inc.php index c08ab143..efc2c054 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -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; }