MySQL数据库八(explain)

explain的作用:

通过explain+sql语句可以知道如下内容:

①表的读取顺序。(对应id)

②数据读取操作的操作类型。(对应select_type)

③哪些索引可以使用。(对应possible_keys)

④哪些索引被实际使用。(对应key)

⑤表直接的引用。(对应ref)

⑥每张表有多少行被优化器查询。(对应rows)

1、id

INSERT INTO n VALUES (5,'FXY',25);

 insert into n values(7,'SXY',24);//大小写都可以

explain select * from n;

MySQL数据库八(explain)

 当ID顺序相同的时候执行顺序是由上到下的

新建一个表 CREATE TABLE m(id INT,name VARCHAR(10),age INT);

MySQL数据库八(explain)

来看一下他的子查询

MySQL数据库八(explain)

 这里如果是子查询id大的会递增,id越大优先级越高越先被执行

 explain select n.* from(
  select m.id
 from m
where m.name = ' ')s,m
 where s.id = m.id;

2、数据读取操作的操作类型。(对应select_type)

explain select * from (select * from n where id = 1)m;

MySQL数据库八(explain)

 const:表示通过一次索引就找到了,const用于比较primary key 或者 unique 索引,因为只匹配一行数据,所以很快。

 

explain select * from m,n where m.id = n.id;

MySQL数据库八(explain)

eq_ref唯一性索引扫描,对于每一个索引键,只有一条记录与之匹配。常见于主键或唯一索引扫描(一个公司只有一个CEO)

 

 create index idx_name_age on l(name,age);//创建一个复合索引