From 07658c3b63585d490cb88d0a2073abda5924fbe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 13 Jul 2023 16:19:07 +0200 Subject: [PATCH] fix: Split query to fetch board ids to avoid slow query join MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/BoardMapper.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/Db/BoardMapper.php b/lib/Db/BoardMapper.php index c5df1b6a3..67f099111 100644 --- a/lib/Db/BoardMapper.php +++ b/lib/Db/BoardMapper.php @@ -116,6 +116,22 @@ public function findBoardIds(string $userId): array { $qb->expr()->eq('owner', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)), )); + $result = $qb->executeQuery(); + $ownerBoards = array_map(function (string $id) { + return (int)$id; + }, $result->fetchAll(\PDO::FETCH_COLUMN)); + $result->closeCursor(); + + $qb = $this->db->getQueryBuilder(); + $qb->selectDistinct('b.id') + ->from($this->getTableName(), 'b') + ->leftJoin('b', 'deck_board_acl', 'acl', $qb->expr()->eq('b.id', 'acl.board_id')); + + // Owned by the user + $qb->where($qb->expr()->andX( + $qb->expr()->eq('owner', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)), + )); + // Shared to the user $qb->orWhere($qb->expr()->andX( $qb->expr()->eq('acl.participant', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)), @@ -141,9 +157,11 @@ public function findBoardIds(string $userId): array { } $result = $qb->executeQuery(); - return array_map(function (string $id) { + $sharedBoards = array_map(function (string $id) { return (int)$id; }, $result->fetchAll(\PDO::FETCH_COLUMN)); + $result->closeCursor(); + return array_unique(array_merge($ownerBoards, $sharedBoards)); } public function findAllForUser(string $userId, ?int $since = null, bool $includeArchived = true, ?int $before = null,