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

Published 3 years ago by meetgodhani


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.


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]);
3 years ago (213,815 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.