Tom
Tom

Reputation: 12988

Laravel - updateOrCreate with large number of records

I have potentially 500,000 records to either insert or update into my database.

I'm using the updateOrCreate function in Laravel but it's still taking a very long time.

I'm current using a foreach loop wrapped in a database transaction but is there a better solution?

DB::transaction(function() use ($items, $client) {
        foreach($items as $item) {

                $data = array(
                    'external_id' => $item->external_id,
                    'comment' => $item->comment,
                    'code_id' => $item->code_id,
                    'client_id' => $client->id
                );

                Registration::updateOrCreate(
                    [
                        'user_id' => $item->user_id,
                        'date' => Carbon::parse($item->date)->format('Y-m-d'),
                        'session' => $item->session
                    ],
                    $data
                );
        }
    });

Upvotes: 0

Views: 1376

Answers (2)

DevAtStack
DevAtStack

Reputation: 3

I think you should here use ShouldQueue approach of Laravel and instead of updateOrCreate method use Query builder to update single row using where('id',$id) this will ma

Upvotes: 0

dz0nika
dz0nika

Reputation: 1021

Well since you have so many records its inevitable for it to take a long time, my suggestion is to chunk the data you are getting like so

foreach($items->chunk(1000) as $chunk) {
   foreach($chunk as $item) {
      ...
   }
}

The above method will go over 1000 (or as many as you want) items at a time, and should theoretically decrease the load time by a bit. But still I really don't think you can make it a lot faster.

Upvotes: 0

Related Questions