Laravel CSV导入内存不足(允许内存耗尽)

时间:2018-09-27 12:58:47

标签: php laravel csv out-of-memory

我有一个成员的CSV文件,我每月收到一次,其中包含约6000行。

我正在(试图)遍历CSV文件,检查record表中是否已经存在members,如果是,则检查其是否相同。

然后将其插入pending表中(在适当的地方带有 exist 标志)。

我正在使用Laravel和League\CSV读取保存在storage文件夹中的文件:

class ImportController extends Controller
{
  public function import(Request $request) {

    $readDirectory = 'storage/csv/';
    $filename = $request->name;

    $stream = fopen($readDirectory.$filename, 'r');
    $reader = Reader::createFromStream($stream, 'r')->setHeaderOffset(0);
    $records = (new Statement())->process($reader);

    // Truncate the imported table prior to import
    Imported::truncate(); 

    foreach ($records as $record) {

        $email = $record['email'];

        $recordExists = $this->recordExists($email);

        if($recordExists) {
          // Compare the md5 of the recordArray and the memberArray and skip the record if thit's the same.
          $memberArray = $this->getmemberArray($recordExists);
          $recordArray = $this->getRecordArray($record);

          if($memberArray['hash'] === $recordArray['hash']) { continue; }

          $record['exists'] = TRUE;
          $this->write($record);

          continue;
        }


        else
        {
          $record['exists'] = FALSE;
          $this->write($record);
          Log::debug("missing: ".$record['URN']);

          continue;
        }
      };
    // End Foreach Loop

    return redirect()->route('upload.show');
  }



  public function recordExists($urn){
    $member = Member::where('email', 'LIKE', $email)->first();
    if ($member == null) { return false; }
    return $member;
  }

  public function getmemberArray($member) {
    $memberArray = [
      'email'       =>  $member->email,
      'first_name'  =>  $member->first_name,
      'last_name'   =>  $member->last_name,
      'age_years'   =>  $member->age_years,
      'gender'      =>  $member->gender,
      'address_1'   =>  $member->address_1,
      'address_2'   =>  $member->address_2,
      'address_3'   =>  $member->address_3,
      'town'        =>  $member->town,
      'county'      =>  $member->county,
      'postcode'    =>  $member->postcode,
      'sport_1'     =>  $member->sport_1,
      'sport_2'     =>  $member->sport_2,
    ];
    $memberArray['hash'] = md5(json_encode($memberArray));
    return $memberArray;
  }

  public function getRecordArray($record) {
    $recordArray = [
      'email'       =>  $record['email'], 
      'first_name'  =>  $record['first_name'], 
      'last_name'   =>  $record['last_name'], 
      'age_years'   =>  $record['age_years'], 
      'gender'      =>  $record['gender'],
      'address_1'   =>  $record['address_1'], 
      'address_2'   =>  $record['address_2'], 
      'address_3'   =>  $record['address_3'], 
      'town'        =>  $record['town'], 
      'county'      =>  $record['county'], 
      'postcode'    =>  $record['postcode'], 
      'sport_1'     =>  $record['sport_1'], 
      'sport_2'     =>  $record['sport_2'], 
    ];
    $recordArray['hash'] = md5(json_encode($recordArray));
    return $recordArray;
  }

  public function write($record) {

    $import = [];

    $import['email']      = $record['email'], 
    $import['first_name'] = $record['first_name'], 
    $import['last_name']  = $record['last_name'], 
    $import['age_years']  = $record['age_years'], 
    $import['gender']     = $record['gender'],
    $import['address_1']  = $record['address_1'], 
    $import['address_2']  = $record['address_2'], 
    $import['address_3']  = $record['address_3'], 
    $import['town']       = $record['town'], 
    $import['county']     = $record['county'], 
    $import['postcode']   = $record['postcode'], 
    $import['sport_1']    = $record['sport_1'], 
    $import['sport_2']    = $record['sport_2'], 
    $import['exists']     = $record['exists']

    DB::table('imported')->insert(
      $import
    );

    Log::debug($record['email']);

    return TRUE;
  }
}

但是我不断得到:

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Allowed memory size of 134217728 bytes exhausted (tried to allocate 181321056 bytes)

如果我在CSV中使用的行数少得多,则可以使用,但这不是一种选择。

我以前使用eloquent->save()写数据库,但是将其更改为DB::table()->insert以提高性能。

为了测试目的,我已经添加了以下内容,但仍无法解决。

set_time_limit(0);
ini_set('max_execution_time', 100000);
ini_set('memory_limit','512m');

我想念什么吗?某处发生某种内存泄漏?

我猜它每次都会将记录保存在内存中,所以有什么方法可以让每一行都忘记它吗?

也: 有没有办法清除此内存,以便我可以编辑代码并重试?

即使我停止并重新运行php artisan serve,它仍然保留相同的错误消息。

2 个答案:

答案 0 :(得分:3)

这里的问题是,idNumber在执行以下操作时会将整个CSV文件读入内存:

League\CSV

您应该像这样使用$records = (new Statement())->process($reader); 的{​​{1}}方法来一次只读取特定数量的行:

chunk

Reader方法返回一个Generator,您可以对其进行迭代。您可以找到提到的here in the documentation

编辑:我误读了文档,并建议使用错误的方法。

您基本上只需要遍历foreach($reader->chunk(50) as $row) { // do whatever } 本身:

chunk

如果您使用的是Mac,或者您的CSV是在Mac上创建的,则需要使用以下内容才能成功读取大型CSV文件:

$reader

请参阅文档的this part

答案 1 :(得分:-1)

我知道您正在使用php artisan serve运行服务器。您可以尝试部署某种形式的实际Web服务器,因为您将在生产环境中使用它。您可以尝试Apache,在Windows和Linux的XAMPP中很容易获得。

您可以在线检查如何在操作系统上安装Apache HTTP Server或Nginx。这些具有比php默认服务器更好的控制和使用内存。