Skip to content

Commit

Permalink
Use a single query to fetch database size
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kienan Stewart authored and camlafit committed Sep 28, 2023
1 parent f56809f commit 504e770
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions bureau/class/m_mysql.php
Expand Up @@ -1084,28 +1084,20 @@ function alternc_export_data($dir) {
* @param $db_login the login 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
* @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) {
global $msg;
$msg->debug("mysql", "get_dbus_size", $db_host);

$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();
foreach($alldb as $dbname) {
$c = $this->dbus->query("SHOW TABLE STATUS FROM $dbname;");
$size = 0;
while ($this->dbus->next_record()) {
$size+=$this->dbus->f("Data_length") + $this->dbus->f("Index_length");
}
$res["$dbname"] = "$size";
# Note: this returns the size in bytes
$this->dbus->query("SELECT table_schema, SUM(data_length + index_length) AS size FROM information_schema.tables GROUP BY table_schema;");

while ($this->dbus->next_record()) {
$res[$this->dbus->f("table_schema")] = $this->dbus->f("size");
}
return $res;
}
Expand Down

0 comments on commit 504e770

Please sign in to comment.