为什么解释说竞赛表使用 ALL 类型,尽管竞赛表有 chid 作为主键?

发布于 2024-08-23 14:46:33 字数 2976 浏览 2 评论 0原文

为什么解释说竞赛表使用 ALL 类型,尽管竞赛表有 chid 作为主键?


mysql> explain SELECT contests.chid, contests.diff_level from contests, contest_users where contests.chid=contest_users.chid;          


+----+-------------+-----------------+------+---------------------+---------------------+---------+-------------------------------+-------+-------------+
| id | select_type | table           | type | possible_keys       | key                 | key_len | ref                           | rows  | Extra       |
+----+-------------+-----------------+------+---------------------+---------------------+---------+-------------------------------+-------+-------------+
|  1 | SIMPLE      | contests        | ALL  | PRIMARY             | NULL                | NULL    | NULL                          | 37660 |             | 
|  1 | SIMPLE      | contest_users   | ref  | contest_users_idx   | contest_users_idx   | 4       | fbtable.contests.chid         |     2 | Using index | 
+----+-------------+-----------------+------+---------------------+---------------------+---------+-------------------------------+-------+-------------+
2 rows in set (0.00 sec)

mysql> desc contests;
  +------------+------------------+------+-----+----------+----------------+
  | Field      | Type             | Null | Key | Default  | Extra          |
  +------------+------------------+------+-----+----------+----------------+
  | chid       | int(10) unsigned | NO   | PRI | NULL     | auto_increment | 
  | puzzle     | char(81)         | YES  |     | NULL     |                | 
  | solution   | char(81)         | YES  |     | NULL     |                | 
  | isComplete | tinyint(1)       | YES  |     | 0        |                | 
  | diff_level | char(7)          | YES  |     | NULL     |                | 
  | time       | time             | YES  |     | 00:00:00 |                | 
  +------------+------------------+------+-----+----------+----------------+
  6 rows in set (0.00 sec)

mysql> desc contest_users;
  +------------+----------------------+------+-----+---------+-------+
  | Field      | Type                 | Null | Key | Default | Extra |
  +------------+----------------------+------+-----+---------+-------+
  | chid       | int(10) unsigned     | NO   | MUL | NULL    |       | 
  | uid        | bigint(20) unsigned  | YES  |     | NULL    |       | 
  | gamestate  | char(81)             | YES  |     | NULL    |       | 
  | score      | int(10) unsigned     | YES  |     | 0       |       | 
  | no_correct | smallint(5) unsigned | YES  |     | 0       |       | 
  | no_wrong   | smallint(5) unsigned | YES  |     | 0       |       | 
  | time       | time                 | YES  |     | NULL    |       | 
  | isComplete | tinyint(1)           | YES  |     | 0       |       | 
  +------------+----------------------+------+-----+---------+-------+
  8 rows in set (0.00 sec)

Why does explain say that it uses type ALL for contests table though contest table has chid as primary key?


mysql> explain SELECT contests.chid, contests.diff_level from contests, contest_users where contests.chid=contest_users.chid;          


+----+-------------+-----------------+------+---------------------+---------------------+---------+-------------------------------+-------+-------------+
| id | select_type | table           | type | possible_keys       | key                 | key_len | ref                           | rows  | Extra       |
+----+-------------+-----------------+------+---------------------+---------------------+---------+-------------------------------+-------+-------------+
|  1 | SIMPLE      | contests        | ALL  | PRIMARY             | NULL                | NULL    | NULL                          | 37660 |             | 
|  1 | SIMPLE      | contest_users   | ref  | contest_users_idx   | contest_users_idx   | 4       | fbtable.contests.chid         |     2 | Using index | 
+----+-------------+-----------------+------+---------------------+---------------------+---------+-------------------------------+-------+-------------+
2 rows in set (0.00 sec)

mysql> desc contests;
  +------------+------------------+------+-----+----------+----------------+
  | Field      | Type             | Null | Key | Default  | Extra          |
  +------------+------------------+------+-----+----------+----------------+
  | chid       | int(10) unsigned | NO   | PRI | NULL     | auto_increment | 
  | puzzle     | char(81)         | YES  |     | NULL     |                | 
  | solution   | char(81)         | YES  |     | NULL     |                | 
  | isComplete | tinyint(1)       | YES  |     | 0        |                | 
  | diff_level | char(7)          | YES  |     | NULL     |                | 
  | time       | time             | YES  |     | 00:00:00 |                | 
  +------------+------------------+------+-----+----------+----------------+
  6 rows in set (0.00 sec)

mysql> desc contest_users;
  +------------+----------------------+------+-----+---------+-------+
  | Field      | Type                 | Null | Key | Default | Extra |
  +------------+----------------------+------+-----+---------+-------+
  | chid       | int(10) unsigned     | NO   | MUL | NULL    |       | 
  | uid        | bigint(20) unsigned  | YES  |     | NULL    |       | 
  | gamestate  | char(81)             | YES  |     | NULL    |       | 
  | score      | int(10) unsigned     | YES  |     | 0       |       | 
  | no_correct | smallint(5) unsigned | YES  |     | 0       |       | 
  | no_wrong   | smallint(5) unsigned | YES  |     | 0       |       | 
  | time       | time                 | YES  |     | NULL    |       | 
  | isComplete | tinyint(1)           | YES  |     | 0       |       | 
  +------------+----------------------+------+-----+---------+-------+
  8 rows in set (0.00 sec)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱成瘾 2024-08-30 14:46:33

我的猜测是,因为行数很少。

当您有数千行时,查询优化器可能会做出不同的决定。

My guess, because there are very few rows.

The query optimizer will probably make different decisions when you have many thousands of rows.

假装爱人 2024-08-30 14:46:33

我(最近)的建议是在竞赛表上添加一个键,它必须包含用于连接(chid)的列以及您想要从该查询中获得的列。

这将导致mysql实际上使用索引来获取数据而不是扫描整个表(解释结果中的type =“index”,extra =“use index”)。

由于 chid 位于结果集中并且也处于连接条件中,因此在您的情况下它将如下所示:

alter table contests add key `chid_diffLevel` (chid, diff_level);

我希望它对您有用。我已经在我的一个数据库上尝试过了,我知道它对我有用。

My (late) advice is to add a key on contests table, it has to include the column used for join (chid) and also the columns you want from that query.

This will cause mysql to actually use the index to get the data instead of scanning the whole table (type="index", extra="use index" in explain result).

Since chid is in the resultset and also in the join condition, in your case it will look like this:

alter table contests add key `chid_diffLevel` (chid, diff_level);

I hope it works for you. I've tried it on one of my databases and I know it works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文