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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With