理解 MySQL 的“解释”

Mic*_*man 1 mysql execution-plan explain

我有以下查询explain

mysql> explain 
     select dd.data from dane dd 
       join test1.tag1 t1 on dd.id = t1.id 
       join test1.tag2 t2 on dd.id = t2.id 
       join test1.tag3 t3 on dd.id = t3.id 
       join test1.tag4 t4 on dd.id = t4.id 
       join test1.tag5 t5 on dd.id = t5.id 
       join test1.tag6 t6 on dd.id = t6.id 
       join test1.tag7 t7 on dd.id = t7.id 
       join test1.tag8 t8 on dd.id = t8.id 
       join test1.tag9 t9 on dd.id = t9.id 
       join test1.tag10 t10 on dd.id = t10.id 
     where t1.val = 1 
       and t2.val = 2 
       and t3.val = 3 
       and t4.val = 4 
       and t5.val = 5 
       and t6.val = 6 
       and t7.val = 7 
       and t8.val = 8 
       and t9.val = 9 
       and t10.val = 10 ;
+--+-----------+-----+------+-------------+-------+-------+------------------+-----+------------------------+
|id|select_type|table|type  |possible_keys|key    |key_len|ref               |rows |Extra                   |
+--+-----------+-----+------+-------------+-------+-------+------------------+-----+------------------------+
| 1|SIMPLE     |t10  |index |PRIMARY      |PRIMARY|8      |NULL              |94577|Using where; Using index|
| 1|SIMPLE     |dd   |eq_ref|PRIMARY      |PRIMARY|4      |test1.t10.id      |    1|                        |
| 1|SIMPLE     |t8   |eq_ref|PRIMARY      |PRIMARY|8      |test1.t10.id,const|    1|Using index             |
| 1|SIMPLE     |t1   |eq_ref|PRIMARY      |PRIMARY|8      |test1.t8.id,const |    1|Using where; Using index|
| 1|SIMPLE     |t2   |eq_ref|PRIMARY      |PRIMARY|8      |test1.t1.id,const |    1|Using where; Using index|
| 1|SIMPLE     |t6   |eq_ref|PRIMARY      |PRIMARY|8      |test1.dd.id,const |    1|Using where; Using index|
| 1|SIMPLE     |t4   |eq_ref|PRIMARY      |PRIMARY|8      |test1.t8.id,const |    1|Using where; Using index|
| 1|SIMPLE     |t3   |eq_ref|PRIMARY      |PRIMARY|8      |test1.dd.id,const |    1|Using where; Using index|
| 1|SIMPLE     |t7   |eq_ref|PRIMARY      |PRIMARY|8      |test1.dd.id,const |    1|Using where; Using index|
| 1|SIMPLE     |t9   |eq_ref|PRIMARY      |PRIMARY|8      |test1.t10.id,const|    1|Using index             |
| 1|SIMPLE     |t5   |eq_ref|PRIMARY      |PRIMARY|8      |test1.dd.id,const |    1|Using where; Using index|
+--+-----------+-----+------+-------------+-------+-------+------------------+-----+------------------------+
11 rows in set (0.08 sec)
Run Code Online (Sandbox Code Playgroud)

每个 tag1 - tag10 表都具有相同的构建:两列 ( id, val)id是主键,没有其他索引。

我对解释的第一行有疑问:

  1. 行数 - 这个数字是扫描行的确切数量还是只是估计?每个表有 100,000 行。
  2. 第一行有类型index,但Extra显示Using where; Using index。这是否意味着,使用第一个全表扫描where,然后indexWhere位于val没有任何索引的列上。

Bil*_*win 5

type: index意味着它是一个索引扫描。也就是说,它正在扫描该表的整个索引。

索引扫描通常伴随着,Using index因为后者表明查询能够使用索引来满足查询,而不会触及表的行。Using index会更清楚地标注Using only index

索引中的条目与表中的行rows数一样多,因此索引扫描报告的行数与表扫描期间报告的行数相同。

报告的数字rows是估计值,基于 InnoDB 收集的有关该表的统计信息。它可能不准确 10% 甚至更多。不要依赖于这是一个精确的数字,而是将其视为按实际行数(或索引条目)的数量级的指标。

您的tags表上可能没有二级索引,但您有一个主键,它隐式地有一个索引。该type: eq_ref指示查询使用的主键找到相应的行期间加入。按主键查找很好。

要阅读有关 EXPLAIN 输出的更多信息,请阅读:http : //dev.mysql.com/doc/refman/5.6/en/explain-output.html