简体   繁体   English

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

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

I am writing an online poker calculator just for fun :) I tried the pure php calculation approach, to compare two hands it calculates the outcome for every possible deck (C(5,48) = 1712304 decks) That takes around 12 seconds on my sucky one.com server :D What is ofcourse far too slow if i would ever put it online for public. 我在写在线扑克计算器只是为了好玩:)我尝试了纯PHP计算方法,比较两只手来计算每个可能的牌组的结果(C(5,48)= 1712304牌组),这大约需要12秒钟糟糕的one.com服务器:D如果我将其在线上进行公开发布,那当然太慢了。 So I tried a new approach, databases, I stored all the combinations for 7 cards (hand + deck) in a database. 因此,我尝试了一种新方法,即数据库,我将7张卡(手+副牌)的所有组合存储在数据库中。 So I have a database of 5gb over 130mil rows with a primary key (the deck representation in binary) and the points or rank of those 7 cards. 因此,我有一个数据库,该数据库有超过1.3亿行的5gb数据库,其中有一个主键(卡座表示形式为二进制)以及这7张卡的points or rank

So lets say the columns are called a and b , where a is the primary key. 因此,可以说这些列称为ab ,其中a是主键。

I now want/need to compare the b where ( a = x) and ( a = y) but again in the worst case for : C(5,48). 我现在想/需要比较b在( a = x)和( a = y)的情况下,但是在最坏的情况下,还是要比较C(5,48)。

So for example in badly written code : 因此,例如在编写不好的代码中:

$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++;
}

So the question is, is there a faster way ? 所以问题是,有没有更快的方法? Also is there a direct way to do this and get a table win win draw loss back immediately? 还有一种直接的方法来做到这一点,并立即获得桌面赢win draw loss

All help and answers are welcome!!! 欢迎所有帮助和答案!!! :) :)

EDIT: This approach clearly didnt work out very good haha :D it took around 100 seconds :D 编辑:这种方法显然无法解决非常好的哈哈:D花了大约100秒:D

Any other ideas are welcome ! 任何其他想法都欢迎!

One way that is worth trying is to let the database do most of the work. 值得尝试的一种方法是让数据库完成大部分工作。 Transfer your array to a temporary table with the primary keys of the matches to compare: 使用要比较的匹配项的主键将数组转移到临时表中:

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

Now you can query the bigger table for the win/loss/draw statistics: 现在,您可以查询更大的表格以获取赢/输/平局统计数据:

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

I've added the force index hint which might help for a relatively small number of lookups to a very large table. 我添加了force index提示 ,这可能有助于在很大的表中进行相对较少的查找。 You can find the name for an index using show index from mytable . 您可以使用show index from mytable找到索引的名称。

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

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