如何解决使用maatwebsite excel的Laravel大型CSV导入
我有一个包含3万行的CSV文件。我已使用maatwebsite excel将CSV导入我的pgsql数据库中。
问题是每次它在数据库中上传10k-12k数据,然后页面给出 HTTP错误500
错误:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
我在php.ini中的变量下面进行了更改
max_execution_time=0
max_input_time=3000
post_max_size=128M
我尝试了以下ReportsImport
中的代码
class UserReport implements ToModel,WithCustomCsvSettings,WithChunkReading,WithHeadingRow
{
public function model(array $row)
{
// dd($row);
return new UserReport([
'user' => $row['username],'amount' => $row['amount']
]);
}
public function getCsvSettings(): array
{
return [
'input_encoding' => 'UTF-8'
];
}
public function chunkSize(): int
{
return 1000;
}
}
如何解决这个HTTP 500 error
吗?
错误日志:local.ERROR: Allowed memory size of 536870912 bytes exhausted
版本:Laravel Framework 7.26.1
解决方法
问题是应用程序试图在内存中保存太多数据。我看到您已经在使用Chunk reading,但似乎还不够
要减少读取表数据时的内存消耗,请尝试减小块大小。
要减少模型的内存消耗,除了读取块外,还尝试添加Batch inserts。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。