Performance is poor
Closed this issue · 2 comments
Hi Sasha,
Great job on this extension. I was test driving this on the Bokt staging (running in sync with their current phpBB forum) flarum installation. I wasn't completely aware about the reason for poor performance on that site, assuming this related to some DNS resolution issues I had. However I patched fof/clockwork for use and excluded DNS as an issue by triangulating my requests.
Anyway, the issue is this:
if ($event->isSerializer(TagSerializer::class)) {
$event->attributes['discussionCount'] = $event->model->discussions()->whereVisibleTo($event->actor)->count();
$event->attributes['postCount'] = $event->model->discussions()->whereVisibleTo($event->actor)->sum('comment_count');
}
Bokt has many tags and this code will load all discussions twice, do a count and sum separately. Maybe this is a solution:
if ($event->isSerializer(TagSerializer::class)) {
$result = $event->model->discussions()
->select(['sum(comment_count) as postCount', 'count(id) as discussionCount'])
->whereVisibleTo($event->actor)
->get();
$event->attributes['discussionCount'] = $result['discussionCount'];
$event->attributes['postCount'] = $result['postCount'];
}
Untested 🙈
This would half the number of sql queries. It's still not great with many tags, I think Flarum reads them all on page load 🙈
PS this is the performance capture from clockwork:
You could possibly cache these results.. That would make this so much faster..
Fixed in v1.0.1, thank you for the awesomely detailed bug report (and code, which I ended up copying into a 'Small Forum Optimized' mode)