【问题标题】:Laravel Large CSV import using maatwebsite excelLaravel 使用 maatwebsite excel 导入大型 CSV
【发布时间】:2020-12-20 19:28:47
【问题描述】:

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

问题是每次在数据库中上传 10k-12k 数据,然后页面给出 HTTP ERROR 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

【问题讨论】:

  • 错误日志中有什么内容?
  • @Thomas local.ERROR: 允许的内存大小为 536870912 字节已用尽
  • 您是否添加了implements WithChunkReading?如果你减小 cunk 大小会发生什么?
  • 尝试使用作业来处理这个任务
  • 您是否尝试减小块大小?如果这没有帮助,我会尝试添加Batch inserts next(除了现有的 Chunk 阅读)。

标签: laravel laravel-7


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2020-08-28
    • 2021-10-08
    • 2020-03-10
    • 1970-01-01
    • 2020-07-09
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多