J
J
jazzus2019-03-15 15:41:00
Laravel
jazzus, 2019-03-15 15:41:00

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']);
                });

The code updates the rating after sorting the date. For all records, 1 field is updated - rank, put +1 in order. They write about ON DUPLICATE KEY on the net, but they have their own examples there that don’t work for me, and I don’t understand sql injections. And I would not want to, but apparently there is no other way in Laravel.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Orlov, 2019-03-15
@jazzus

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 question

Ask a Question

731 491 924 answers to any question