MySQL 执行计划使用详解,2024年最新华为外包Java面试题

image.png

select_type


包括范围: simple. primary,subquery, derived, union, union result 查询类型主要是用于区别普通查询,联合查询,子查询等复杂的查询

  • simple,简单的select 语句,查询中不包含自查询或者 union

  • primary, 查询若包含任何复杂的子部分,最外层查询则被标记为primary

  • subquery, 在 select 或 where 列表中包含子查询

  • derived,在 from 列表中包含自查询被标记为 derived (衍生)MySQL 会递归执行这些自查询,把结果放在临时表中。

  • union,若第二个 select 出现在 union 之后,则被标记为 union. 若 union 包含在 from 子句子查询中,外层 select 将别标记为 derived

  • union result, 从 union 表中获取结果的 select

table


  • 这行数据是关于那种表的

type


类型: all , index , range, ref, eq_ref, const, system ,null type 显示的是防卫类型,是较为重要的一个指标,结果从好到坏依次是: system > count > eq_ref > range > index > all

​sytem > const > eq_ref > ref > fulltext > ref_or_null > index_merge >> unique_subquery > index_subquery > range > index > ALL

system

表只有一行记录(等于系统表),这是 const 类型的特列, 平时不会出现,这个也可以忽略不计

count

explain select * from (select * from t1 where id =1) d1;

表示通过索引一次就找到了, const 用于比较 primary key 或者 unique 索引。 因为只匹配一行数据,所以很快如将主键置于where 列表中, MySQL 就能将该查询转换为一个常量。

image.png

eq_ref

explain select * from t1, t2 where t1.id = t2.id

唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或唯一索引扫描. 查询案例:

image.png

ref

tb_emp ddl

CREATE TABLE tb_emp (

id int(11) NOT NULL AUTO_INCREMENT,

name varchar(30) DEFAULT NULL,

dept_id int(11) DEFAULT NULL,

PRIMARY KEY (id),

) ;

#员工表添加年龄列

alter table tb_emp add column age int(11) default null after name;

#添加复合索引

create index idx_emp_name_age on tb_emp(name, age);

explain select * from t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值