繁体   English   中英

MYSQL / PHP:比较一个表中的2个特定行的大集合(POKER-calculator)

[英]MYSQL/PHP: Comparing large sets of 2 specific rows in one table (POKER-calculator)

我在写在线扑克计算器只是为了好玩:)我尝试了纯PHP计算方法,比较两只手来计算每个可能的牌组的结果(C(5,48)= 1712304牌组),这大约需要12秒钟糟糕的one.com服务器:D如果我将其在线上进行公开发布,那当然太慢了。 因此,我尝试了一种新方法,即数据库,我将7张卡(手+副牌)的所有组合存储在数据库中。 因此,我有一个数据库,该数据库有超过1.3亿行的5gb数据库,其中有一个主键(卡座表示形式为二进制)以及这7张卡的points or rank

因此,可以说这些列称为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++;
}

所以问题是,有没有更快的方法? 还有一种直接的方法来做到这一点,并立即获得桌面赢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提示 ,这可能有助于在很大的表中进行相对较少的查找。 您可以使用show index from mytable找到索引的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM