What is the best way to update one field with rows resulting 15k?

Published 2 years ago by meetgodhani

Hi

What is the most optimized way to update mass rows which are more than 15k in number without getting time out ? Using Laravel.

Any suggestion.

_stefanzweifel

You can use chunk() for that. Quick example from the docs (http://laravel.com/docs/5.1/eloquent#retrieving-multiple-models):

User::chunk(200, function($users)
{
    foreach ($users as $user)
    {
        // Execute the row update here
    }
});

Or if all 15k rows should update with the same value:

Model::whereIn('id', $arrayOfIds)->update(['field' => $value]);
jekinney
jekinney
2 years ago (212,765 XP)

Yes, chunk and a queued command or event. Chunking is very efficient as the time to execute isn't significantly longer and doesn't use up your servers memory.

Please sign in or create an account to participate in this conversation.