gpt4 book ai didi

php - 如何正确运行需要大量处理的查询而不会出错

转载 作者:行者123 更新时间:2023-12-05 04:36:36 26 4
gpt4 key购买 nike

我正在使用 Laravel 5.8 开发在线电子学习网站,我需要运行查询以更新已参加考试的用户的考试结果

这是更新考试成绩的 Controller 方法:

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); 但仍然没有锻炼并显示此消息:

enter image description here

所以我想知道是什么问题导致了这个错误?

我怎样才能正确地使这个处理适用于大量用户?

我非常感谢你们的任何想法或建议,因为我的生活取决于此...

最佳答案

在laravel中有一个chunk方法用于对大数据进行排队。您可以分块数据并尝试导入数据这是供引用的链接:here

希望此链接对您有所帮助。以下是有关文档的内容。

If you need to work with thousands of database records, consider using the chunk method provided by the DB facade. This method retrieves a small chunk of results at a time and feeds each chunk into a closure for processing. For example, let's retrieve the entire users table in chunks of 100 records at a time:

关于php - 如何正确运行需要大量处理的查询而不会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70799201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com