Improve performance of subqueries (#75)

This commit is contained in:
Ian Morland 2020-03-06 14:12:34 +00:00 committed by GitHub
parent aa6ace8699
commit c2f8aeeecc
3 changed files with 16 additions and 21 deletions

View File

@ -76,11 +76,10 @@ class DiscussionPolicy extends AbstractPolicy
public function find(User $actor, Builder $query)
{
// Hide discussions which have tags that the user is not allowed to see.
$query->whereNotExists(function ($query) use ($actor) {
return $query->selectRaw('1')
$query->whereNotIn('discussions.id', function ($query) use ($actor) {
return $query->select('discussion_id')
->from('discussion_tag')
->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'viewDiscussions'))
->whereColumn('discussions.id', 'discussion_id');
->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'viewDiscussions'));
});
// Hide discussions with no tags if the user doesn't have that global
@ -100,11 +99,10 @@ class DiscussionPolicy extends AbstractPolicy
// If a discussion requires a certain permission in order for it to be
// visible, then we can check if the user has been granted that
// permission for any of the discussion's tags.
$query->whereExists(function ($query) use ($actor, $ability) {
return $query->selectRaw('1')
$query->whereIn('discussions.id', function ($query) use ($actor, $ability) {
return $query->select('discussion_id')
->from('discussion_tag')
->whereIn('tag_id', Tag::getIdsWhereCan($actor, 'discussion.'.$ability))
->whereColumn('discussions.id', 'discussion_id');
->whereIn('tag_id', Tag::getIdsWhereCan($actor, 'discussion.'.$ability));
});
}

View File

@ -43,18 +43,16 @@ class TagGambit extends AbstractRegexGambit
$search->getQuery()->where(function ($query) use ($slugs, $negate) {
foreach ($slugs as $slug) {
if ($slug === 'untagged') {
$query->orWhereExists(function ($query) {
$query->selectRaw('1')
->from('discussion_tag')
->whereColumn('discussions.id', 'discussion_id');
$query->orWhereIn('discussions.id', function ($query) {
$query->select('discussion_id')
->from('discussion_tag');
}, ! $negate);
} else {
$id = $this->tags->getIdForSlug($slug);
$query->orWhereExists(function ($query) use ($id) {
$query->selectRaw('1')
$query->orWhereIn('discussions.id', function ($query) use ($id) {
$query->select('discussion_id')
->from('discussion_tag')
->whereColumn('discussions.id', 'discussion_id')
->where('tag_id', $id);
}, $negate);
}

View File

@ -45,11 +45,10 @@ class FilterDiscussionListByTags
return;
}
$query->whereNotExists(function ($query) {
return $query->selectRaw('1')
$query->whereNotIn('discussions.id', function ($query) {
return $query->select('discussion_id')
->from('discussion_tag')
->whereIn('tag_id', Tag::where('is_hidden', 1)->pluck('id'))
->whereColumn('discussions.id', 'discussion_id');
->whereIn('tag_id', Tag::where('is_hidden', 1)->pluck('id'));
});
}
}