Laravel-Challenge-Movie-Table-Eloquent
Laravel-Challenge-Movie-Table-Eloquent copied to clipboard
Optimize eloquent query
I expected that doing Movie::with(['ratings', 'category'])
would make a query like
SELECT * FROM movies LEFT JOIN ratings ON ratings.movie_id = movies.id
LEFT JOIN categories ON movies.category_id = categories.id;
but it didn't, instead it made 3 separate queries. Other ORMs like TypeORM do this with 1 query that uses LEFT JOIN
which is very effective.
I searched around a bit and I found this post.
Hope you can discuss this in a future video. I enjoyed it a lot.
@avidianity thanks for the comment, I definitely will discuss it in a future video.
As for your solution, I think you misunderstood the task, the task was to sort all movies by avg rating and then get top 100 from that. In your case, I see you're getting 100 movies first, and only then doing the sorting from those 100.