Javascript 世界杯预测系统排名
我正在为即将到来的世界杯做预测赛 这些是我的桌子:Javascript 世界杯预测系统排名,javascript,php,mysql,Javascript,Php,Mysql,我正在为即将到来的世界杯做预测赛 这些是我的桌子: 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` timest
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,数据库将爆炸
有什么建议吗?谢谢我提出了这个查询,直接在mysql中计算预测的排名:
SELECT rank, id_user, first_name, last_name, iso, correct, points, total_predictions
FROM (
SELECT @rank:=@rank+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;
在不知道您认为什么是大的,以及您投入的资源的情况下,询问代码是否能够容纳一个大的用户群也安全吗???。如果large==1000次点击/天,并且服务器位于可扩展的主机提供商上,则没有问题。如果large==1000000次点击/秒,并且服务器是您的笔记本电脑,则在该代码运行之前,站点可能会失败。一般来说,嵌套查询可能会出现性能问题,但这取决于上下文的详细信息,这可能无关紧要。@acrosman感谢您的评论。那么一个更好的问题是,有没有办法优化这个查询?提出好的优化问题真的很难。如果你想尝试,我建议你尝试一个新的问题,而不是用这个空间来重新提问。从阅读关于优化查询的其他问题开始,尝试一些你在那里看到的策略,也看看哪些问题得到了有用的回答,哪些没有。一旦您尝试了一些方法并了解了它们对性能的影响,您可能会发现不需要再问了。