Answer the question
In order to leave comments, you need to log in
How to bulk update rows without calling 200 thousand requests?
Code to convert
$projects=Project::orderBy('lift_at', 'desc')
->get()
->map(function ($item, $index) {
$item['rank'] = $index + 1;
return $item->update(['rank']);
});
Answer the question
In order to leave comments, you need to log in
https://laravel.com/docs/5.8/database
In your case, it's better not to update rank, but to use the correct sorting starting from date, because when updating rank, whatever you say, you will have to update all 200k records.
If, after all, you will update all records, then you need ON DUPLICATE KEY, and ordinary variables in SQL, here is an example:
UPDATE tbl, (select @cnt:=0) as cnt SET rank=(@cnt:[email protected]+1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question