Good afternoon people, I'm doing a query in the bank where everything happens well, however when I make use of "groupby" this query gets very slow, very slow even, example, just take the groupby line that the query returns me in less than 1 second, with the groupby line the query takes about 25 seconds or more. :
Query code:
$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');