【发布时间】:2012-07-27 13:56:02
【问题描述】:
我有这个下表:
Matches -> match_id, team_a_id , team_b_id, score
此表将记录两支球队(A 队和 B 队)之间的比赛。但是,有时 A 队担任东道主,有时 B 队担任东道主。因此,当我试图查找 a 队和 b 队之间的历史匹配时。我目前正在做的是
select * from matches where (team_a_id = 1 and team_b_id = 2) or (team_a_id = 2 and team_b_id = 1);
有没有更好的方法来处理这种情况?至于上面的查询,我是否可以包含组合 team_a_id 和 team_b_id 的索引?但即便如此,那么我在 AB OR BA 之间仍然有一个逻辑或条件。
或者, 我有另一个想法,那就是有另一个表让我们说历史
History -> team_hash, match_id
我手动构建 team_hash where hash(a,b) == hash(b,a)。但这会导致插入速度稍慢但读取速度更快。还是读起来真的更快?
【问题讨论】:
标签: mysql database database-design indexing two-columns