mysql select type_explain之select_type分析

MySQL EXPLAIN分析 select_type字段详解

SIMPLE

简单查询,不包含子查询和union

示例

下面的单标查询不包含子查询和union,则 select_type就为SIMPLE

mysql> explain select * from article;

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+

| 1 | SIMPLE | article | NULL | ALL | NULL | NULL | NULL | NULL | 38 | 100.00 | NULL |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+

1 row in set, 1 warning (0.00 sec)

PRIMARY

查询中包含任意复杂的子部分,最外层查询会被标记为primary

示例

mysql> explain select id, (select count(id) from article_detail where article_id=w1.id) from article as w1;

+----+--------------------+----------------+------------+--------+----------------+----------------+---------+-----------+------+----------+--------------------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+--------------------+----------------+------------+--------+----------------+----------------+---------+-----------+------+----------+--------------------------+

| 1 | PRIMARY | w1 | NULL | index | NULL | PRIMARY | 130 | NULL | 38 | 100.00 | Using index |

| 2 | DEPENDENT SUBQUERY | article_detail | NULL | eq_ref | idx_article_id | idx_article_id | 34 | lrw.w1.id | 1 | 100.00 | Using where; Using index |

+----+--------------------+----------------+------------+--------+----------------+----------------+---------+-----------+------+----------+--------------------------+

2 rows in set, 2 warnings (0.00 sec)

SUBQUERY

查询中包含子查询

示例

mysql> explain select id, (select count(id) from article_detail where article_id=w1.id) from article as w1;

+----+--------------------+----------------+------------+--------+----------------+----------------+---------+-----------+------+----------+--------------------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+--------------------+----------------+------------+--------+----------------+----------------+---------+-----------+------+----------+--------------------------+

| 1 | PRIMARY | w1 | NULL | index | NULL | PRIMARY | 130 | NULL | 38 | 100.00 | Using index |

| 2 | DEPENDENT SUBQUERY | article_detail | NULL | eq_ref | idx_article_id | idx_article_id | 34 | lrw.w1.id | 1 | 100.00 | Using where; Using index |

+----+--------------------+----------------+------------+--------+----------------+----------------+---------+-----------+------+----------+--------------------------+

2 rows in set, 2 warnings (0.00 sec)

DERIVED

示例

衍生表, 子查询会被标记为derived, mysql会递归这些子查询,把结果放入临时表

EXPLAIN SELECT

*

FROM

contract_installment

WHERE

contract_id IN (

SELECT

b1.contract_id

FROM

( SELECT contract_id, sum( pay_amount ) AS totalPayAmount FROM contract_installment WHERE pay_off = 1 GROUP BY contract_id ) as b1)

+----+-------------+----------------------+------------+------+---------------+-------------+---------+---------------------------------------+------+----------+----------------------------------------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+----------------------+------------+------+---------------+-------------+---------+---------------------------------------+------+----------+----------------------------------------------+

| 1 | PRIMARY | contract_installment | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using where |

| 1 | PRIMARY | | NULL | ref | | | 5 | test.contract_installment.contract_id | 2 | 100.00 | FirstMatch(contract_installment) |

| 3 | DERIVED | contract_installment | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using where; Using temporary; Using filesort |

+----+-------------+----------------------+------------+------+---------------+-------------+---------+---------------------------------------+------+----------+----------------------------------------------+

3 rows in set, 1 warning (0.00 sec)

union

当union出现在第二个select中,则被标记为union

当union出现在子查询中,最外层select被标记为derived

示例

mysql> explain select * from contract where id in(1,2,3) union select * from contract where id in(4,5,6);

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

| 1 | PRIMARY | contract | NULL | ALL | PRIMARY | NULL | NULL | NULL | 1 | 100.00 | Using where |

| 2 | UNION | contract | NULL | ALL | PRIMARY | NULL | NULL | NULL | 1 | 100.00 | Using where |

| NULL | UNION RESULT | | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

3 rows in set, 1 warning (0.00 sec)

union result

union的结果集

示例

mysql> explain select * from contract where id in(1,2,3) union select * from contract where id in(4,5,6);

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

| 1 | PRIMARY | contract | NULL | ALL | PRIMARY | NULL | NULL | NULL | 1 | 100.00 | Using where |

| 2 | UNION | contract | NULL | ALL | PRIMARY | NULL | NULL | NULL | 1 | 100.00 | Using where |

| NULL | UNION RESULT | | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |

+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+

3 rows in set, 1 warning (0.00 sec)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值