php - Laravel: How to speed up foreach database update -


i got cron job want run every month, assigns random serial number each active customer.

but takes long time run, 2000+ active customers , each customer gets 10 random serials, takes 20k+ update queries finish job.

how speed up?

foreach($subscriptions $subscription){     $updated_at = carbon::now()->todatetimestring();     foreach($games $game){         db::table('serials')             ->wherenull('user_id')             ->where('game_id', $game->game_id)             ->limit(1)             ->update([                 'user_id'    => $subscription->user_id,                 'updated_at' => $updated_at             ]);     } } 

you can try :

foreach($subscriptions $subscription){     $updated_at = carbon::now()->todatetimestring();     foreach($games $game){         db::table('serials')             ->wherenull('user_id')             ->where('game_id', $game->game_id)             ->limit(1)             ->updateveryfast([                 'user_id'    => $subscription->user_id,                 'updated_at' => $updated_at             ]);     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -