From c82829942c7e92c9286f76b3020246bab676098c Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Fri, 29 Jul 2011 17:08:06 +0200 Subject: [PATCH] Fast number of rows with big tables in PostgreSQL (thanks to juzna) --- adminer/drivers/mssql.inc.php | 4 ++++ adminer/drivers/mysql.inc.php | 9 +++++++++ adminer/drivers/oracle.inc.php | 4 ++++ adminer/drivers/pgsql.inc.php | 12 ++++++++++++ adminer/drivers/sqlite.inc.php | 4 ++++ adminer/select.inc.php | 4 ++-- changes.txt | 1 + 7 files changed, 36 insertions(+), 2 deletions(-) diff --git a/adminer/drivers/mssql.inc.php b/adminer/drivers/mssql.inc.php index 84b085340..9fa19c6f9 100644 --- a/adminer/drivers/mssql.inc.php +++ b/adminer/drivers/mssql.inc.php @@ -490,6 +490,10 @@ function explain($connection, $query) { return $return; } + function found_rows($table_status, $where) { + return false; + } + function foreign_keys($table) { $return = array(); foreach (get_rows("EXEC sp_fkeys @fktable_name = " . q($table)) as $row) { diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 29575ccb5..9932a7cb4 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -822,6 +822,15 @@ function explain($connection, $query) { return $connection->query("EXPLAIN $query"); } + /** Get approximate number of rows + * @param array + * @param array + * @return int or null if approximate number can't be retrieved + */ + function found_rows($table_status, $where) { + return ($where || $table_status["Engine"] != "InnoDB" ? null : $table_status["Rows"]); + } + /** Get user defined types * @return array */ diff --git a/adminer/drivers/oracle.inc.php b/adminer/drivers/oracle.inc.php index 878d5e3c9..afe60ccc9 100644 --- a/adminer/drivers/oracle.inc.php +++ b/adminer/drivers/oracle.inc.php @@ -275,6 +275,10 @@ function explain($connection, $query) { return $connection->query("SELECT * FROM plan_table"); } + function found_rows($table_status, $where) { + return false; + } + function alter_table($table, $name, $fields, $foreign, $comment, $engine, $collation, $auto_increment, $partitioning) { $alter = $drop = array(); foreach ($fields as $field) { diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index c36fbb67d..bab11b836 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -511,6 +511,18 @@ function explain($connection, $query) { return $connection->query("EXPLAIN $query"); } + function found_rows($table_status, $where) { + global $connection; + if (ereg( + " rows=([0-9]+)", + $connection->result("EXPLAIN SELECT * FROM " . idf_escape($table_status["Name"]) . ($where ? " WHERE " . implode(" AND ", $where) : "")), + $regs + )) { + return $regs[1]; + } + return false; + } + function types() { return get_vals("SELECT typname FROM pg_type diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index c7213e484..053f02f80 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -503,6 +503,10 @@ function explain($connection, $query) { return $connection->query("EXPLAIN $query"); } + function found_rows($table_status, $where) { + return false; + } + function types() { return array(); } diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 56f45dd33..81bf6ffef 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -373,8 +373,8 @@ if ($rows || $page) { $exact_count = true; if ($_GET["page"] != "last" && +$limit && count($group) >= count($select) && ($found_rows >= $limit || $page)) { - $found_rows = $table_status["Rows"]; - if (!isset($found_rows) || $where || ($table_status["Engine"] == "InnoDB" && $found_rows < max(1e4, 2 * ($page + 1) * $limit))) { + $found_rows = found_rows($table_status, $where); + if ($found_rows < max(1e4, 2 * ($page + 1) * $limit)) { // slow with big tables ob_flush(); //! doesn't work with AJAX flush(); diff --git a/changes.txt b/changes.txt index 9209e3dd0..c2a4bdbce 100644 --- a/changes.txt +++ b/changes.txt @@ -1,5 +1,6 @@ Adminer 3.3.2-dev: Don't scroll with AJAX select order and alter move column +Fast number of rows with big tables (PostgreSQL) Adminer 3.3.1 (released 2011-07-27): Fix XSS introduced in Adminer 3.2.0