mirror of
https://github.com/flarum/framework.git
synced 2024-12-01 22:43:41 +08:00
Improve performance of subqueries (#75)
This commit is contained in:
parent
aa6ace8699
commit
c2f8aeeecc
|
@ -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));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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'));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user