I have a laravel application but a query needs a lot of time to be executed.
I tried to build a raw query but with no success.
The query is this one:
$utente = Utente::with('coll')->with('collaboratori')
->where('id', '<>', '0')
->whereHas('coll', function ($query) use($id_utente, $att_dummy){
$query->where('collaboratori_id', 'like', $id_utente);})
->orderBy('created_at')
->get();
I also tried to look over the sql query with the queryLog and there are different query that laravel executed:
select * from `utenti_nuovo` where `id` <> 0 and exists (select * from `collaboratori_utenti` where `utenti_nuovo`.`id` = `collaboratori_utenti`.`id_utente`
and `collaboratori_id` like 100008) order by `created_at` asc
select * from `collaboratori_utenti` where `collaboratori_utenti`.`id_utente` in (718, 834, 844, 848, 875, 890, 894, 895, 897, 898)
select * from `collaboratori` where `collaboratori`.`id` in (12,13,16)
select `collaboratori`.*, `collaboratori_utenti`.`id_utente` as `pivot_id_utente`, `collaboratori_utenti`.`collaboratori_id` as `pivot_collaboratori_id`,
`collaboratori_utenti`.`attivita` as `pivot_attivita`, `collaboratori_utenti`.`created_at` as `pivot_created_at`, `collaboratori_utenti`.`updated_at` as `pivot_updated_at`
from `collaboratori` inner join `collaboratori_utenti` on `collaboratori`.`id` = `collaboratori_utenti`.`collaboratori_id`
where `collaboratori_utenti`.`id_utente` in (718, 834, 844, 848, 875, 890, 894, 895, 897, 898)
The first of those query is the slow one that causes a long time to wait.
Is there a way to change the EXIST with a faster query?