From 50bbe321758cdf46e13d9361502c8f46b8b0a827 Mon Sep 17 00:00:00 2001 From: Maxim Chistyakov Date: Thu, 12 May 2016 17:07:41 +0300 Subject: [PATCH] SQL Performance tuning (#952) MySQL has problems with executing this subquery efficiently. --- framework/core/src/Core/Repository/PostRepository.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/framework/core/src/Core/Repository/PostRepository.php b/framework/core/src/Core/Repository/PostRepository.php index 7c14c6192..aa18b00ee 100644 --- a/framework/core/src/Core/Repository/PostRepository.php +++ b/framework/core/src/Core/Repository/PostRepository.php @@ -146,9 +146,11 @@ class PostRepository protected function getDiscussionsForPosts($postIds, User $actor) { - return Discussion::whereIn('id', function ($query) use ($postIds) { - $query->select('discussion_id')->from('posts')->whereIn('id', $postIds); - }) + return Discussion::query() + ->select('discussions.*') + ->join('posts', 'posts.discussion_id', '=', 'discussions.id') + ->whereIn('posts.id', $postIds) + ->groupBy('discussions.id') ->whereVisibleTo($actor) ->get(); }