I have an interesting situation of calculations that need to be made in a project, and am looking for an efficient way to handle it. Here is the scenario.
We are making a "Polling" website, where users answer Poll questions. They can answer each question once.
We are generating a "Score" for every user based on their answers. They receive 1 point for each other user that answered the same.
For Example:
Question 1 has 2 answers, "Yes" and "No"
7 Users answered "Yes" and 3 answered "No"
Each User that answered "Yes" adds 7 points to their score
Each User that answered "No" adds 3 points to their score
If a 4th User answers "No", 1 extra point is added to each User that answered "No"
As you can imagine, it would be far too many calculations to do this on the fly, since lots of user scores must be regenerated every time a question is answered. So I want to do this as a Cron Job every X hours.
My Data currently returns a single Row for each Question answered by a single user, along with how many points each answer is worth (comma separated: 7,3)
How should I go about regenerating these results? I do not want to use a simple "Foreach" to loop through every User, as this doesn't seem like it will scale as the User base grows. Is there a way to run PHP scripts in the background or concurrently, as to not cause the loop to hang?
Any help or suggestions are greatly appreciated!
EDIT:
Sorry, I should have explained the database a bit too.
This is a WordPress website, so some of the data is in the default WordPress postmeta table. The tally is stored as comma separated meta_key value for the "Post" (Poll question)
All answers are store in their own answers table. Each answer is a row in the table, and it includes user_id, post_id (of poll question), answer chosen(index of comma separated meta_key value)
And this is the query I am using to get all the answers for a particular User:
SELECT * FROM `wp_myo_ip` LEFT JOIN `wp_postmeta` ON `wp_myo_ip`.`myo_polling_id` = `wp_postmeta`.`post_id` AND `wp_postmeta`.`meta_key` = 'myo-votes' WHERE `wp_myo_ip`.`myo_polling_ip` = 1
The myo_polling_ip
column is actually the User ID