I think script payload reduction will be cumbersome and not give you a satisfactory result. If you have such an opportunity, I would advise you to register which lines you have already processed, and the script to run the following lines x. If you can use cronjob, you can create mail, and let cronjob add messages to the queue every 5 minutes until all users are processed.
The easiest way is to store somewhere, the highest user id that you processed. I would not advise you to keep the number of users, because between parties the user can be added or deleted, as a result, users will not receive an email. But if you order by user ID (provided that you use an auto-incrementing column for the identifier!), You can be sure that each user will be processed.
So, your user request will look something like this:
SELECT * FROM users WHERE user_id > [highest_processed_user_id] ORDER BY user_id LIMIT 1000
Then process your loop and save the last user id:
if(!empty($users)) { $last_processed_id = null; foreach($users as $user) { $message = // Message creation magic queue_mail_to_send( /** parameters **/ ); $last_processed_id = $user->id; } // batch done! store processed user id $query = 'UPDATE mail_table SET last_processed_user_id = '. $last_processed_id; // please use parameterized statements here // execute the query }
And the next time you do it again, until all users receive the mail.
source share