Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel eager loading vs explicit join

This might sound like an obvious question but I just want to get some reassurance.

Using Laravel's eager loading functionality, from what I understand it will create two queries to return a whole list of related results (say if you're working with two tables). However, and correct me if I'm wrong, using a join statement will leave you with only one query, which creates one less round trip to the server's database (MySQL) and is a more efficient query.

I know that you can write join queries in Laravel, which is great, so the question is: am I incorrect to assume that when retrieving related data from two or more tables, should I not bother with eager loading and instead just write my own join statements?

****** Edit *******

Coming back to this one year later, I'd say in my personal opinion, just write the queries, raw, and write them well.

******** Edit 2 *********

Okay now six years later, I keep getting points for this.

Whether I was unclear from the beginning or not, contrary to what I've said above, Eloquent at this point writes great queries. Use Eloquent - even if there's a slight query inefficiency, it allows you to write very clear, maintainable code which at this point in my career I would argue is more important in most cases. Only write raw queries and optimize in cases where performance enhancements are critical and you can measure the impact.

like image 361
coleman-benjamin Avatar asked Jun 17 '14 19:06

coleman-benjamin


People also ask

Are joins or subqueries faster?

The advantage of a join includes that it executes faster. The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query.

What is difference between eager and lazy loading laravel?

The main difference between eager and lazy loading is eager loading get all data with relationship records in single query and lazy loading require N+1 queries for getting main model and relation data. Eager loading run single query whereas lazy loading run N+1 queries.

What is laravel eager loading?

Laravel eager loading. What is eager loading? Eager loading is a concept in which when retrieving items, you get all the needed items together with all (or most) related items at the same time. This is in contrast to lazy loading where you only get one item at one go and then retrieve related items only when needed.

What is lazy loading in laravel?

1 Lazy Loading : In Laravel Lazy Loading can be seen on two ways but both are nuance. Let us suppose a relationship: Users and Posts. a User can have multiple Posts. a Post can have single User. Here we can say User is parent table for Posts.


1 Answers

You are absolutely right about your understanding. If you write a join statement to join two or more tables using join() in Laravel then it makes only one query where using an Eloquent model with eager loading technique requires more than one query.

should I not bother with eager loading and instead just write my own join statements

Actually, the eager loading is a technique to load related models using Eloquent ORM easily and it uses Query Builder behind the scene and lets you use Eloquent Model Object without making the query by your self and represents the data differently, Using Eloquent ORM you are able to interact with model directly which represent objects in the database with additional features. Most importantly, it hides the complexity of SQL and allows you to do the database query in an OOP fashion using PHP code.

But when you manually call join method which belongs to Illuminate\Database\Query\Builder class then you are using the Query Builder directly which requires you write more code and requires more knowledge of sql query because it doesn't hide the query from you but helps you make queries more precisely, but you still make queries.

Both are different things and they work differently. You may search on Google using the term ORM vs Query Builder.

like image 70
The Alpha Avatar answered Sep 27 '22 00:09

The Alpha