Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent ORM Performance

I have just been testing the performance Eloquent ORM in Laravel and was shocked to find a simple query taking over 3 seconds to perform compared to the normal Laravel query which finished in 0.1 seconds. I'm only returning 1500 records.

DB::table('t_organisations')->get();  -  0.12253594398499 seconds
Organisation::all();   -   3.6389181613922 seconds

Surely this can't be normal!? I don't think I have missed anything in the setup. My db is normalized. What could be the problem?

like image 816
Craig Morgan Avatar asked Jul 09 '13 11:07

Craig Morgan


1 Answers

When you do DB::table('t_organisations')->get(); It fetches all results as an array (or objects) but does not hydrate them to the model. See this stackoverflow answer if you want a quick explanation.

When you do Organisation::all(); The hydration process takes place, which is why the request takes longer (you have to allocate all objects in the memory and fill them with the fields). There are many links/tuts on hydration optimization to help you better request your database, and avoid the hydration of objects when you don't need it.

like image 166
hilnius Avatar answered Nov 15 '22 09:11

hilnius