From 494855e5481a0c29023fe5b1aa74a326f147aac1 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 5 Aug 2026 12:36:32 +0300 Subject: [PATCH 1/2] Use union all --- src/Database/Adapter/MariaDB.php | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 3b275b0650..13ca8d9161 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -283,34 +283,34 @@ public function getSizeOfCollection(string $collection): int $database = $this->getDatabase(); $permissions = $collection . '_perms'; - $collectionSize = $this->getPDO()->prepare(" - SELECT SUM(data_length + index_length) - FROM INFORMATION_SCHEMA.TABLES - WHERE table_name = :name AND - table_schema = :database - "); - - $permissionsSize = $this->getPDO()->prepare(" - SELECT SUM(data_length + index_length) - FROM INFORMATION_SCHEMA.TABLES - WHERE table_name = :permissions AND - table_schema = :database + // Both tables in one round trip. Keep the equality predicates: LIKE and IN are + // not indexed here, they scan every table in the schema. + $statement = $this->getPDO()->prepare(" + SELECT SUM(size) FROM ( + SELECT data_length + index_length AS size + FROM INFORMATION_SCHEMA.TABLES + WHERE table_name = :name AND + table_schema = :database + UNION ALL + SELECT data_length + index_length AS size + FROM INFORMATION_SCHEMA.TABLES + WHERE table_name = :permissions AND + table_schema = :database + ) AS sizes "); - $collectionSize->bindParam(':name', $collection); - $collectionSize->bindParam(':database', $database); - $permissionsSize->bindParam(':permissions', $permissions); - $permissionsSize->bindParam(':database', $database); + $statement->bindParam(':name', $collection); + $statement->bindParam(':permissions', $permissions); + $statement->bindParam(':database', $database); try { - $collectionSize->execute(); - $permissionsSize->execute(); - $size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn(); + $statement->execute(); + $size = $statement->fetchColumn(); } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } - return $size; + return (int) $size; } /** From e9d3b1497a76730891fe1649f0dab5c4605d795e Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 5 Aug 2026 12:38:28 +0300 Subject: [PATCH 2/2] throw --- src/Database/Adapter/MariaDB.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 13ca8d9161..d5fca182a7 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -307,7 +307,7 @@ public function getSizeOfCollection(string $collection): int $statement->execute(); $size = $statement->fetchColumn(); } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } return (int) $size;