executeSQL("UPDATE records SET played = played + 1 WHERE player IN " + generateClause(all));
executeSQL("UPDATE records SET wins = wins + 1 WHERE player IN " + generateClause(r.getWinners()));
executeSQL("UPDATE records SET winrate = ((wins/played) * 1000) WHERE player IN " + generateClause(all));
基本上我试图在比赛后更新球员的记录。我的程序按预期工作,但它似乎需要3个查询,我想只做一个,如果可能的话,因为我已经发现较少的查询大大减少了应用程序的运行时间,即使对db的修改是相同的
我的查询如下...... 1)增加所有已经玩过的玩家的“玩”数 2)为赢得的球员增加一场胜利 3)计算刚刚参加比赛的所有球员的胜率
任何建议都将不胜感激
答案 0 :(得分:1)
您可以使用CASE语句来管理wins
递增播放器和WHERE语句以增加其他所有人:
executeSQL = "UPDATE records SET played = played + 1, wins = wins + CASE WHEN player in " + generateClause(r.getWinners) + " THEN 1 ELSE 0 END, winrate = ((wins/played) * 1000) WHERE player in " + generateClause(all))
答案 1 :(得分:0)
我认为generateClause(all)包含所有玩家,而另一个只包含获胜者,对吧?...
我建议:
UPDATE ... SET wins = wins + player IN( list of winners ),
played = played+1,
winrate = 1000*(wins + player IN( list of winners ))/(played+1)
WHERE player IN (list of players)
我刚刚检查过:IN似乎在这里得到了适当的优化。
但是,如果您使用更新版本的MySQL,请记住您可以创建表达式(即虚拟)列,并且可以将winrate作为虚拟列。这些也可以编入索引!
编辑:
“seq”包含1M行。
SELECT sum(id) FROM seq
SELECT sum(id IN (list of 1000 numbers)) FROM seq
执行时间的差异是0.3s,这意味着IN(1000个数字)需要0.3μs,这意味着MySQL必须使用某种形式的哈希来优化它。所以,不要担心重复你的IN()三次......它会很快。