Poor whereHas performance in Laravel
I want to apply a where condition to relation. Here's what I do: Replay::whereHas('players', function ($query) { $query->where('battletag_name', 'test'); })->limit(100); It generates the following query: select * from `replays` where exists ( select * from `players` where `replays`.`id` = `players`.`replay_id` and `battletag_name` = 'test') order by `id` asc limit 100; Which executes in 70 seconds. If I manually rewrite query like this: select * from `replays` where id in ( select replay_id from `players` where `battletag_name` = 'test') order by `id` asc limit 100; It executes in 0.4 seconds.