I have 2 tables comments and images
I'm trying to order the comments that have an image first and then comments without images.
I used this query below to solve this issue but it's very slow on large data it takes about 12 seconds
$data = Comment::withCount([
'images' => function ($query) use ($shop)
{
$query->where('shop_name', $shop);
},
])->where('shop_name', $shop)->orderBy('images_count', 'desc')->paginate(10);
How can I improve the performance or is there any other way to get similar results in faster way ?