LoginSignup
18
8

More than 3 years have passed since last update.

Insert処理一括ドーン!をするときに気をつけたいこと

Last updated at Posted at 2019-09-08

CSVファイルを基におおよそ1万レコードをDBにドーンとする処理を書いていたら、下記のエラーが。

SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders in XXX

あっ…(察し)
バインドしすぎってことね。

    // 1万分の配列が纏まっている$hogeDataをコレクション化
    $collection = collect($hogeData);
    // chunk()で1000個ごとに分割する
    $hogeData = $collection->chunk(1000);


    try {
        DB::beginTransaction();
        // 1000レコードを約10回インサート
        foreach ($hogeData as $value) {
            DB::table('hoges')->insert($value->toArray());
        }
        DB::commit();
        return redirect('/hogehoge', 302, [], true)->with('create-success', 'データ登録が完了しました。');
    } catch (\Throwable $e) {
        DB::rollBack();
        Log::error($e->getMessage());
        abort(500);
    }

こんな感じでコレクション化して1000個ずつ分割したらいけた。

※補足

あと、ドーンってするより適度に分けてやった方が勿論のことはやい。

18
8
1

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
18
8