From e4d9a1a66ab0b036d7925167a6259ab96d251426 Mon Sep 17 00:00:00 2001 From: Nicolas Hervouet Date: Sun, 11 Sep 2022 11:44:02 +0200 Subject: [PATCH] Fix for handle large number of tables in a single database (more than 10k) getTablesFull was taking too much time, because MySQL do not have a real table where it stores all tables information. So when it runs a query like select * from information_schema.TABLES ORDER BY Name limit 250 offset 0, MySQL has to read information about all tables to build his in memory table, then sort it by name, and then return 250 first tables. But reading information about one table takes time, and that's why the query is very slow when you have a lot of tables. Note that the problem is the same with 'SHOW TABLE STATUS'. So, in order to speed up this query, we have to limit it to a small list of tables, as it, MySQL has to read table information only for few tables. Note that SHOW TABLES is not that long, even with a large amount of tables. The idea here is to use the result of SHOW TABLES query, to apply paging on this table list, and then to run the query on information_schema.TABLES only on tables we want. Signed-off-by: Nicolas Hervouet --- libraries/classes/DatabaseInterface.php | 13 +++++++++++-- libraries/classes/Util.php | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php index 5dc2886006e5..e9fce75c4dc1 100644 --- a/libraries/classes/DatabaseInterface.php +++ b/libraries/classes/DatabaseInterface.php @@ -385,6 +385,15 @@ public function getTablesFull( } $tables = []; + $paging_applied = false; + + if ($limit_count && is_array($table) && $sort_by === 'Name') { + if ($sort_order === 'DESC') { + $table = array_reverse($table); + } + $table = array_slice($table, $limit_offset, $limit_count); + $paging_applied = true; + } if (! $GLOBALS['cfg']['Server']['DisableIS']) { $sql_where_table = QueryGenerator::getTableCondition( @@ -412,7 +421,7 @@ public function getTablesFull( // Sort the tables $sql .= ' ORDER BY ' . $sort_by . ' ' . $sort_order; - if ($limit_count) { + if ($limit_count && ! $paging_applied) { $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset; } @@ -593,7 +602,7 @@ static function ($a, $b) { unset($sortValues); } - if ($limit_count) { + if ($limit_count && ! $paging_applied) { $each_tables = array_slice($each_tables, $limit_offset, $limit_count, true); } diff --git a/libraries/classes/Util.php b/libraries/classes/Util.php index 335cd35f8e08..eb71a244f666 100644 --- a/libraries/classes/Util.php +++ b/libraries/classes/Util.php @@ -2292,7 +2292,7 @@ public static function getDbInfo($db, string $subPart) $tables = $groupTable + $dbi->getTablesFull( $db, - $groupWithSeparator !== false ? $groupWithSeparator : '', + $groupWithSeparator !== false ? $groupWithSeparator : $tables, $groupWithSeparator !== false, $limitOffset, $limitCount,