Permalink
Browse files

Simplify slow queries

  • Loading branch information...
1 parent 72b8015 commit ea6d6e0d44741eabb6e623c51d98d98580807538 @vrana committed Aug 20, 2012
Showing with 19 additions and 38 deletions.
  1. +5 −9 adminer/drivers/mysql.inc.php
  2. +12 −21 adminer/include/functions.inc.php
  3. +1 −5 adminer/script.inc.php
  4. +1 −3 adminer/select.inc.php
@@ -270,14 +270,11 @@ function get_databases($flush) {
// 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 @@ function show_variables() {
}
/** 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
@@ -899,43 +899,34 @@ 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 () {
}, 'token=<?php echo $token; ?>&kill=<?php echo $kill; ?>');
}, <?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
@@ -33,11 +33,7 @@
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) {
@@ -405,9 +405,7 @@
$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;
}

0 comments on commit ea6d6e0

Please sign in to comment.