Laravel Eloquent vs query builder - Why use eloque

2019-01-08 05:44发布

问题:

I did some performance test between Laravel query builder and eloquent. Query builder was much faster with various of sql statement (select-update-delete-insert).

So my question is: Why someone uses Laravel Eloquent against plain query builder?

回答1:

Eloquent is Laravel's implementation of Active Record pattern and it comes with all its strengths and weaknesses. It is a good solution to use when you process a single entity in a CRUD manner - that is, read from database or create a new entity and then save it or delete. You will benefit a lot from Eloquent's features such as dirty checking (to send SQL UPDATE only for the fields which have been changed), model events (e.g. to send administrative alert or update statistics counter when someone has created a new account), traits (timestamps, soft deletes, custom traits) eager/lazy loading etc.

But, as you already know, it comes with some performance price. When you process a single or just a few records, there is nothing to worry about. But for cases when you read lots of records (e.g. for datagrids, for reports, for batch processing etc.) the plain DB is better approach.

For our application we are doing exactly that - using Laravel's Eloquent in web forms to process a single record and using DB (with SQL views) to retrieve data for grids, export etc.



回答2:

Why Laravel Eloquent:

  1. Executing query in a OOP manner.
  2. Easy to use than raw query or query builder.
  3. No binding with table schema. i.e. Even you change your table name, not need to touch a single query(there may have 1000 query) to make it work. Just change the table name in model.
  4. Relationship among tables can be maintain in an elegant way. Just mention the type of relationship, nothing else(JOIN, LEFT JOIN, RIGHT JOIN etc.) needed in query anymore to get data of related tables.
  5. Queries highly readable while written using Eloquent comparing with Query Builder.


回答3:

Yes, In some case you are right. When we've more data and almost in every site, data is not small really. Then it is better to use DB Query than the Eloquent Query.

In a performance issue of Eloquent VS DB I've heard that,

To insert 1000 rows for a simple table Eloquent takes 1.2 seconds and in that case DB facades take only 800 mili seconds(ms).

So Why then Eloquent ? Is't any necessary of that ?

Answer is - Eloquent is also necessary. Cause-

To create a better relationship and get the results in view with so much simple syntax, when there needs to join.

Eloquent is also for who have not much knowledge of SQL query.

An MVC framework follow the rules of Code Readability, Code Maintainability and which Eloquent is, you know that. A code comparison below. Obviously, Eloquent is better to read.

// In Eloquent
$student = App\Student::find($id);

// In DB facade
$student = DB::table('student')->where('id', $id)->first();

Most important part is if we want to change other database, then raw query will be a lot much headache to us and in that case Laravel Eloquent will solve all the problems with one hand. It can handle different types Database.

So when we use Eloquent and When DB facades:

  1. When we'll work on a simple and small records site with simple CRUD and there records are not fact, then use Eloquent there.
  2. When we'll work on a lot's of records, it is better to use DB Query than Eloquent.

So, finally it is cleared that - when we'll use Database Query and When we'll use Eloquent Query.

Edit - Real Life Example

  1. I'm making an University website. Which may contain maximum 5,000 teachers and 10,000 students and some notices and files. Then it is better to do this with simple Laravel Eloquent which is very much standard and readable.
  2. Now I'm making a site like Stackoverflow. Which may contains more than 1,000,0000 (1 crore) posts and many more things. I will must choose the conventional DB facades there. It is more faster for searching the posts from so much records.

Now it's your choice. What you want to make...



回答4:

When it comes to performance and the application grows, for the sake of comparison, take a loot at the following tables:

Comparison of select operation average response time between Eloquent ORM and Raw SQL

Eloquent ORM average response time

Joins | Average (ms) 
------+-------------
1     | 162,2 
3     | 1002,7 
4     | 1540,0 

Result of select operation average response time for Eloquent ORM 

Raw SQL average response time

Joins | Average (ms) 
------+-------------
1     | 116,4 
3     | 130,6 
4     | 155,2 

Result of select operation average response time for Raw SQL 

Article Reference



回答5:

It is just my opinion, not a comprehensive answer. I use whatever is more convenient in a given situation.

If I come across a package or code written either in eloquent or query builder, I use whatever is being used.

I found query builder to be more intuitive if I create something from scratch so I use it more often.

When it comes to Laravel, it seems, the ease and speed of developing an app is more important then performance. I really like that they make everything very easy even for someone with little prior knowledge of php/mysql. In some cases eloquent is easier than query builder. In others vice versa. I think having many ways of doing something is what makes Laravel so easy and newbie friendly.



回答6:

I like using query builder when building complex query from database because it seems easy to use. For working with a single table, I like eloquent.