Skip to content

Commit

Permalink
fix: Split query to fetch board ids to avoid slow query join
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliushaertl committed Jul 13, 2023
1 parent 1a89685 commit 07658c3
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/Db/BoardMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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,
Expand Down

0 comments on commit 07658c3

Please sign in to comment.