Groupby making query get very slow

Asked

Viewed 45 times

1

Good afternoon guys, I’m making a query in the bank where everything goes well, however when I use the "groupby" this query is very slow, very slow even, example, just take the line of the groupby that the query returns me in less than 1 second, with the groupby line the query takes about 25 seconds or more. :/

Code of consultation:

$query = DB::table('recmovto')
                ->join('receita', 'recmovto.receitaId', '=', 'receita.id')
                ->join('natureza_receita', 'receita.naturezaId', '=', 'natureza_receita.id')
                ->select('natureza_receita.natureza as a', 'natureza_receita.nome as b',
                        DB::raw("(Select valorAno from fonte_receita where receitaId = receita.id) as  c"),
                        DB::raw("(Select sum(valor) from recmovto where substr(data, 6, 2) = ".$request->valorMensal." and receitaId = receita.id) as d"),
                        DB::raw("(Select sum(valor) from recmovto where receitaId = receita.id and substr(data, 6, 2) between 01 and ".$request->valorMensal.") as e"))
                ->groupby('recmovto.receitaId')
                ->orderby('recmovto.data', 'desc');
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.