如前所述,如果块是时间执行问题,那么在这种情况下,块并不能真正帮助您。我认为您尝试使用的大容量插入无法处理该数量的数据,因此我看到两个选项:
1-重新组织代码以正确使用块,如下所示:
$insert_data = [];
foreach ($json['value'] as $value) {
$posting_date = Carbon::parse($value['Posting_Date']);
$posting_date = $posting_date->format('Y-m-d');
$data = [
'item_no' => $value['Item_No'],
'entry_no' => $value['Entry_No'],
'document_no' => $value['Document_No'],
'posting_date' => $posting_date,
....
];
$insert_data[] = $data;
}
$insert_data = collect($insert_data); // Make a collection to use the chunk method
// it will chunk the dataset in smaller collections containing 500 values each.
// Play with the value to get best result
$chunks = $insert_data->chunk(500);
foreach ($chunks as $chunk)
{
\DB::table('items_details')->insert($chunk->toArray());
}
这样,您的大容量插入将包含较少的数据,并且能够以相当快的方式处理它。
2-如果您的主机支持运行时重载,您可以在代码开始执行之前添加一个指令:
ini_set('max_execution_time', 120 ) ; // time in seconds
$insert_data = [];
foreach ($json['value'] as $value)
{
...
}
要了解更多信息,请向官员咨询
docs