doutuoshou8915 2013-11-12 21:19
浏览 35

大数据集Cron工作建议

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

  • 写回答

1条回答 默认 最新

  • douxing9813 2013-11-12 21:37
    关注

    Based on absolutly no database information given ...

    UPDATE answer_tbl 
        LEFT JOIN (SELECT answer_tbl.id, IF(answer_tbl.answer = 'YES', COUNT(yes_tbl.id), COUNT(no_tbl.id)) AS score
                    FROM answer_tbl
                        LEFT JOIN answer_tbl AS yes_tbl ON answer_tbl.question_id = yes_tbl.question_id AND yes_tbl.answer = 'YES'
                        LEFT JOIN answer_tbl AS no_tbl ON answer_tbl.question_id = no_tbl.question_id AND no_tbl.answer = 'NO'
                    WHERE 1
                    GROUP BY answer_tbl.id) AS score_tbl
        SET answer_tbl.score AS score_tbl.score
        WHERE answer_tbl.id = score_tbl.score
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错