1
0
mirror of https://github.com/flarum/core.git synced 2025-08-13 11:54:32 +02:00

Compare commits

...

10 Commits

Author SHA1 Message Date
StyleCI Bot
88b5918e3a Apply fixes from StyleCI
[ci skip] [skip ci]
2022-03-24 13:28:40 +00:00
Daniël Klabbers
d0119e7634 Merge branch 'dk/20220318-tag-index' into dk/20220324-performance-test 2022-03-24 14:28:04 +01:00
Daniël Klabbers
726c8f3a9a missed another prefix for tests 2022-03-24 00:35:41 +01:00
StyleCI Bot
8fe1d1cabc Apply fixes from StyleCI
[ci skip] [skip ci]
2022-03-23 23:28:45 +00:00
Daniël Klabbers
d0b4e74b1f fix prefix failing in tests 2022-03-24 00:28:25 +01:00
StyleCI Bot
65c861ab7b Apply fixes from StyleCI
[ci skip] [skip ci]
2022-03-23 20:30:55 +00:00
Daniël Klabbers
cb9fe6930f possibly fixes the issue with post number calculation 2022-03-23 21:30:46 +01:00
Daniël Klabbers
3343fde5f2 add distinct and clean up code further 2022-03-23 12:05:03 +01:00
StyleCI Bot
907b1f10aa Apply fixes from StyleCI
[ci skip] [skip ci]
2022-03-18 15:46:30 +00:00
Daniël Klabbers
ae7d31ec16 fix(tags)!: reduce tag filter query from 3+ second
This PR removes the subqueries inserted for each selected tag. As this
caused the query to take over 3 seconds on communities with hundred
million posts.

```sql
and (`discussions`.`id` in (select `discussion_id` from `discussion_tag` where `tag_id` = 105))
```

The above part takes on average 3.85 seconds, with a join it takes 0.00
seconds.

BREAKING CHANGE: extending the query might have different outcome as we
now use a join and no subquery.
2022-03-18 16:40:30 +01:00
2 changed files with 32 additions and 18 deletions

View File

@@ -55,23 +55,23 @@ class TagFilterGambit extends AbstractRegexGambit implements FilterInterface
{
$slugs = explode(',', trim($rawSlugs, '"'));
$query->where(function (Builder $query) use ($slugs, $negate) {
foreach ($slugs as $slug) {
if ($slug === 'untagged') {
$query->whereIn('discussions.id', function (Builder $query) {
$query->select('discussion_id')
->from('discussion_tag');
}, 'or', ! $negate);
} else {
$id = $this->tags->getIdForSlug($slug);
$query->whereIn('discussions.id', function (Builder $query) use ($id) {
$query->select('discussion_id')
->from('discussion_tag')
->where('tag_id', $id);
}, 'or', $negate);
$query
->distinct()
->leftJoin('discussion_tag', 'discussions.id', '=', 'discussion_tag.discussion_id')
->where(function (Builder $query) use ($slugs, $negate) {
foreach ($slugs as $slug) {
if ($slug === 'untagged' && ! $negate) {
$query->orWhereNull('discussion_tag.tag_id');
} elseif ($slug === 'untagged' && $negate) {
$query->orWhereNotNull('discussion_tag.tag_id');
} elseif ($id = $this->tags->getIdForSlug($slug)) {
$query->orWhere(
'discussion_tag.tag_id',
$negate ? '!=' : '=',
$id
);
}
}
}
});
});
}
}

View File

@@ -16,7 +16,9 @@ use Flarum\Foundation\EventGeneratorTrait;
use Flarum\Notification\Notification;
use Flarum\Post\Event\Deleted;
use Flarum\User\User;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
/**
* @property int $id
@@ -91,7 +93,19 @@ class Post extends AbstractModel
// discussion.
static::creating(function (self $post) {
$post->type = $post::$type;
$post->number = ++$post->discussion->post_number_index;
/** @var ConnectionInterface $db */
$db = static::getConnectionResolver();
$post->number = new Expression('('.$db
->table('posts', 'pn')
->whereRaw($db->getTablePrefix().'pn.discussion_id = '.intval($post->discussion_id))
->select($db->raw('max('.$db->getTablePrefix().'pn.number) + 1'))
->toSql()
.')');
});
static::created(function (self $post) {
$post->refresh();
$post->discussion->save();
});