I have a function that should run once every week (cron job), and now I try to do a stress test.
In my request I get:
Maximum execution time of 60 seconds exceeded
protected function updateAnswerHistory(){
$answer_statistics = AnswerStatistic::select('question_id','paragraph_id','lkp_answer_id')->get(); //about 500row
$history = AnswerHistory::select('id', 'question_id','paragraph_id','lkp_answer_id','correct_answer_score')->get(); //about 40k rows
foreach ($history as $row) {
if($row->question_id){
$lkp_answer_id = $answer_statistics->where('question_id', $row->question_id)->pluck('lkp_answer_id')->first();
if($row->lkp_answer_id === $lkp_answer_id){
$row->update(['correct_answer_score' => 7]);
}else{
$row->update(['correct_answer_score' => 4]);
}
}
if($row->paragraph_id){
$lkp_answer_id = $answer_statistics->where('paragraph_id', $row->paragraph_id)->pluck('lkp_answer_id')->first();
if($row->lkp_answer_id === $lkp_answer_id){
$row->update(['correct_answer_score' => 7]);
}else{
$row->update(['correct_answer_score' => 4]);
}
}
}
}
One bad thing is that query from foreach which takes time, but I am not sure how can I improve this.