Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize polls loading #3692

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions lib/Db/PollMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,17 @@
$paramUser = $qb->createNamedParameter($currentUserId, IQueryBuilder::PARAM_STR);
$paramAnswerYes = $qb->createNamedParameter(Vote::VOTE_YES, IQueryBuilder::PARAM_STR);

$qb->selectAlias($qb->createFunction('(' . $this->subQueryVotesCount(self::TABLE, $paramUser)->getSQL() . ')'), 'current_user_count_votes');
$qb->selectAlias($qb->createFunction('(' . $this->subQueryVotesCount(self::TABLE, $paramUser, $paramAnswerYes)->getSQL() . ')'), 'current_user_count_votes_yes');
// migrated to joinVotesCount
// $qb->selectAlias($qb->createFunction('(' . $this->subQueryVotesCount(self::TABLE, $paramUser)->getSQL() . ')'), 'current_user_count_votes');
// $qb->selectAlias($qb->createFunction('(' . $this->subQueryVotesCount(self::TABLE, $paramUser, $paramAnswerYes)->getSQL() . ')'), 'current_user_count_votes_yes');
$this->joinVotesCount($qb, self::TABLE, $paramUser, $paramAnswerYes);

$qb->selectAlias($qb->createFunction('(' . $this->subQueryOrphanedVotesCount(self::TABLE, $paramUser)->getSQL() . ')'), 'current_user_count_orphaned_votes');

$this->joinOptionsForMaxDate($qb, self::TABLE);
$this->joinUserRole($qb, self::TABLE, $currentUserId);
$this->joinGroupShares($qb, self::TABLE);
\OC::$server->getLogger()->error(json_encode($qb->getSQL()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
\OC::$server->getLogger()->error(json_encode($qb->getSQL()));
\OCP\Server::get(\Psr\Log\LoggerInterface::class)->error(json_encode($qb->getSQL()));

\OC::$server and getLogger are deprecated.
That said I know realise this one is left over debug and will be removed before merge.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe. Yeah, I know. Thats just an old macro I use for quick logging of implementation steps. Should not have made it into the repo.

return $qb;
}

Expand Down Expand Up @@ -290,6 +294,30 @@
}


/**
* Subquery for votes count
*/
protected function joinVotesCount(IQueryBuilder &$qb, string $fromAlias, IParameter $currentUserId, ?IParameter $answerFilter = null): IQueryBuilder {

Check failure on line 300 in lib/Db/PollMapper.php

View workflow job for this annotation

GitHub Actions / Psalm (stable27, 8.1)

PossiblyUnusedParam

lib/Db/PollMapper.php:300:114: PossiblyUnusedParam: Param #4 is never referenced in this method (see https://psalm.dev/134)

Check failure on line 300 in lib/Db/PollMapper.php

View workflow job for this annotation

GitHub Actions / Psalm (stable27, 8.1)

PossiblyUnusedReturnValue

lib/Db/PollMapper.php:300:137: PossiblyUnusedReturnValue: The return value for this method is never used (see https://psalm.dev/273)

Check failure on line 300 in lib/Db/PollMapper.php

View workflow job for this annotation

GitHub Actions / Psalm (master, 8.1)

PossiblyUnusedParam

lib/Db/PollMapper.php:300:114: PossiblyUnusedParam: Param #4 is never referenced in this method (see https://psalm.dev/134)

Check failure on line 300 in lib/Db/PollMapper.php

View workflow job for this annotation

GitHub Actions / Psalm (master, 8.1)

PossiblyUnusedReturnValue

lib/Db/PollMapper.php:300:137: PossiblyUnusedReturnValue: The return value for this method is never used (see https://psalm.dev/273)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function joinVotesCount(IQueryBuilder &$qb, string $fromAlias, IParameter $currentUserId, ?IParameter $answerFilter = null): IQueryBuilder {
protected function joinVotesCount(IQueryBuilder $qb, string $fromAlias, IParameter $currentUserId, ?IParameter $answerFilter = null): IQueryBuilder {

You only need a reference if you intend to replace the object with another one ($qb = xxx).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, right. But I would say for consistency the return should be removed, since all joins here base on references.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But even the joins do not require the parameter to be a reference.

$joinAlias = 'user_vote_sub';

$qb->selectAlias($qb->func()->count($joinAlias . '.vote_answer'), 'current_user_count_votes');

$qb->selectAlias($qb->func()->sum(
$qb->createFunction('CASE WHEN '. $joinAlias . '.vote_answer = \'yes\' THEN 1 ELSE 0 END')
), 'current_user_count_votes_yes');

$qb->leftJoin(
$fromAlias,
Vote::TABLE,
$joinAlias,
$qb->expr()->andX(
$qb->expr()->eq($joinAlias . '.poll_id', $fromAlias . '.id'),
$qb->expr()->eq($joinAlias . '.user_id', $currentUserId),
)
);

return $qb;
}

/**
* Subquery for votes count
Expand Down
Loading