Laravel Rloquent Performance Problem

when I use Eloquent to retrieve data and find some performance issues

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

when i use ORM to get about 20 records from my db

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

it took about 24 MB of memory and 250 ms

but when i use query builder as below

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

database queries and returned data are almost the same, but the query builder used only 11 MB of memory and 113 ms to receive data.

when the required records occupy about 200 records and even relate to other tables via ORM, they take almost 8000 ms ... and often receive the "Allowed memory" error message.

So, I was interested, in my case, to use the query designer and join another table?

Or what should I do to speed up Eloquent?

+4
source share
2 answers

Finally, I found out where the problem is. In fact, this problem is not really caused by the Eloquent ORM, but by the point of view. This is what I did to fix the problem.

  • Lively loading: Since there are 4 relationships in my opinion, so this causes N + 4 requests, I use the download to fix it. and use

  • : () .

  • : , , , - S3.

.:)

+3

, , , :

DB::connection()->disableQueryLog()

: http://four.laravel.com/docs/cache,

, , :

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

All Articles