laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.66k stars 11.04k forks source link

The `syncWithoutDetaching` method can cause excessive RAM consumption in large databases #53546

Closed MohammadZarifiyan closed 1 week ago

MohammadZarifiyan commented 1 week ago

Laravel Version

11.32

PHP Version

8.1

Database Driver & Version

MySQL 8.0.31

Description

Using syncWithoutDetaching will cause a shortage of RAM in the system.

Steps To Reproduce

First create post_user pivot table and add 10 million rows to it. The run the following code:

$user = \App\Models\User::first();
$post = \App\Models\Post::first();

$user->posts()->syncWithoutDetaching([
  $post->getKey() => ['status' => 'pending']
]);
marius-mcp commented 1 week ago

I would suggest to use mass update instead of this for this case. Bear in mind it will not trigger events like that. So in conclusion I don't think this is a bug/issue.