我有10,000个物品。
如果我import functools
options = {
...
'2': functools.partial(fn, arg1, arg2, arg3)
...
}
未使用sum()
,请执行以下操作:
get()
需要6秒钟。
当$total_salary = Employee::where('year','=', '2017')
->sum('total_salary');
$total_bonus = Employee::where('year','=', '2017')
->sum('total_bonus');
return ['total_salary'=>$total_salary, 'total_bonus'=>$total_bonus];
与sum()
这样时:
get()
需要20秒。
如何在不等待这么长时间的情况下将$query = Employee::where('year','=', '2017')
->get();
$data['total_salary'] = $query->sum('total_salary');
$data['total_bonus'] = $query->sum('total_bonus');
return $data;
与sum()
一起使用?
答案 0 :(得分:1)
编辑:抱歉,我误解了你的问题。
当你在get
函数后调用sum时,你正在与Illuminate\Support\Collection
求和,这就是为什么它需要这么多。
让我们使用数据库加总:
$result = Employee::where('year','=', '2017')
->select(
\DB::raw('sum(total_salary) as total_salary'),
\DB::raw('sum(total_bonus) as total_bonus')
)
->get();
echo $result->total_salary;
echo $result->total_bonus;