Laravel 使用自定义网站导入大型 CSV excel

Laravel Large CSV import using maatwebsite excel

我有一个包含 3 万行的 CSV 文件。我已经使用 maatwebsite excel 在我的 pgsql 数据库中导入 CSV。

问题是每次它在数据库中上传 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 框架 7.26.1

问题是应用程序试图在内存中保存太多数据。我看到您已经在使用 Chunk reading,但似乎还不够

要减少读取 table 数据时的内存消耗,请尝试减小块大小。

要减少模型的内存消耗,请尝试在块读取之外添加 Batch inserts