Skip to content

Commit

Permalink
Fix for handle large number of tables in a single database (more than…
Browse files Browse the repository at this point in the history
… 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 <github@pico12.fr>
  • Loading branch information
PicoI2 authored and MauricioFauth committed Mar 24, 2023
1 parent 56d19eb commit e4d9a1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions libraries/classes/DatabaseInterface.php
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Util.php
Expand Up @@ -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,
Expand Down

0 comments on commit e4d9a1a

Please sign in to comment.