0

可以说,我有以下 mysql 表:

CREATE TABLE player (
    id int(9) unsigned NOT NULL DEFAULT 0 ,
    score MEDIUMINT(8) unsigned NOT NULL DEFAULT 0,
    signupdate DATE NOT NULL,
    lastupdate DATE NOT NULL
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;

目前我在 id 列上有一个主键。lastupdate栏每天都会更新,如果没有更新就说明玩家已经删除了账号,说明这个栏的基数很低。还有一个与 feilds 匹配的关系表matchidplayerid并且matchdate

我的大多数查询都像

SELECT id,score,signupdate FROM player 
JOIN matches ON matches.playerid = player.id 
WHERE lastupdate = '{today}'

所以我想到了3个索引案例

  1. id 上的主键
  2. id 上的 PRIMARY KEY 和 lastupdate 上的 INDEX
  3. 主键 (id,lastupdate)

哪一个会是最好的??

4

2 回答 2

1

您应该在 table column 上有一个索引,在 table columnmatches上有一个playerid索引。playerlastupdate

作为一个非常粗略的经验法则,如果它是一个大表,那么在WHEREandJOIN子句中使用的应该有一个索引。

要获取更多信息,您可以使用该explain语句。这是它的样子。注意最后的explain声明:

mysql> CREATE TABLE player (
    ->     id int(9) unsigned NOT NULL DEFAULT 0 ,
    ->     score MEDIUMINT(8) unsigned NOT NULL DEFAULT 0,
    ->     signupdate DATE NOT NULL,
    ->     lastupdate DATE NOT NULL
    -> ) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
Query OK, 0 rows affected (0.12 sec)

mysql> 
mysql> CREATE TABLE matches (
    ->     matchid int(9) unsigned NOT NULL DEFAULT 0 ,
    ->     playerid int(9) unsigned NOT NULL DEFAULT 0 ,
    ->     matchdate DATE NOT NULL
    -> ) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
Query OK, 0 rows affected (0.22 sec)

mysql> 
mysql> SELECT id,score,signupdate FROM player 
    -> JOIN matches ON matches.playerid = player.id 
    -> WHERE lastupdate = now()
    -> ;
Empty set (0.00 sec)

mysql> 
mysql> explain
    -> SELECT id,score,signupdate FROM player 
    -> JOIN matches ON matches.playerid = player.id 
    -> WHERE lastupdate = '{today}'
    -> ;
+----+-------------+---------+------+---------------+------+---------+------+------+--------------------------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra                          |
+----+-------------+---------+------+---------------+------+---------+------+------+--------------------------------+
|  1 | SIMPLE      | player  | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where                    |
|  1 | SIMPLE      | matches | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where; Using join buffer |
+----+-------------+---------+------+---------------+------+---------+------+------+--------------------------------+
2 rows in set, 2 warnings (0.00 sec)

mysql> CREATE INDEX player_idx_1 
    -> ON player (id)
    -> ;
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> CREATE INDEX matches_idx_1 
    -> ON matches (playerid)
    -> ;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> 
mysql> explain SELECT id,score,signupdate FROM player  JOIN matches ON matches.playerid = player.id  WHERE lastupdate = '{today}';
+----+-------------+---------+------+---------------+---------------+---------+-----------------+------+-------------+
| id | select_type | table   | type | possible_keys | key           | key_len | ref             | rows | Extra       |
+----+-------------+---------+------+---------------+---------------+---------+-----------------+------+-------------+
|  1 | SIMPLE      | player  | ALL  | player_idx_1  | NULL          | NULL    | NULL            |    1 | Using where |
|  1 | SIMPLE      | matches | ref  | matches_idx_1 | matches_idx_1 | 4       | mysql.player.id |    1 | Using index |
+----+-------------+---------+------+---------------+---------------+---------+-----------------+------+-------------+
2 rows in set, 2 warnings (0.00 sec)

mysql> 

添加索引lastupdate

mysql> CREATE INDEX player_idx_2 
    -> ON player (lastupdate)
    -> ;
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> explain
    -> SELECT id,score,signupdate FROM player 
    -> JOIN matches ON matches.playerid = player.id 
    -> WHERE lastupdate = curdate()
    -> ;
+----+-------------+---------+------+---------------+---------------+---------+-----------------+------+-------------+
| id | select_type | table   | type | possible_keys | key           | key_len | ref             | rows | Extra       |
+----+-------------+---------+------+---------------+---------------+---------+-----------------+------+-------------+
|  1 | SIMPLE      | player  | ref  | player_idx_2  | player_idx_2  | 3       | const           |    1 |             |
|  1 | SIMPLE      | matches | ref  | matches_idx_1 | matches_idx_1 | 4       | mysql.player.id |    1 | Using index |
+----+-------------+---------+------+---------------+---------------+---------+-----------------+------+-------------+
2 rows in set (0.00 sec)

mysql> 
于 2013-05-12T12:05:29.190 回答
0

绝对是第2个。主键用于唯一标识一行,id属性就足够了,所以你不需要选项3。而且由于你的大部分查询看起来像你说的那样,那么在lastupdate上有一个索引肯定会有助于加快您的查询。

于 2013-05-12T12:03:35.603 回答