4

when I use Eloquent to get data, and found out some performance issue

In my case, I use laravel debugbar ( https://github.com/barryvdh/laravel-debugbar ) to collect the information I need.

when I use ORM to get about 20 entries from my DB

$projects = Project::where('status', '=', 2)->get();

it took about 24MB memory usage and 250ms

but when I use the query builder as below

$projects = DB::table('Project')->where('status','=',2)->get();

the database queries and the return data are almost the same, but query builder took only 11MB memory usage and 113ms to get the data.

when the entries I need are about 200 entries, and even relate to other tables via ORM, it take almost 8000ms... and get the "Allowed memory size exhausted" error message very often..

So, I was wondering, in my condition, should I use the query builder and join other table?

Or what should I do to speed up the Eloquent performance ?

2 Answers 2

3

Finally I found out where the problem is. Actually, that issue is not really caused by Eloquent ORM but the view. That are what I did for fixed the problem

  • Eager loading: Since there are 4 relations in my view, so it cause N + 4 queries , I use the eager loading to fixed it. and use

  • Cache: I use remember() to cache results.

  • Cancel the Image checking: last things I did but most important, is to cancel the checking the Images in S3.

The page loading get a huge speed up. :)

Sign up to request clarification or add additional context in comments.

Comments

2

Just an idea, you probably need to disable query logging:

DB::connection()->disableQueryLog()

and use caching: http://four.laravel.com/docs/cache ,

if no significant difference then yes, you pobably just need to use query builder or even the basic:

DB::select(//your query here...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.