Help us understand the problem. What is going on with this article?

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

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個ずつ分割したらいけた。

※補足

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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away