2014-05-21 32 views
0

我正在为即将到来的世界杯预测游戏。排名世界杯预测系统

这些都是我的表:

CREATE TABLE `users` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `first_name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '', 
    `last_name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '', 
    `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 

CREATE TABLE `matches` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `venue` varchar(100) COLLATE utf8_bin NOT NULL, 
    `stage` varchar(100) COLLATE utf8_bin NOT NULL, 
    `teamA` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '', 
    `teamB` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '', 
    `goalsAinit` int(11) DEFAULT NULL, 
    `goalsBinit` int(11) DEFAULT NULL, 
    `goalsAadded` int(11) DEFAULT NULL, 
    `goalsBadded` int(11) DEFAULT NULL, 
    `penaltiesA` int(11) DEFAULT NULL, 
    `penaltiesB` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 

CREATE TABLE `predictions` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `id_user` int(11) unsigned DEFAULT NULL, 
    `id_match` int(11) unsigned DEFAULT NULL, 
    `goalsAinit` int(11) DEFAULT NULL, 
    `goalsBinit` int(11) DEFAULT NULL, 
    `goalsAadded` int(11) DEFAULT NULL, 
    `goalsBadded` int(11) DEFAULT NULL, 
    `penaltiesA` int(11) DEFAULT NULL, 
    `penaltiesB` int(11) DEFAULT NULL, 
    `points` int(11) DEFAULT '60', 
    `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 

我已经有一个系统,用于获取点用户登录时系统:

for (var j in predictions) { 
    if (matches[i].id == predictions[j].id) { 
     if(predictions[j].score[0] != null && predictions[j].score[1]!= null){ 
      if((matches[i].score[0] != null && matches[i].score[1]!= null) && (
      (predictions[j].score[0] > predictions[j].score[1] && matches[i].score[0] > matches[i].score[1]) || 
      (predictions[j].score[0] < predictions[j].score[1] && matches[i].score[0] < matches[i].score[1]) || 
      (predictions[j].score[0] == predictions[j].score[1] && matches[i].score[0] == matches[i].score[1]) 
      )){ 
       //prediction correct, add points 
      }else{ 
       //prediction incorrect 
      } 
     } 
    } 
    break; 
} 

得分goalsAinit和goalsBinit数组

但我该如何为所有用户做出排名?它应该经常更新(虽然它不一定是实时的),但是我觉得每次用户都要执行第2个号码时,每次我想更新排名时,数据库都会发生爆炸...

有什么建议吗?由于

回答

0

我想出了这个查询来计算直接在mysql中的预测排名:

SELECT rank, id_user, first_name, last_name, iso, correct, points, total_predictions 
FROM (
    SELECT @rank:[email protected]+1 AS rank, id_user, first_name, last_name, iso, correct, points, total_predictions 
    FROM (
     SELECT p.id_user, u.first_name, u.last_name, iso, 
       SUM(IF((p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit) 
        OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit) 
        OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit), 1, 0)) AS correct, 
       SUM(IF((p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit) 
        OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit) 
        OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit), p.points, 0)) AS points, 
       COUNT(*) AS total_predictions 
     FROM predictions p 
     INNER JOIN matches_debug m ON m.id = p.id_match 
     LEFT JOIN users u ON p.id_user = u.id 
     LEFT JOIN flags f ON u.country = f.country 
     GROUP BY p.id_user 
     ORDER BY points DESC, correct DESC, total_predictions DESC, id_user 
    ) AS rankings, (SELECT @rank:=0) AS r 
) AS overall_rankings; 

请问这是安全与庞大的用户群来执行,以及?

+0

不知道你的想法是什么,以及你投入的资源,询问代码是否可以适应“大”用户基础太模糊。如果每天大于1000次点击并且服务器位于可扩展托管提供商上,则没有问题。如果大== 1,000,000次点击/秒,并且服务器是您的笔记本电脑,则该代码在运行之前可能会失败。一般来说,嵌套查询会影响性能问题,但取决于上下文的细节,这可能无关紧要。 – acrosman

+0

@acrosman感谢您的评论。那么更好的问题是,有没有办法优化这个查询? – Jan

+1

问好的优化问题真的很难。如果你要尝试,而不是用这个空间重新提出问题,我建议尝试一个新的问题。首先阅读关于优化查询的其他问题,并尝试一些你在那里看到的策略(同时查看哪些问题得到了有用的回答,哪些问题没有得到有用的回答)。一旦你尝试了一些东西并了解它们对性能的影响,你可能会发现你不需要再问了。 – acrosman