php - MySQL/PHP : Comparing large sets of 2 specific rows in one table (POKER-calculator)

标签 php mysql sql calculator poker

我正在写一个在线扑克计算器只是为了好玩:) 我尝试了纯 php 计算方法,为了比较两只手,它计算了每副可能牌组的结果(C(5,48) = 1712304 牌组) 在我糟糕的 one.com 服务器上,这需要大约 12 秒 :D 如果我将它放在网上公开,那当然太慢了。 所以我尝试了一种新方法,数据库,我将 7 张牌(手牌 + 牌组)的所有组合存储在数据库中。所以我有一个超过 1.3 亿行的 5gb 数据库,其中包含一个主键(二进制表示的牌组)和这 7 张牌的点数或排名

假设列名为 ab,其中 a 是主键。

我现在想要/需要比较 b 其中 (a = x) 和 (a = y) 但同样在最坏的情况下:C(5,48)。

例如在写得不好的代码中:

$ar = array(array(1,4),array(53,422),array(4423423,472323),array(71313,13131));

for ($i = 0; $i < count($ar);$i++)
{
    $value_one = mysql_fetch_assoc(mysql_query('select `b` from `hugetable` where (`a` = ' . $ar[$i][0] ' . LIMIT 1;'))['b'];
    $value_two = mysql_fetch_assoc(mysql_query('select `b` from `hugetable` where (`a` = ' . $ar[$i][1] ' . LIMIT 1;'))['b'];
    if ($value_one > $value_two)
        $win++;
    elseif ($value_one < $value_two)
        $lose++;
    else
        $draw++;
}

那么问题来了,有没有更快的方法呢? 还有一种直接的方法可以做到这一点并立即获得一张 table win win draw loss 吗?

欢迎所有的帮助和回答!!! :)

编辑: 这种方法显然效果不佳哈哈:D 花了大约 100 秒 :D

欢迎任何其他想法!

最佳答案

一种值得尝试的方法是让数据库完成大部分工作。将您的数组转移到一个临时表,其中包含要比较的匹配项的主键:

create temporary table match_list (int pk1, int pk2);

现在您可以查询更大的表以获取赢/输/平局统计信息:

select  sum(case when t1.score > t2.score then 1 end) as wins
,       sum(case when t1.score < t2.score then 1 end) as losses
,       sum(case when t1.score = t2.score then 1 end) as draws
from    match_list
join    match_results t1 force index (pk_match_results)
on      t1.pk = match_list.pk1
join    match_results t2 force index (pk_match_results)
on      t2.pk = match_list.pk2

我添加了 force index hint这可能有助于对非常大的表进行相对少量的查找。您可以使用 show index from mytable 找到索引的名称。

关于php - MySQL/PHP : Comparing large sets of 2 specific rows in one table (POKER-calculator),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28260747/

相关文章:

php - 如何为 WordPress 插件的选项设置默认值?

php - 使用带有 readline 的 PHP 读取用户命令行输入,但 bash 不是默认 shell

mysql - 我应该在 MySQL InnoDB 表的 id 列上应用 PRIMARY 和 UNIQUE 索引吗?

sql - 如何保证统计表的连续性?

c# - ASP.NET C# 如何从数据库填充表

php - 返回表的一行 与另一个表的左连接具有具有相同 ID 的多行

php - 如何将sharepoint与php连接

php - 自动执行 MySQL 查询以创建列表

mysql - Node.js 中的目标表中的 INNER JOIN 未更新

sql - 如何查看 Oracle DB 中的 View 何时被修改