Are there any optimizations that can help speed up using Eloquent, or Fluent? I want to use it for an API, but it's very slow.
In my testing so far, I get roughly the following speeds:
230ms using PDO
300ms using RedBeanPHP
850ms using either DB::select(), or Eloquent.
I have tested without bootstraping Eloquent, and without Facades. The speeds are very similar. I understand it's going to be slower than a regular PDO query. I'm just trying to find some tricks or tips to help bring them closer.
There are a few things. First and foremost 850 ms response is crazy high but it gives you a base line. I personally during testing using eloquent on a live server have almost never gone over 300 ms.
Caching queries, queries that retrieve only the required data (I see a lot of eloquent queries retrieve every column), etc.
Yes eloquent and query builder add a little overhead but it shouldn't be that much depending. Seems something with the environment?
Make sure you check your logs and see if adding a few indexes here and there could help. I spent some time this weekend reviewing my slow query log and optimizing a few of my larger report queries (you might need to turn this on since it usually is not on by default).
After adding a few compound indexes on fields I was hitting a lot I ended up shaving tons of time from the queries. I also tweaked some caches. Now things are almost instantaneous.
It was the environment. The connection overhead I suppose. I was testing it installed locally, connecting to a remote database. The slowness was due to the PDO connection and subsequent queries it sets up with the character set and collation, etc.. it runs a few queries immediately after connect that added to the time.
@beerbuddha for me it became less important. I know that developing locally it'll be a lot slower due to the connection and additional queries it sets up, but it tested fine in the remote environment. The speeds there were between 40 - 80ms. I don't know of any additional tricks to help improve the Eloquent connection in this respect without removing the additional queries it makes for setting charset or collations. I've seen some optimizations that suggest not using facades and not booting eloquent will help some but in my experiments it wasn't as significant.