我正在编写一个在线扑克计算器只是为了好玩:)我尝试了纯 php 计算方法,为了比较两手牌,它会计算每个可能的套牌的结果(C(5,48) = 1712304 副牌)这在我糟糕的 one.com 服务器上大约需要 12 秒:D如果我把它放到网上供公众使用,那当然太慢了。所以我尝试了一种新方法,数据库,我将 7 张牌(手牌 + 套牌)的所有组合存储在数据库中。所以我有一个超过 130mil 行的 5gb 数据库,其中包含一个主键(二进制的甲板表示)和这 7 张卡的points or rank
。
因此,假设这些列称为a
和b
,其中a
是主键。
我现在想要/需要比较 ( a
= x) 和 ( a
= y) 的b
但在最坏的情况下: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
查找索引的名称。