如何正确 运行 需要大量处理的查询而不会出错
How to properly run a query that needs a lot of Processing without Getting Error
我正在 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();
}
}
现在此方法适用于一些参加过考试的用户,但不适用于大量用户[=35] =](约 500 名用户)。
因此我尝试在查询 运行s 之前设置 ini_set('memory_limit', '-1');
和 set_time_limit(0);
但仍然没有锻炼并显示此消息:
所以我想知道是什么问题导致了这个错误?
我怎样才能正确地使这个处理适用于大量用户?
我非常感谢你们的任何想法或建议因为我的生活取决于此...
laravel中有一个chunk方法用于大数据的排队。
您可以分块数据并尝试导入数据
这里是link供参考:here
希望这篇link对您有所帮助。
这是文档中关于它的内容。
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:
我正在 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();
}
}
现在此方法适用于一些参加过考试的用户,但不适用于大量用户[=35] =](约 500 名用户)。
因此我尝试在查询 运行s 之前设置 ini_set('memory_limit', '-1');
和 set_time_limit(0);
但仍然没有锻炼并显示此消息:
所以我想知道是什么问题导致了这个错误?
我怎样才能正确地使这个处理适用于大量用户?
我非常感谢你们的任何想法或建议因为我的生活取决于此...
laravel中有一个chunk方法用于大数据的排队。 您可以分块数据并尝试导入数据 这里是link供参考:here
希望这篇link对您有所帮助。 这是文档中关于它的内容。
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: