Skip to content

Commit 5a94062

Browse files
authored
Merge branch 'main' into fix/concurrent-collection-create
2 parents bd7ac3e + 383deae commit 5a94062

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

‎src/Database/Adapter/MariaDB.php‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -284,34 +284,35 @@ public function getSizeOfCollection(string $collection): int
284284
$database = $this->getDatabase();
285285
$permissions = $collection . '_perms';
286286

287-
$collectionSize = $this->getPDO()->prepare("
288-
SELECT SUM(data_length + index_length)
289-
FROM INFORMATION_SCHEMA.TABLES
290-
WHERE table_name = :name AND
291-
table_schema = :database
292-
");
293-
294-
$permissionsSize = $this->getPDO()->prepare("
295-
SELECT SUM(data_length + index_length)
296-
FROM INFORMATION_SCHEMA.TABLES
297-
WHERE table_name = :permissions AND
298-
table_schema = :database
287+
// Both tables in one round trip. Keep the equality predicates: LIKE and IN are
288+
// not indexed here, they scan every table in the schema.
289+
$statement = $this->getPDO()->prepare("
290+
SELECT SUM(size) FROM (
291+
SELECT data_length + index_length AS size
292+
FROM INFORMATION_SCHEMA.TABLES
293+
WHERE table_name = :name AND
294+
table_schema = :database_name
295+
UNION ALL
296+
SELECT data_length + index_length AS size
297+
FROM INFORMATION_SCHEMA.TABLES
298+
WHERE table_name = :permissions AND
299+
table_schema = :database_permissions
300+
) AS sizes
299301
");
300302

301-
$collectionSize->bindParam(':name', $collection);
302-
$collectionSize->bindParam(':database', $database);
303-
$permissionsSize->bindParam(':permissions', $permissions);
304-
$permissionsSize->bindParam(':database', $database);
303+
$statement->bindParam(':name', $collection);
304+
$statement->bindParam(':permissions', $permissions);
305+
$statement->bindParam(':database_name', $database);
306+
$statement->bindParam(':database_permissions', $database);
305307

306308
try {
307-
$collectionSize->execute();
308-
$permissionsSize->execute();
309-
$size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn();
309+
$statement->execute();
310+
$size = $statement->fetchColumn();
310311
} catch (PDOException $e) {
311312
throw new DatabaseException('Failed to get collection size: ' . $e->getMessage());
312313
}
313314

314-
return $size;
315+
return (int) $size;
315316
}
316317

317318
/**

0 commit comments

Comments
 (0)