Skip to content

Commit

Permalink
fix: Slow join on documents to messages. (dvsa/olcs-backend#95)
Browse files Browse the repository at this point in the history
* fix: Slow join on documents to messages.

* chore: Typing
  • Loading branch information
wadedvsa authored Mar 6, 2024
1 parent 30d0b52 commit 43b1347
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function handleQuery(QueryInterface $query)
$unreadMessageCount = $this->getUnreadMessageCountForUser($value);
$conversations[$key]['userContextUnreadCount'] = $unreadMessageCount;
$conversations[$key]['userContextStatus'] = $this->stringifyMessageStatusForUser($value, $unreadMessageCount);
$conversations[$key]['latestMessage'] = $this->getLatestMessageMetadata($value['id']);
$conversations[$key]['latestMessage'] = $this->getLatestMessageMetadata((int)$value['id']);
}

$conversations = $this->orderResultPrioritisingNewMessages($conversations);
Expand All @@ -51,17 +51,14 @@ private function getUnreadMessageCountForUser($conversation): int
return count($results);
}

private function getLatestMessageMetadata($conversationId): array
private function getLatestMessageMetadata(int $conversationId): array
{
$messageRepository = $this->getRepo('Message');
assert($messageRepository instanceof MessageRepo);
return $messageRepository->getLastMessageByConversationId($conversationId);
}

private function getRepository(): ConversationRepo
{
$conversationRepository = $this->getRepo('Conversation');
assert($conversationRepository instanceof ConversationRepo);
return $conversationRepository;
return $this->getRepo('Conversation');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function handleQuery(QueryInterface $query): array
);
$conversations[$key]['userContextUnreadCount'] = $unreadMessageCount;
$conversations[$key]['userContextStatus'] = $this->stringifyMessageStatusForUser($value, $unreadMessageCount);
$conversations[$key]['latestMessage'] = $messageRepository->getLastMessageByConversationId($value['id']);
$conversations[$key]['latestMessage'] = $messageRepository->getLastMessageByConversationId((int)$value['id']);
}

$conversations = $this->orderResultPrioritisingNewMessages($conversations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handleQuery(QueryInterface $query)

$messageQueryBuilder = $messageRepository->getBaseMessageListWithContentQuery($query);

$messagesQuery = $messageRepository->filterByConversationId($messageQueryBuilder, $query->getConversation());
$messagesQuery = $messageRepository->filterByConversationId($messageQueryBuilder, (int)$query->getConversation());

$messages = $messageRepository->fetchPaginatedList($messagesQuery);

Expand Down
14 changes: 11 additions & 3 deletions app/api/module/Api/src/Domain/Repository/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dvsa\Olcs\Api\Domain\Repository;

use Doctrine\DBAL\ParameterType;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Dvsa\Olcs\Api\Entity\Messaging\MessagingMessage as Entity;
Expand Down Expand Up @@ -30,13 +31,20 @@ public function getBaseMessageListWithContentQuery(QueryInterface $query): Query

$this->getQueryBuilder()->modifyQuery($qb)
->with('messagingContent')
->with('documents')
->withCreatedByWithTeam();

$qb->leftJoin(
$this->alias . '.documents',
'd',
Query\Expr\Join::WITH,
$this->alias . '.messagingConversation = d.messagingConversation'
);
$qb->addSelect('d');

return $qb;
}

public function filterByConversationId(QueryBuilder $qb, $conversationId): QueryBuilder
public function filterByConversationId(QueryBuilder $qb, int $conversationId): QueryBuilder
{
$qb
->andWhere($qb->expr()->eq($this->alias . '.messagingConversation', ':messagingConversation'))
Expand All @@ -45,7 +53,7 @@ public function filterByConversationId(QueryBuilder $qb, $conversationId): Query
return $qb;
}

public function getLastMessageByConversationId($conversationId): array
public function getLastMessageByConversationId(int $conversationId): array
{
$qb = $this->createQueryBuilder();
$qb = $this->filterByConversationId($qb, $conversationId);
Expand Down

0 comments on commit 43b1347

Please sign in to comment.