Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DavidBuchukuri's avatar

Why are eloquent queries much slower than Db facade queries?

I was playing around with db facade and eloquent queries. I have installed clockwork extension, which basically shows sql queries which are ran, time that a query takes to finish execution, and a response time. I generated 10 000 records of posts for testing. I have one to many relationship defined on a User and a Post models.

When I do an eloquent query

User::with('posts')->find(1);

It generates 2 sql queries, without a join image

also queries take 36ms to execute, but response time is much longer - 661ms

When I do a query with a DB facade

	DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')->where('users.id', 1)->get();

there is only 1 sql query generated, with a join operation image

that 1 query takes a bit more time, because there is a join operation, but response time is much much faster. It's only 100ms

What i think is happening is that, In case of eloquent, join doesn't happen on database level, which we can see from the generated queries. Isn't that a little weird? databases are designed and optimized, to make operations like that and have lots of mechanisms to make joins faster, like query optimizers , indexes and so on. Why does eloquent do that on laravel's side. Isn't that pretty bad for performance? As we can see from results, eloquent's response time is much longer. Am I misunderstanding something?

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

What i think is happening is that, In case of eloquent, join doesn't happen on database level

Correct, because with eager-loaded Eloquent relation(s), you get fully hydrated Post instances associated with the User.

Isn't that pretty bad for performance?

Ultimately, this is a decision for you to make - do you need Post instances which can have their own behaviours, or do you simply need data. from the posts table?

1 like

Please or to participate in this conversation.