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?
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?
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.
Why Laravel Eloquent:
OOP
manner.query builder
.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
. JOIN, LEFT JOIN, RIGHT JOIN
etc.) needed in query anymore to get data of related tables.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:
So, finally it is cleared that - when we'll use Database Query and When we'll use Eloquent Query.
Edit - Real Life Example
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.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...
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
Joins | Average (ms)
------+-------------
1 | 162,2
3 | 1002,7
4 | 1540,0
Result of select operation average response time for Eloquent ORM
Joins | Average (ms)
------+-------------
1 | 116,4
3 | 130,6
4 | 155,2
Result of select operation average response time for Raw SQL
Article Reference
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.
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.