专栏名称: 人工智能头条
专注人工智能技术前沿、实战技巧及大牛心得。
目录
相关文章推荐
爱可可-爱生活  ·  Andrej ... ·  2 天前  
宝玉xp  ·  转发微博-20250603073219 ·  2 天前  
爱可可-爱生活  ·  《爱可可微博热门分享(6.2)》 ... ·  2 天前  
爱可可-爱生活  ·  【[31星]Chatterbox-TTS-S ... ·  2 天前  
爱可可-爱生活  ·  【[81星]Beam-and-Spyroso ... ·  2 天前  
51好读  ›  专栏  ›  人工智能头条

面试官:不会看 Explain执行计划,简历敢写 SQL 优化?

人工智能头条  · 公众号  · AI  · 2021-05-17 18:30

正文

请到「今天看啥」查看全文


( select one_id from one where o.one_name= "我是第一表2" );
+ ----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref                | rows | filtered | Extra       |
+ ----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
|  1 | PRIMARY     | o     | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL               |    2 |       50 | Using where |
|  1 | PRIMARY     | one   | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | xin-slave.o.one_id |    1 |      100 | Using index |
|  2 | SUBQUERY    | t     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL               |    2 |       50 | Using where |
|  3 | SUBQUERY    | r     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL               |    2 |       50 | Using where |
+ ----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+-------------+

二、select_type

select_type :表示 select 查询的类型,主要是用于区分各种复杂的查询,例如: 普通查询 联合查询 子查询 等。

1、SIMPLE

SIMPLE :表示最简单的 select 查询语句,也就是在查询中不包含子查询或者 union 交并差集等操作。

2、PRIMARY

PRIMARY :当查询语句中包含任何复杂的子部分,最外层查询则被标记为 PRIMARY

3、SUBQUERY

SUBQUERY :当 select where 列表中包含了子查询,该子查询被标记为: SUBQUERY

4、DERIVED

DERIVED :表示包含在 from 子句中的子查询的select,在我们的 from 列表中包含的子查询会被标记为 derived

5、UNION

UNION :如果 union 后边又出现的 select 语句,则会被标记为 union ;若 union 包含在 from 子句的子查询中,外层 select 将被标记为 derived

6、UNION RESULT

UNION RESULT :代表从 union 的临时表中读取数据,而 table 列的 表示用第一个和第四个 select 的结果进行 union 操作。

mysql> EXPLAIN select t.two_name, ( select one.one_id from one) o from (select two_id,two_name from two where two_name ='') t  union (select r.three_name,r.three_id from three r);

+------+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| id   | select_type  | table      | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra           |
+------+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
|    1 | PRIMARY      | two        | NULL       | ALL   | NULL          | NULL    | NULL    | NULL |    2 |       50 | Using where     |
|    2 | SUBQUERY     | one        | NULL       | index | NULL          | PRIMARY | 4       | NULL |    2 |      100 | Using index     |
|    4 | UNION        | r          | NULL       | ALL   | NULL          | NULL    | NULL    | NULL |    2 |      100 | NULL            |
| NULL | UNION RESULT |  | NULL       | ALL   | NULL          | NULL    | NULL    | NULL | NULL | NULL     | Using temporary |
+------+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+

三、table

查询的表名,并不一定是真实存在的表,有别名显示别名,也可能为临时表,例如上边的 DERIVED 等。

四、partitions

查询时匹配到的分区信息,对于非分区表值为 NULL ,当查询的是分区表时, partitions 显示分区表命中的分区情况。

+----+-------------+----------------+---------------------------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table          | partitions                      | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------------+---------------------------------+-------+---------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | one            | p201801,p201802,p201803,p300012 | index | NULL          | PRIMARY | 9       | NULL |    3 |      100 | Using index |
+----+-------------+----------------+---------------------------------+-------+---------------+---------+---------+------+------+----------+-------------+

五、type

type :查询使用了何种类型,它在 SQL 优化中是一个非常重要的指标,以下性能从好到坏依次是: system > const > eq_ref > ref > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

1、system

system : 当表仅有一行记录时(系统表),数据量很少,往往不需要进行磁盘IO,速度非常快。

2、const

const :表示查询时命中 primary key 主键或者 unique 唯一索引,或者被连接的部分是一个常量( const )值。这类扫描效率极高,返回数据量少,速度非常快。

mysql> EXPLAIN SELECT * from three where three_id=1;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | three | NULL       | const | PRIMARY       | PRIMARY | 4       | const |    1 |      100 | NULL  |






请到「今天看啥」查看全文