Simplify slow queries

This commit is contained in:
Jakub Vrana 2012-08-19 21:55:00 -07:00
parent 72b801513b
commit ea6d6e0d44
4 changed files with 19 additions and 38 deletions

View file

@ -270,14 +270,11 @@ if (!defined("DRIVER")) {
// SHOW DATABASES can take a very long time so it is cached // SHOW DATABASES can take a very long time so it is cached
$return = get_session("dbs"); $return = get_session("dbs");
if ($return === null) { if ($return === null) {
$kill = ($flush ? kill_timeout() : 0); $query = ($connection->server_info >= 5
$return = @get_vals("/* Adminer $kill */ " . ($connection->server_info >= 5 // @ - may be killed
? "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA" ? "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA"
: "SHOW DATABASES" : "SHOW DATABASES"
)); // SHOW DATABASES can be disabled by skip_show_database ); // SHOW DATABASES can be disabled by skip_show_database
if ($flush) { $return = ($flush ? slow_query($query) : get_vals($query));
cancel_kill_timeout();
}
restart_session(); restart_session();
set_session("dbs", $return); set_session("dbs", $return);
stop_session(); stop_session();
@ -935,11 +932,10 @@ if (!defined("DRIVER")) {
} }
/** Get process list /** Get process list
* @param bool
* @return array ($row) * @return array ($row)
*/ */
function process_list($full = true) { function process_list() {
return get_rows("SHOW" . ($full ? " FULL" : "") . " PROCESSLIST"); return get_rows("SHOW FULL PROCESSLIST");
} }
/** Get status variables /** Get status variables

View 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 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 /** Run query which can be killed by AJAX call after timing out
* @return int kill token * @param string
* @return Min_Result
*/ */
function kill_timeout() { function slow_query($query) {
global $adminer, $token; global $adminer, $token;
$kill = mt_rand(); if (support("kill") && is_object($connection2 = connect()) && (DB == "" || $connection2->select_db(DB))) {
if (support("kill")) { $kill = $connection2->result("SELECT CONNECTION_ID()"); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL
?> ?>
<script type="text/javascript"> <script type="text/javascript">
var timeout = setTimeout(function () { var timeout = setTimeout(function () {
ajax('<?php echo js_escape(ME); ?>script=kill', function () { ajax('<?php echo js_escape(ME); ?>script=kill', function () {
@ -914,28 +915,18 @@ var timeout = setTimeout(function () {
}, <?php echo 1000 * $adminer->queryTimeout(); ?>); }, <?php echo 1000 * $adminer->queryTimeout(); ?>);
</script> </script>
<?php <?php
} else {
$connection2 = null;
} }
ob_flush(); ob_flush();
flush(); flush();
return $kill; $return = @get_key_vals($query, $connection2); // @ - may be killed
} if ($connection2) {
/** Cancel kill query timeout
* @return null
*/
function cancel_kill_timeout() {
global $connection;
if (support("kill")) {
echo "<script type='text/javascript'>clearTimeout(timeout);</script>\n"; echo "<script type='text/javascript'>clearTimeout(timeout);</script>\n";
ob_flush(); ob_flush();
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 /** Callback registered to erase output buffer in AJAX calls

View file

@ -33,11 +33,7 @@ if ($_GET["script"] == "db") {
json_row(""); json_row("");
} elseif ($_GET["script"] == "kill") { } elseif ($_GET["script"] == "kill") {
foreach (process_list(false) as $process) { $connection->query("KILL " . (+$_POST["kill"]));
if (ereg('^/\* Adminer ' . (+$_POST["kill"]) . ' \*/', $process["Info"])) {
$connection->query("KILL $process[Id]");
}
}
} else { // connect } else { // connect
foreach (count_tables($adminer->databases()) as $db => $val) { foreach (count_tables($adminer->databases()) as $db => $val) {

View file

@ -405,9 +405,7 @@ if (!$columns) {
$found_rows = found_rows($table_status, $where); $found_rows = found_rows($table_status, $where);
if ($found_rows < max(1e4, 2 * ($page + 1) * $limit)) { if ($found_rows < max(1e4, 2 * ($page + 1) * $limit)) {
// slow with big tables // slow with big tables
$kill = kill_timeout(); $found_rows = reset(slow_query("SELECT COUNT(*) FROM " . table($TABLE) . ($where ? " WHERE " . implode(" AND ", $where) : "")));
$found_rows = @$connection->result("/* Adminer $kill */ SELECT COUNT(*) FROM " . table($TABLE) . ($where ? " WHERE " . implode(" AND ", $where) : "")); // @ - may be kill
cancel_kill_timeout();
} else { } else {
$exact_count = false; $exact_count = false;
} }