1

I have written a method to update a json string in my database with translations. Some categories have ~300.000 products that need to be updated.

This is my method:

public function generateDutchAttributes(Category $category)
    {
        $category->products()->chunk(100, function ($products) {
            foreach ($products as $product) {
                $attributesArray = [];
                foreach ($product->attributes as $attribute) {
                    $attributesArray[Translation::translateNL($attribute['attribute'])] = Translation::translateNL($attribute['value']);
                }
                //dd($attributesArray);
                $product->update(['dutch_attributes' => $attributesArray]);
            }
        });
        return redirect()->route('backend.import');
    }
}

After ~10.000 rows, I get a HTTP 500 error because too much memory was used.

Mwisho
  • 39
  • 9
  • Do you know where the memory usage comes from? Is it from the chunks being kept in memory or something else? If I recall correctly Laravel keeps a log of all queries, which can grow quite big with a lot of database interaction. Edit: I can't find any recent source, but for [Laravel 5.0 this does apply](https://laravel.com/docs/5.0/database#query-logging). – PtrTon Jul 28 '19 at 16:42
  • I think query logging is disabled by default indeed. I use laravel 5.8. – Mwisho Jul 28 '19 at 17:06
  • https://stackoverflow.com/q/45464676/2693543 try checking this question and its answers – Shobi Jul 28 '19 at 18:25

0 Answers0