Laravel Eloquent ORM vs Raw Queries | Laravel Performance Testing

preview_player
Показать описание
Today, I want to settle a debate around which method is faster, Using Laravel Eloquent ORM or running Raw Queries.

Both methods have their advantages. However, its important to realize that Eloquent is much slower than using Laravel Query builder or just running raw queries.

For us to determine which is better for performance, I have created this quick experiment that should demonstrate that using Eloquent can be 20x slower than running raw queries.

The main purpose of this experiment is to measure the execution time of running multiple queries using Laravel Eloquent and Raw Queriers.

I have posted all my code for this experiment on my github account. Feel free to download it and see the results for yourself.

Follow us
Рекомендации по теме
Комментарии
Автор

Its worth mentioning, in this example I used lazy-loading which is why Eloquent performed soo bad. We can improve it greatly if we eager-load the records instead. However, if we eager-load we can reduce the queries from n+1 to 2. The best Eloquent can perform is 2 queries. In this example, our raw method runs only 1 query. So even if you optimized your Eloquent query, it will still be slower.

emadzaamout
Автор

Great experiment, I didn't know it could perform that slower. Your tutorials are incredible.
Can you do a tutorial on MVCS?

yashuabaryosef
Автор

You give such incredible insights on laravel best practices.
Thank you.

kibetkiptanui
Автор

Very interesting video. I like your approach - comes across as more software engineering than web develpment - which is refreshing. Implementing the service layer, evaluating performance of alternative approaches. All very good.

If you take requests, I would be interested in a video on Laravel running in docker, using an existing, external PostgreSQL instance where the app cannot run migrations, and an external REDIS service.

I feel like all the Laravel videos are focussed on setting up the development environment, but I'm more interested in the production environment, such as in a SAAS context where there is only one DB instance shared among all pods, where that DB instance is which is PostgreSQL not MySQL, and where the REDIS service is also run external to the pod.

I'm thinking of how it could be scaled in a Kubernetes or OpenShift, environment, hence when I say pod, I refer to container :)

Great work - thanks for your excellent videos!!

garymarshall