我正在编写一个工匠控制台命令,它循环遍历表中的所有记录并重新生成该表上的字段。
该字段为hash
,并生成为特定字符串的md5()
。
最初我的代码看起来像这样:
// Get all recipes
$recipes = Recipe::all();
$hashProgress = $this->output->createProgressBar(count($recipes));
// Loop over each recipe and generate a new hash for it
foreach ($recipes as $recipe)
{
$hashString = '';
$hashString .= $recipe->field1;
$hashString .= $recipe->field2;
$hashString .= $recipe->field3;
$hashString .= $recipe->field4;
$hashString .= $recipe->field5;
$hashString .= $recipe->field6;
$hashString .= $recipe->field7;
$extras1Total = $recipe->extras1->sum('amount');
$hashString .= $recipe->extras1->reduce(function ($str, $item) use ($extras1Total) {
return $str . $item->name . ($extras1Total == 0 ? $item->amount : ($item->amount / $extras1Total * 100));
}, '');
$extras2Total = $recipe->extras2->sum('amount');
$hashString .= $recipe->extras2->reduce(function ($str, $item) use ($extras2Total) {
return $str . $item->name . ($extras2Total == 0 ? $item->amount : ($item->amount / $extras2Total * 100));
}, '');
$extras3Total = $recipe->extras3->sum('amount');
$hashString .= $recipe->extras3->reduce(function ($str, $item) use ($extras3Total) {
return $str . $item->name . ($extras3Total == 0 ? $item->amount : ($item->amount / $extras3Total * 100));
}, '');
$extras4Total = $recipe->extras4->sum('amount');
$hashString .= $recipe->extras4->reduce(function ($str, $item) use ($extras4Total) {
return $str . $item->name . ($extras4Total == 0 ? $item->amount : ($item->amount / $extras4Total * 100));
}, '');
$recipe->update([
'hash' => md5($hashString),
]);
$hashProgress->advance();
}
$hashProgress->finish();
$this->info(' Recipe hashes regenerated.');
在获得大约10,000条28,000条记录之后,它将因内存耗尽错误而死亡:
PHP致命错误:允许的内存大小为268435456字节耗尽(尝试分配4096字节)
我认为chunk
这可能会有所帮助:
// Get all recipes
$recipes = Recipe::all();
$hashProgress = $this->output->createProgressBar(count($recipes));
// Loop over each recipe and generate a new hash for it
foreach ($recipes->chunk(1000) as $chunk)
{
foreach ($chunk as $recipe)
{
$hashString = '';
$hashString .= $recipe->field1;
$hashString .= $recipe->field2;
$hashString .= $recipe->field3;
$hashString .= $recipe->field4;
$hashString .= $recipe->field5;
$hashString .= $recipe->field6;
$hashString .= $recipe->field7;
$extras1Total = $recipe->extras1->sum('amount');
$hashString .= $recipe->extras1->reduce(function ($str, $item) use ($extras1Total) {
return $str . $item->name . ($extras1Total == 0 ? $item->amount : ($item->amount / $extras1Total * 100));
}, '');
$extras2Total = $recipe->extras2->sum('amount');
$hashString .= $recipe->extras2->reduce(function ($str, $item) use ($extras2Total) {
return $str . $item->name . ($extras2Total == 0 ? $item->amount : ($item->amount / $extras2Total * 100));
}, '');
$extras3Total = $recipe->extras3->sum('amount');
$hashString .= $recipe->extras3->reduce(function ($str, $item) use ($extras3Total) {
return $str . $item->name . ($extras3Total == 0 ? $item->amount : ($item->amount / $extras3Total * 100));
}, '');
$extras4Total = $recipe->extras4->sum('amount');
$hashString .= $recipe->extras4->reduce(function ($str, $item) use ($extras4Total) {
return $str . $item->name . ($extras4Total == 0 ? $item->amount : ($item->amount / $extras4Total * 100));
}, '');
$recipe->update([
'hash' => md5($hashString),
]);
$hashProgress->advance();
}
}
$hashProgress->finish();
$this->info(' Recipe hashes regenerated.');
但我仍然收到内存耗尽错误。
如何在不增加内存限制的情况下遍历所有这些记录并实现我的目标?
答案 0 :(得分:5)
您“分块”的方式实际上比初始代码消耗更多内存。
您正在做的是立即获取所有记录 ,将其存储在$recipes
中,然后通过调用chunk()
on the resulted collection来分析结果。
相反,您需要在基础Recipe
模型的查询构建器上调用具有相同名称chunk()
的方法,并按块生成哈希块:
Recipe::chunk(1000, function ($recipies) {
// Hash generation logic here
});
通过这种方式,你可以省去一个巨大的$recipes
变量,我确信这是我们的瓶颈。根据可用内存的不同,您可能需要稍微调整块大小以避免内存耗尽。
此外,我会尝试在生成哈希时使用更少的变量,而不是留下$extras1Total
,extras2Total
,......变量的踪迹。所有这些都可以替换为$total
,它将被反复重写。这是微观优化。
P.S。如果数据库写入压力很大(总共28k很少见),您可能需要考虑在一个(或几个)go(es)中进行最终更新,而不是按记录执行。