1
0
mirror of https://github.com/flarum/core.git synced 2025-08-25 09:22:52 +02:00

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) public function find(User $actor, Builder $query)
{ {
// Hide discussions which have tags that the user is not allowed to see. // Hide discussions which have tags that the user is not allowed to see.
$query->whereNotExists(function ($query) use ($actor) { $query->whereNotIn('discussions.id', function ($query) use ($actor) {
return $query->selectRaw('1') return $query->select('discussion_id')
->from('discussion_tag') ->from('discussion_tag')
->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'viewDiscussions')) ->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'viewDiscussions'));
->whereColumn('discussions.id', 'discussion_id');
}); });
// Hide discussions with no tags if the user doesn't have that global // 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 // 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 // visible, then we can check if the user has been granted that
// permission for any of the discussion's tags. // permission for any of the discussion's tags.
$query->whereExists(function ($query) use ($actor, $ability) { $query->whereIn('discussions.id', function ($query) use ($actor, $ability) {
return $query->selectRaw('1') return $query->select('discussion_id')
->from('discussion_tag') ->from('discussion_tag')
->whereIn('tag_id', Tag::getIdsWhereCan($actor, 'discussion.'.$ability)) ->whereIn('tag_id', Tag::getIdsWhereCan($actor, 'discussion.'.$ability));
->whereColumn('discussions.id', 'discussion_id');
}); });
} }

View File

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

View File

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