php – 在MySQL中创建查找表的最快方法

我想为扑克手的组合创建一个查找表. 7卡板中有1.13亿种不同的手部组合.

如果我给每张卡片说一个数字(1-52)并希望将每个可能的组合存储在表格中,那么最好的方法是什么呢?我希望它快速查找,所以如果我有一只手13,18,1,51,38,8,49,我可以搜索表中的行.

我可以将每张卡存储在它自己的列中,如下所示:

poker_hands (id, card1, card2, card3, card4, card5, card6, card7)

或者我可以为7张牌创建某种哈希值,例如:

$string= md5($card1 . $card2 . $card3 . $card4 . $card5 . $card6. $card7);

然后用它来查找手

poker_hands (id, hash) 

(我将在数据库中存储有关每只手的等级的信息;但是现在我只想知道创建查找表的最佳方法.)

解决方法:

首先创建一个从1到52的数字列表.您可以这样做:

create table numbers as
    select 1 as n union all select 2 union all . . .;

或者,从已存在的表中:

create table numbers as
    select (@rn := @rn + 1) as n
    from t
    limit 52;

然后,使用交叉连接创建组合:

create table hands as
    select n1.n as card1, n2.n as card2, n3.n as card3, n4.n as card4,
           n5.n as card5, n6.n as card6, n7.n as card7
    from numbers n1 cross join
         numbers n2 cross join
         numbers n3 cross join
         numbers n4 cross join
         numbers n5 cross join
         numbers n6 cross join
         numbers n7;

这可能是数据库中最快的方法.

编辑:

如果卡片应该不同,那么放入适当的条件:

create table hands as
    select n1.n as card1, n2.n as card2, n3.n as card3, n4.n as card4,
           n5.n as card5, n6.n as card6, n7.n as card7
    from numbers n1 join
         numbers n2
         on n2.n not in (n1.n) join
         numbers n3
         on n3.n not in (n1.n, n2.n) join
         numbers n4
         on n4.n not in (n1.n, n2.n, n3.n) join
         numbers n5
         on n5.n not in (n1.n, n2.n, n3.n, n4.n) join
         numbers n6
         on n6.n not in (n1.n, n2.n, n3.n, n4.n, n5.n) join
         numbers n7
         on n7.n not in (n1.n, n2.n, n3.n, n4.n, n5.n, n6.n);
上一篇:c# – 保存相关常量的最佳实践?


下一篇:c# – 缓存查找性能