2014-05-21 34 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