Use a single query to fetch database size

The procedure can use a single query instead of a 1 + N queries, where
N is the number of tables on the server.

There is a minor side-effect in the result: databases with no tables
will not be listed in the output. Before, they would be output but
with an empty size value.

close #452
This commit is contained in:
Kienan Stewart 2021-03-24 12:02:19 -04:00 committed by Camille Lafitte
parent f56809fd8a
commit 504e77034f
1 changed files with 6 additions and 14 deletions

View File

@ -1084,7 +1084,7 @@ class m_mysql {
* @param $db_login the login to access the SQL db * @param $db_login the login to access the SQL db
* @param $db_password the password to access the SQL db * @param $db_password the password to access the SQL db
* @param $db_client the client to access the SQL db * @param $db_client the client to access the SQL db
* @return an array associating the name of the databases to their sizes : array(dbname=>size) * @return an array associating the name of the databases to their size in bytes : array(dbname=>size)
*/ */
function get_dbus_size($db_name, $db_host, $db_login, $db_password, $db_client) { function get_dbus_size($db_name, $db_host, $db_login, $db_password, $db_client) {
global $msg; global $msg;
@ -1092,20 +1092,12 @@ class m_mysql {
$this->dbus = new DB_Sql("mysql",$db_host,$db_login,$db_password); $this->dbus = new DB_Sql("mysql",$db_host,$db_login,$db_password);
$this->dbus->query("SHOW DATABASES;");
$alldb=array();
while ($this->dbus->next_record()) {
$alldb[] = $this->dbus->f("Database");
}
$res = array(); $res = array();
foreach($alldb as $dbname) { # Note: this returns the size in bytes
$c = $this->dbus->query("SHOW TABLE STATUS FROM $dbname;"); $this->dbus->query("SELECT table_schema, SUM(data_length + index_length) AS size FROM information_schema.tables GROUP BY table_schema;");
$size = 0;
while ($this->dbus->next_record()) { while ($this->dbus->next_record()) {
$size+=$this->dbus->f("Data_length") + $this->dbus->f("Index_length"); $res[$this->dbus->f("table_schema")] = $this->dbus->f("size");
}
$res["$dbname"] = "$size";
} }
return $res; return $res;
} }