简体   繁体   English

为什么这个MySQL查询的解释计划只有三行?

[英]Why does this MySQL query's explain plan have only three rows?

There are three MySQL tables: 有三个MySQL表:

在此处输入图片说明

To join all three tables, all we need are the PKs, plus an index for courseid: 要连接所有三个表,我们需要的是PK,以及课程编号的索引:

alter table enrollment add index (courseid);

The query: 查询:

select s.name, c.name, e.semesterid
from student s
join enrollment e on s.id=e.studentid
join course c on c.id=e.courseid;

The explain plan: 解释计划:

+----+-------------+-------+--------+------------------+----------+---------+------------------+------+-------------+
| id | select_type | table | type   | possible_keys    | key      | key_len | ref              | rows | Extra       |
+----+-------------+-------+--------+------------------+----------+---------+------------------+------+-------------+
|  1 | SIMPLE      | e     | index  | PRIMARY,courseid | courseid | 4       | NULL             |   10 | Using index |
|  1 | SIMPLE      | s     | eq_ref | PRIMARY          | PRIMARY  | 4       | test.e.studentid |    1 |             |
|  1 | SIMPLE      | c     | eq_ref | PRIMARY          | PRIMARY  | 4       | test.e.courseid  |    1 |             |
+----+-------------+-------+--------+------------------+----------+---------+------------------+------+-------------+

Explain plan looks fine, indexes will be used, with no full table scans. 解释计划看起来不错,将使用索引,而无需全表扫描。 But here is the question, why does the plan have only three rows? 但是这里的问题是,为什么计划只有三行? I would expect four rows. 我希望有四行。 I would expect: 我期望:

  1. Table Student - Primary Key 表学生-主键
  2. Table Enrollment - Primary Key <-- this one I dont see in the plan, why? 表注册-主键<-我在计划中没有看到的这是为什么?
  3. Table Enrollment - index courseid 表注册-索引课程ID
  4. Table Course - Primary Key 表课程-主键

The query joins three tables, that means two times joining two tables. 该查询联接三个表,这意味着两次联接两个表。 That means I expect four indexes to be utilized. 这意味着我希望可以利用四个索引。

在此处输入图片说明

http://sqlfiddle.com/#!2/b30132/2 http://sqlfiddle.com/#!2/b30132/2

It will eventually have a table scan (you have no where clause) 最终将进行表扫描(您没有where子句)

It is linking three tables together. 它正在将三个表链接在一起。 Two bonds. 两个债券。 Scan down one and use the indexes to link to the other two. 向下扫描一个,并使用索引链接到其他两个。

Three rows in the plan 计划中的三行

BTW - CourseID a PK in two of them helps 顺便说一句-CourseID和PK在其中两个帮助中

EDIT 编辑

For your update. 为了您的更新。

No where clause - So need everything No sorting either 没有where子句-因此需要所有内容也不进行排序

Go down the Enrolment table. 在注册表下。 Use the CourseID as it is the primary key. 使用CourseID,因为它是主键。 Got the courseid in the other table as well (Course). 在另一个表中也获得了课程编号(课程)。

So got a join down. 所以加入了。 Now need to find the student bit. 现在需要找到学生位。 looking at a particular enrolment so got the student ID so use the PK in the student to find the details of the student. 查看特定的入学申请,因此获得了学生ID,因此请使用学生中的PK来查找学生的详细信息。

No need for the PK in enrolment for enrolment 无需注册PK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM