我有这张桌子用于 2-4 名玩家的纸牌游戏:
CREATE TABLE IF NOT EXISTS `cardgame` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`players` tinyint(1) unsigned NOT NULL DEFAULT '0',
`p1` mediumint(6) unsigned NOT NULL DEFAULT '0',
`p2` mediumint(6) unsigned NOT NULL DEFAULT '0',
`p3` mediumint(6) unsigned NOT NULL DEFAULT '0',
`p4` mediumint(6) unsigned NOT NULL DEFAULT '0',
`p1_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
`p2_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
`p3_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
`p4_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
/* other rows */,
`game_state` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=79218 DEFAULT CHARSET=utf8;
我必须检查所有比赛,我是一名球员。(在示例中我的 user_id 是 1981)
mysql> EXPLAIN SELECT id FROM cardgame
WHERE ((p1=1981 AND p1_state=1) OR (p2=1981 AND p2_state=1)
OR (p3=1981 AND p3_state=1) OR (p4=1981 AND p4_state=1))
AND game_state < 7;
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | cardgame | ALL | NULL | NULL | NULL | NULL | 79208 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
我可以在哪些行上创建索引?p1, p1_state, p2.. p3.. p4, p4_state 行需要索引加上 game_state,但不是太多了吗?
或者我必须以某种方式重新设计架构?