我正在使用 Laravel 5.8 開發在線電子學習網站,我需要運行查詢以更新已參加考試的用戶的考試結果。
這是更新考試成績的控制器方法:
public function compute($oleId)
{
try {
ini_set('memory_limit', '-1');
set_time_limit(0);
DB::beginTransaction();
/* Get the Exam */
$exam = OlympiadExam::query()->where('ole_id', $oleId)->first();
/* Calculate the score of Exam */
if ($exam->ole_is_main == '0') {
foreach ($exam->olympiadExamExecution as $execution) {
$questions = $execution->load('olympiadExamExecutionQuestion.olympiadExamQuestion');
$all = $exam->ole_question_count;
$notAnswered = $questions->olympiadExamExecutionQuestionNotAnswered->where('oee_oex_id', $execution->oex_id)->count();
$answered = $all - $notAnswered;
$truthy = $questions->olympiadExamExecutionQuestionTruthy->where('oee_oex_id', $execution->oex_id)->count();
$falsy = $all - ($truthy $notAnswered);
$score = (float)($truthy - ($falsy / 3));
$percentage = (float)((($truthy * 3) - $falsy) / ($all * 3)) * 100;
$prePositive = (float)$percentage * ($exam->ole_percent_effect / 100);
$percentPositive = ($prePositive > 0) ? $prePositive : 0;
$percentFinal = (($percentage $percentPositive) > 100) ? 100 : ($percentage $percentPositive);
$scoreFinal = ((($percentFinal * $all) / 100) > $all) ? $all : ($percentFinal * ($all / 100));
$examResult = [
'oex_correct_answer_count' => $truthy,
'oex_wrong_answer_count' => $falsy,
'oex_no_answer_count' => $notAnswered,
'oex_score' => $score,
'oex_percent' => $percentage,
'oex_percent_positive' => $percentPositive,
'oex_percent_final' => $percentFinal,
'oex_score_final' => $scoreFinal,
];
OlympiadExamExecution::query()->where('oex_id', $execution->oex_id)->update($examResult);
}
}
$candidates = OlympiadExamExecution::query()->where('oex_ole_id', $oleId)->pluck('oex_percent_final')->toArray();
if (!empty($candidates)) {
$this->computeRank($candidates);
}
DB::commit();
session()->flash('computeStatus', 'Process for calculating score completed');
return redirect()->back();
} catch (\Exception $exception) {
DB::rollBack();
session()->flash('computeStatus', 'Process for calculating score is going wrong');
return redirect()->back();
}
}
現在,這種方法適用于少數參加過考試的用戶,但不適用于大量用戶(大約 500 個用戶)。
因此,我嘗試在查詢運行之前進行設定ini_set('memory_limit', '-1');,set_time_limit(0);但仍然沒有鍛煉并顯示此訊息:

所以我想知道導致這個錯誤的原因是什么?
如何正確地使此處理適用于大量用戶?
I would REALLY appreciate any idea or suggestion from you guys because my life depends on this...
uj5u.com熱心網友回復:
laravel 中有一個 chunk 方法用于對大資料進行排隊。您可以分塊資料并嘗試匯入資料這是供參考的鏈接:here
我希望這個鏈接對你有幫助。這是檔案中有關它的內容。
如果您需要處理數千條資料庫記錄,請考慮使用 DB 門面提供的 chunk 方法。此方法一次檢索一小塊結果,并將每個塊送入閉包進行處理。例如,讓我們一次檢索 100 條記錄的整個用戶表:
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417676.html
標籤:
