4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHPで大容量CSV書き出しを高速で処理する

Last updated at Posted at 2020-07-29

CSVエクスポートでLaravelが用意するStorageクラスでファイル作成してループして1行ずつ書き込みすると無茶苦茶重いというか時間がかかったのでもっと早く出来ないかと調べた結果をメモ。

参考サイト

【PHP】大量データをfputcsvで瞬時にCSVファイル出力する方法

環境

FW: Laravel 6.x
PHP: 7

どのぐらい早いか

条件

レコード数: 50,000件
テーブルデータサイズ : 5.4MB
※PCのスペックとかもあるだろうけど、開発環境はDocker for Mac使っています。

結果

実行時間1回目: 16.8 sec
実行時間2回目: 25.2 sec

サンプルコード

UserController.php
public function export() {
    $users = new App\User::all();

    $filePath = storage_path(sprintf('app/%s.csv', date('ymd_hisu')));

    $stream = fopen('php://temp', 'w');

    $header = $users->first()->getFillable();
    mb_convert_variables('SJIS-win', 'UTF-8', $header);
    fputcsv($stream, $header);

    foreach ($users as $user) {
        $line = $user->toArray();
        mb_convert_variables('SJIS-win', 'UTF-8', $line);
        fputcsv($stream, $line);
    }

    rewind($stream);
    $content = stream_get_contents($stream);
    file_put_contents($filePath, $content);
    fclose($stream);

    return response()
            ->download($filePath, sprintf('サンプル%s.csv', now()->format('YmdHis')))
            ->deleteFileAfterSend();
}
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?