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
Post a Comment