I've got a common scenario here that I've yet to find out what the most effective way of going about this is.
Let's say I have a table of 100,000 users.
On the 5th of every month, I want to run a scheduler that will add an entry in the database for each user. This entry will be a message. So they essentially all receive a message on the 5th of every month.
At the moment, it could be done like this:
$user = \App\User::all();
foreach ($users as $user){
$message = new Message;
$message->user_id = $user->id;
$message->content = "Message goes here";
$message->save();
}
Essentially this creates 100k new entries in the database.
Of course this is just an example as it would be crazy to run a foreach on a 100,000 users.
I could perhaps utilise the insert
method, but I can't see how that would work with large amounts of data.
What is the most efficient way of solving this issue? I can't imagine that queues would be efficient either because then it would essentially create 100,000 items in the queue resulting in the same, or even worse, performance.