Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent vs query builder - Why use eloquent to decrease performance [closed]

I did some performance tests between Laravel's DB facade query builder and Laravel's Eloquent ORM. The DB facade was much faster than Eloquent for many SQL statements (SELECT, UPDATE, DELETE, INSERT).

So why would someone use the slower Laravel Eloquent rather than the faster DB facade?

like image 201
Panagiotis Koursaris Avatar asked Jul 15 '16 08:07

Panagiotis Koursaris


People also ask

Which is better eloquent or query builder in Laravel?

When you are handling more data, it is better to use Laravel's DB facade query builder than Laravel's Eloquent ORM. From performance tests, inserting 1000 rows in a simple table takes Eloquent 1.2 seconds whereas the DB facade takes only 800 milliseconds.

Why we use eloquent in Laravel?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Is Laravel eloquent slow?

Laracasts VeteranSure it is slow, first it fetch all the record in one time, then it instantiate an eloquent object for each lines. It is not made to retrieve 10 000 objects.


4 Answers

Eloquent is Laravel's implementation of Active Record pattern and it comes with all its strengths and weaknesses.

Active Record is a good solution for processing a single entity in CRUD manner - that is, create a new entity with filled properties and then save it to a database, load a record from a database, 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 alerts or update statistics counters when someone has created a new account), traits (timestamps, soft deletes, your custom traits) eager/lazy loading etc. You can also apply domain-driven pattern and implement some pieces of business logic in your Active Record entities, for example, validation, managing relations, calculations etc.

But, as you already know, Active Record comes with some performance price.

When you process a single record or 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 Laravel DB methods is a better approach.

For our Laravel based applications we are using both approaches as we see appropriate. We use Laravel's Eloquent for UI forms to process a single record and use DB methods (backed by SQL views with additional database engine specific performance tweaks) to retrieve data for UI tables, export tasks etc. It also works well with RESTful APIs - Eloquent for GET, PUT, POST, DELETE with a key and DB for GET without key but with filters and sorting and paging.

like image 73
JustAMartin Avatar answered Oct 17 '22 10:10

JustAMartin


Yes, in some case you are correct.

When you are handling more data, it is better to use Laravel's DB facade query builder than Laravel's Eloquent ORM.

From performance tests, inserting 1000 rows in a simple table takes Eloquent 1.2 seconds whereas the DB facade takes only 800 milliseconds.

So why use Eloquent at all? Eloquent is also important, because:

  • It has a simpler syntax than the DB facade.

  • It is easier for developers who don't know SQL. You need to know SQL to use the DB facade.

  • Code written using Eloquent is more readable and thus more maintainable than code written using the DB facade:

    // With Eloquent
    $student = App\Student::find($id);
    
    // With the DB facade
    $student = DB::table('student')->where('id', $id)->first();
    
  • If you want to change the database, it will be easier with Laravel Eloquent as it can handle many different databases, whereas the DB facade requires you to write SQL which may have to be rewritten for a different database.

So use Eloquent when you work on a small site with simple CRUD operations, and use the DB facade when you use many joins and other features not supported by Eloquent.

Real-life examples:

  1. You're making a university website, which contains 5,000 teachers, and 10,000 students, and some notices and files. It would be better to build this site with Laravel Eloquent as it is simple and readable.

  2. You're making a high-traffic site like Stack Overflow, which serves more than 100 million people every month and has more than 70 million posts. It would be better to use the DB facade as it is faster and will lead to significantly faster response times.

You can check your query performance using Laravel Debugbar.

Here is a full comparison of performance, memory consumption and code quality between Eloquent and the DB facade.

like image 35
Maniruzzaman Akash Avatar answered Oct 17 '22 09:10

Maniruzzaman Akash


Why use Laravel Eloquent instead of the DB facade:

  1. You can write code that is object-oriented.

  2. It is easier to use than writing raw SQL or using the DB facade.

  3. There is no binding to the table schema, so for example if you want to change your table name, you don't have to touch a single Eloquent query, just change the table name in the Eloquent model.

  4. Relationships between tables can be maintained in an elegant way. Just mention the type of relationship (JOIN, LEFT JOIN, RIGHT JOIN etc.) to get data from related tables.

  5. Eloquent queries are more readable than raw SQL or the DB facade.

  6. You can use methods, scopes, accessors, modifiers etc. inside of a model, which is a maintainable pattern.

like image 41
Imran Avatar answered Oct 17 '22 08:10

Imran


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

Eloquent ORM average response times:

Joins Average (ms)
1 162.2
3 1002.7
4 1540.0

Raw SQL average response times:

Joins Average (ms)
1 116.4
3 130.6
4 155.2

Article Reference

like image 29
Mohammad Naji Avatar answered Oct 17 '22 08:10

Mohammad Naji