Laravel: Be Careful with whereHas Performance in Eloquent

preview_player
Показать описание
Quick video, comparing two Laravel queries that perform almost the same task - querying by relationship. WhereHas() or join()?

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

More performance related topics will be appreciated

tahaelkholy
Автор

Yesterday evening i was working on a transaction report with nearly same logic with whereHas and thinking about the performance... I was very surprised seeing what you shared this morning. Almost like i have asked the question and answered. Thank you.

emrahisk
Автор

i subscribed you just for these short tip videos, so don't stop please

arifdevcoding
Автор

You can use sub query whereIn( categ_id, categ:select(id)->where( project, 1)))

martinlionel
Автор

Yoo can also add ON and WHERE chaining as a closure.

->join('table2', function($join) {
$join->on('table1.column', '=', 'table2.column)

->on('table2.column', '=', value');
});


And if wanted to remove as a Row, also add where clause on main query

Respect from Pakistan 🇵🇰

nabeelyousafpasha
Автор

I tried this on a 2 years old project and the results are awesome.
As great as always, thank you

raadkasem
Автор

just 3 - 4 minutes with amazing information, thank you

bagusk_awan
Автор

Wow, I have an API which takes 40 sec without cache and I did use whereHas to join 6 tables. I think I can reduce the query time so much with this technique. thank you a lot

arashnikbakht
Автор

I was about to use 2-level nested whereHas.
thanks for this explanation

fmedeiroz
Автор

Everyday I learn eloquent goodies harm perforamnce a lot. I hope Laravel team focuses more on improving eloquent performance than anything else

wildfireDZ
Автор

Interesting. I'll switch more to join in these case where performance us important. Otherwise I'll stick to where has, I think, as it results in code that is easier to read/understand

ndeblauw
Автор

after watching this, tried 3 different queries.

in the user model i add scope with getting users with role and i read some comments and the 3rd queries is better than join if you have pivot table like for this senario:

public function scopegetUserWithRole($query, $name = 'admin')
{
return $query->whereHas('roles', function ($query) use ($name) {
return $query->whereName($name);
});



return $query->select('roles.*')
->join('role_user', 'role_user.user_id', 'users.id')
->join('roles', 'roles.id', 'role_user.role_id')
->whereName($name)->get();

return $query->with([
'roles' => function($query) use ($name) {
return $query->whereName($name);
},
]);
}

jessdhoctor
Автор

Also used this yesterday.. Let's take the work laptop and measure those times. Super video!

beatnu
Автор

Thank you, for this performance related video. I always try to spend time on such topics.

hassamulhaq
Автор

Thanks for this video!! This video is very interesting. Many developers don't care about database query time and in this video we have a clear example of the time difference between two queries.

arthurarw
Автор

Thanks Man, I was eagerly waiting for this video. has and whereHas is much time consuming if we have lot of data.

QaziHamayun
Автор

I use whereHas only when I use paginate() or just call one object like find() or first() because it is more simple and readable, but I prefer join when making query for reporting because from performance join is better. CMIIW

raga_kun
Автор

This performance related videos are great. Thanks!

pahuana
Автор

Wow, this is so much good information. Never had such insights. Thank you for this.

codewithfongoh
Автор

I have a table with huge records. Too slow query 1min. Let me try this. Thanks for the tips.

devsun