zoukankan      html  css  js  c++  java
  • 详解MySQL中EXPLAIN解释命令

    Explain 结果解读与实践
     
    基于 MySQL 5.0.67 ,存储引擎 MyISAM 。
     
    注:单独一行的"%%"及"`"表示分隔内容,就象分开“第一章”“第二章”。
     
    explain 可以分析 select 语句的执行,即 MySQL 的“执行计划”:
     
    mysql> explain select 1;
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
    |  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
     
    用"G"代替分号可得到竖排的格式:
     
    mysql> explain select 1G
    *************************** 1
               id: 1
      select_type: SIMPLE
            table: NULL
             type: NULL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: NULL
            Extra: No tables used
     
    `
     
    可以用 desc 代替 explain :
    desc select 1;
     
    %%
     
    id 列
     
    建表:
    create table a(a_id int);
    create table b(b_id int);
    create table c(c_id int);
     
    mysql> explain select * from a join b on a_id=b_id where b_id in (select c_id from c);
    +----+--------------------+-------+...
    | id | select_type        | table |...
    +----+--------------------+-------+...
    |  1 | PRIMARY            | a     |...
    |  1 | PRIMARY            | b     |...
    |  2 | DEPENDENT SUBQUERY | c     |...
    +----+--------------------+-------+...
     
    从 3 个表中查询,对应输出 3 行,每行对应一个表, id 列表示执行顺序,id 越大,越先执行,id 相同,由上至下执行。此处的执行顺序为(以 table 列表示):c -> a -> b
     
    %%
     
    select_type 列
     
    MySQL 把 SELECT 查询分成简单和复杂两种类型,复杂类型又可以分成三个大类:简单子查询、所谓的衍生表(子查询在 FROM 子句里)和 UNION 。
     
    SIMPLE:查询中不包含子查询或者UNION
     
    mysql> explain select * from a;
    +----+-------------+-------+...
    | id | select_type | table |...
    +----+-------------+-------+...
    |  1 | SIMPLE      | a     |...
    +----+-------------+-------+...
     
    SUBQUERY:子查询
    PRIMARY:子查询上层
     
    mysql> explain select * from a where a_id in (select b_id from b);
    +----+--------------------+-------+...
    | id | select_type        | table |...
    +----+--------------------+-------+...
    |  1 | PRIMARY            | a     |...
    |  2 | DEPENDENT SUBQUERY | b     |...
    +----+--------------------+-------+...
     
    DERIVED:在FROM列表中包含子查询, MySQL 会递归执行这些子查询,把结果放在临时表里。
     
    mysql> explain select count(*) from (select * from a) as der;
    +----+-------------+-------+...
    | id | select_type | table |...
    +----+-------------+-------+...
    |  1 | PRIMARY     | NULL  |...
    |  2 | DERIVED     | a     |...
    +----+-------------+-------+...
     
    %%
     
    table 列
     
    显示每行对应的表名。若在 SELECT 语句中为表起了别名,则会显示表的别名。
     
    一个很复杂的示例及解释可参考《高性能 MySQL 》(第二版)中文版 P467(pdf.491) 〈附录 B.2.3 table 列〉
     
    %%
     
    type 列
     
    MySQL 在表里找到所需行的方式。包括(由左至右,由最差到最好):
    | All | index | range | ref | eq_ref | const,system | null |
     
    ALL
    全表扫描,MySQL 从头到尾扫描整张表查找行。
     
    mysql> explain select * from aG
    ...
             type: ALL
     
    如果加上 limit 如 select * from a limit 10 MySQL 会扫描 10 行,但扫描方式不会变,还是从头到尾扫描。
     
    `
     
    index
    按索引次序扫描表,就是先读索引,再读实际的行,其实还是全表扫描。主要优点是避免了排序,因为索引是排好序的。
     
    建表:
    create table a(a_id int not null, key(a_id));
    insert into a value(1),(2)
     
    mysql> explain select * from aG
    ...
             type: index
     
    `
     
    range
    以范围的形式扫描索引
     
    建表:
    create table a(a_id int not null, key(a_id));
    insert into a values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
     
    mysql> explain select * from a where a_id > 1G
    ...
             type: range
    ...
     
    IN 比较符也会用 range 表示:
     
    mysql> explain select * from a where a_id in (1,3,4)G
    ...
             type: range
    ...
     
    `
     
    ref
    非唯一性索引访问
     
    建表:
    create table a(a_id int not null, key(a_id));
    insert into a values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
     
    mysql> explain select * from a where a_id=1G
    ...
             type: ref
    ...
     
    `
     
    eq_ref
    使用有唯一性索引查找(主键或唯一性索引)
     
    建表及插入数据:
    create table a(id int primary key);
    create table a_info(id int primary key, title char(1));
    insert into a value(1),(2);
    insert into a_info value(1, 'a'),(2, 'b');
     
    mysql> explain select * from a join a_info using(id);
    ...+--------+--------+...
    ...| table  | type   |...
    ...+--------+--------+...
    ...| a      | index  |...
    ...| a_info | eq_ref |...
    ...+--------+--------+...
     
    此时 a_info 每条记录与 a 一一对应,通过主键 id 关联起来,所以 a_info 的 type 为 eq_ref。
     
    删除 a_info 的主键:ALTER TABLE  `a_info` DROP PRIMARY KEY;
    现在 a_info 已经没有索引了:
     
    mysql> explain select * from a join a_info using(id);
    +----+...+--------+--------+...
    | id |...| table  | type   |...
    +----+...+--------+--------+...
    |  1 |...| a_info | ALL    |...
    |  1 |...| a      | eq_ref |...
    +----+...+--------+--------+...
     
    这次 MySQL 调整了执行顺序,先全表扫描 a_info 表,再对表 a 进行 eq_ref 查找,因为 a 表 id 还是主键。
     
    删除 a 的主键:alter table a drop primary key;
    现在 a 也没有索引了:
     
    mysql> explain select * from a join a_info using(id);
    ...+--------+------+...
    ...| table  | type |...
    ...+--------+------+...
    ...| a      | ALL  |...
    ...| a_info | ALL  |...
    ...+--------+------+...
     
    现在两个表都使用全表扫描了。
     
    建表及插入数据:
    create table a(id int primary key);
    create table a_info(id int, title char(1), key(id));
    insert into a value(1),(2);
    insert into a_info value(1, 'a'),(2, 'b');
     
    现在 a_info 表 id 列变为普通索引(非唯一性索引):
     
    mysql> explain select * from a join a_info using(id) where a.id=1;
    ...+--------+-------+...
    ...| table  | type  |...
    ...+--------+-------+...
    ...| a      | const |...
    ...| a_info | ref   |...
    ...+--------+-------+...
     
    a_info 表 type 变为 ref 类型了。
     
    所以,唯一性索引才会出现 eq_ref (非唯一性索引会出现 ref ),因为唯一,所以最多只返回一条记录,找到后无需继续查找,因此比 ref 更快。
     
    `
     
    const
    被称为“常量”,这个词不好理解,不过出现 const 的话就表示发生下面两种情况:
     
    在整个查询过程中这个表最多只会有一条匹配的行,比如主键 id=1 就肯定只有一行,只需读取一次表数据便能取得所需的结果,且表数据在分解执行计划时读取。
     
    返回值直接放在 select 语句中,类似 select 1 AS f 。可以通过 extended 选择查看内部过程:
     
    建表及插入数据:
    create table a(id int primary key, c1 char(20) not null, c2 text not null, c3 text not null);
    insert into a values(1, 'asdfasdf', 'asdfasdf', 'asdfasdf'), (2, 'asdfasdf', 'asdfasdf', 'asdfasdf');
     
    mysql> explain extended select * from a where id=1G
    ...
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
    ...
     
    用 show warnings 查看 MySQL 是如何优化的:
     
    mysql> show warningsG
    ...
    Message: select '1' AS `id`,'asdfasdf' AS `c1`,'asdfasdf' AS `c2`,'asdfasdf' AS
    `c3` from `test`.`a` where 1
     
    查询返回的结果为:
     
    mysql> select * from a where id=1;
    +----+----------+----------+----------+
    | id | c1       | c2       | c3       |
    +----+----------+----------+----------+
    |  1 | asdfasdf | asdfasdf | asdfasdf |
    +----+----------+----------+----------+
     
    可以看出,返回结果中的字段值都以“值 AS 字段名”的形式直接出现在优化后的 select 语句中。
     
    修改一下查询:
     
    mysql> explain select * from a where id in(1,2)G
    ...
             type: range
    ...
     
    当返回结果超过 1 条时, type 便不再为 const 了。
     
    重新建表及插入数据:
    create table a (id int not null);
    insert into a value(1),(2),(3);
     
    mysql> explain select * from a where id=1G
    ...
             type: ALL
     
    目前表中只有一条 id=1 的记录,但 type 已为 ALL ,因为只有唯一性索引才能保证表中最多只有一条记录,只有这样 type 才有可能为 const 。
    为 id 加普通索引后, type 变为 ref ,改为加唯一或主键索引后, type 便变为 const 了。
     
    相关阅读:
    《高性能 MySQL 》第 2 版中文版
    P130(pdf.153) 计算和减少常量表达式
    P471(pdf.495) B.2.4 type 列
     
    《 MySQL 性能调优与架构设计》
    pdf.109
     
    MySQL 帮助手册 7.2.1. EXPLAIN语法(获取SELECT相关信息)
     
    `
     
    system
     
    system 是 const 类型的特例,当表只有一行时就会出现 system 。
     
    建表及插入数据:
    create table a(id int primary key);
    insert into a value(1);
     
    mysql> explain select * from aG
    ...
             type: system
    ...
     
    `
     
    NULL
    在优化过程中就已得到结果,不用再访问表或索引。
     
    mysql> explain select min(a_id) from aG
    ...
             type: NULL
    ...
     
    %%
     
    possible_keys 列
     
    可能被用到的索引
     
    建表:
    create table a (a_id int primary key, a_age int, key (a_id, a_age));
    此表有 主键及普通索引 两个索引。
     
    mysql> explain select * from a where a_id=1G
    ...
    possible_keys: PRIMARY,a_id
     
    %%
     
    key 列
     
    查询过程中实际使用的索引
     
    mysql> explain select * from a where a_id=1G
    ...
    possible_keys: PRIMARY,a_id
              key: PRIMARY
     
    %%
     
    key_len 列
     
    索引字段最大可能使用的长度。
     
    mysql> explain select * from a where a_id=1G
    ...
              key: PRIMARY
          key_len: 4
     
    a_id 是 int 类型,int 的长度是 4 字节,所以 key_len 为 4。
     
    %%
     
    ref 列
    指出对 key 列所选择的索引的查找方式,常见的值有 const, func, NULL, 具体字段名。当 key 列为 NULL ,即不使用索引时,此值也相应的为 NULL 。
     
    建表及插入数据:
    create table a(id int primary key, age int);
    insert into a value(1, 10),(2, 10);
     
    mysql> desc select * from a where age=10G
    ...
              key: NULL
          key_len: NULL
              ref: NULL
    ...
     
    当 key 列为 NULL , ref 列也相应为 NULL 。
     
    mysql> explain select * from a where id=1G
    ...
              key: PRIMARY
          key_len: 4
              ref: const
     
    这次 key 列使用了主键索引,where id=1 中 1 为常量, ref 列的 const 便是指这种常量。
     
    mysql> explain select * from a where id in (1,2)G
    ...
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: NULL
    ...
     
    不理解 ref 为 NULL 的含意,比如上面这个查询, key 列有使用索引,但 ref 列却为 NULL 。网上搜索及查阅了一下 MySQL 帮助手册都没有找到相关的描述。
     
    再建表及插入数据:
    create table a(id int primary key, a_name int not null);
    create table b(id int primary key, b_name int not null);
    insert into a value(1, 1),(2, 2),(3, 3);
    insert into b value(1, 111),(2, 222),(3, 333);
     
    mysql> explain select * from a join b using(id);
    ...+-------+--------+...+---------+...+-----------+...
    ...| table | type   |...| key     |...| ref       |...
    ...+-------+--------+...+---------+...+-----------+...
    ...| a     | ALL    |...| NULL    |...| NULL      |...
    ...| b     | eq_ref |...| PRIMARY |...| test.a.id |...
    ...+-------+--------+...+---------+...+-----------+...
     
    这里 test.a.id 即为具体字段,意为根据表 a 的 id 字段的值查找表 b 的主键索引。
     
    mysql> explain select * from a join b using(id) where b.id=1;
    ...+-------+-------+...+---------+...+-------+...
    ...| table | type  |...| key     |...| ref   |...
    ...+-------+-------+...+---------+...+-------+...
    ...| a     | const |...| PRIMARY |...| const |...
    ...| b     | const |...| PRIMARY |...| const |...
    ...+-------+-------+...+---------+...+-------+...
     
    因为 a join b 的条件为 id 相等,而 b.id=1 ,就是 a.id 也为 1 ,所以 a,b 两个表的 ref 列都为 const 。
     
    ref 为 func 的情况出现在子查询中,暂不明其原理:
     
    mysql> explain select * from a where id in (select id from b where id in (1,2));

    +----+--------------------+-------+...+---------+...+------+...
    | id | select_type        | table |...| key     |...| ref  |...
    +----+--------------------+-------+...+---------+...+------+...
    |  1 | PRIMARY            | a     |...| NULL    |...| NULL |...
    |  2 | DEPENDENT SUBQUERY | b     |...| PRIMARY |...| func |...
    +----+--------------------+-------+...+---------+...+------+...
     
    %%
     
    rows 列
     
    MySQL 估计的需要扫描的行数。只是一个估计。
     
    %%
     
    Extra 列
     
    显示上述信息之外的其它信息,但却很重要。
     
    Using index
    此查询使用了覆盖索引(Covering Index),即通过索引就能返回结果,无需访问表。
    若没显示"Using index"表示读取了表数据。
     
    建表及插入数据:
    create table a (id int primary key, age int);
    insert into a value(1, 10),(2, 10);
     
    mysql> explain select id from aG
    ...
            Extra: Using index
     
    因为 id 为主键索引,索引中直接包含了 id 的值,所以无需访问表,直接查找索引就能返回结果。
     
    mysql> explain select age from aG
    ...
            Extra:
     
    age 列没有索引,因此没有 Using index ,意即需要访问表。
     
    为 age 列添加索引:create index age on a(id, age);
     
    mysql> explain select age from aG
    ...
            Extra: Using index
     
    现在索引 age 中也包含了 age 列的值,因此不用访问表便能返回结果了。
     
    建表:create table a(id int auto_increment primary key, age int, name char(10));
    插入 100w 条数据:insert into a value(null, rand()*100000000, 'jack');
     
    `
     
    Using where
    表示 MySQL 服务器从存储引擎收到行后再进行“后过滤”(Post-filter)。所谓“后过滤”,就是先读取整行数据,再检查此行是否符合 where 句的条件,符合就留下,不符合便丢弃。因为检查是在读取行后才进行的,所以称为“后过滤”。
     
    建表及插入数据:
    create table a (num_a int not null, num_b int not null, key(num_a));
    insert into a value(1,1),(1,2),(2,1),(2,2);
     
    mysql> explain select * from a where num_a=1G
    ...
             type: ref
    possible_keys: num_a
              key: num_a
          key_len: 4
    ...
            Extra:
     
    虽然查询中有 where 子句,但只有 num_a=1 一个条件,且 num_a 列存在索引,通过索引便能确定返回的行,无需进行“后过滤”。
    所以,并非带 WHERE 子句就会显示"Using where"的。
     
    mysql> explain select * from a where num_a=1 and num_b=1G
    ...
             type: ref
    possible_keys: num_a
              key: num_a
    ...
            Extra: Using where
     
    此查询增加了条件 num_b=1 ,此列没有索引,但可以看到查询同样能使用 num_a 索引。 MySQL 先通过索引 num_a 找到 num_a=1 的行,然后读取整行数据,再检查 num_b 是否等于 1 ,执行过程看上去象这样:
     
    num_a索引|num_b 没有索引,属于行数据
    +-------+-------+
    | num_a | num_b | where 子句(num_b=1)
    +-------+-------+
    |     1 |     1 | 符合
    |     1 |     2 | 不符合
    |   ... |   ... | ...
    +-------+-------+
     
    在《高性能 MySQL 》(第二版)P144(pdf.167) 页有更形象的说明图片(图 4-5 MySQL 通过整表扫描查找数据)。
     
    字段是否允许 NULL 对 Using where 的影响:
     
    建表及插入数据:
    create table a (num_a int null, num_b int null, key(num_a));
    insert into a value(1,1),(1,2),(2,1),(2,2);
     
    这次 num_a, num_b 字段允许为空。
     
    在上例 num_a not null 时, num_a 索引的长度 key_len 为 4 ,当 num_a null 时, num_a 索引的长度变为了 5 :
     
    mysql> explain select * from a where num_a=1G
    ...
             type: ref
    possible_keys: num_a
              key: num_a
          key_len: 5
    ...
            Extra: Using where
     
    并且哪怕只有 num_a=1 一个条件,也会出现 Using where 。原因暂不明白。
     
    `
     
    Using temporary
    使用到临时表
     
    建表及插入数据:
    create table a(a_id int, b_id int);
    insert into a values(1,1),(1,1),(2,1),(2,2),(3,1);
     
    mysql> explain select distinct a_id from aG
    ...
            Extra: Using temporary
     
    MySQL 使用临时表来实现 distinct 操作。
     
    `
     
    Using filesort
    若查询所需的排序与使用的索引的排序一致,因为索引是已排序的,因此按索引的顺序读取结果返回,否则,在取得结果后,还需要按查询所需的顺序对结果进行排序,这时就会出现 Using filesort 。
     
    建表及插入数据:
    create table a(a_id int, b_id int);
    insert into a values(1,1),(1,1),(2,1),(2,2),(3,1);
     
    mysql> explain select * from a order by a_idG
    ...
            Extra: Using filesort
     
    对于没有索引的表,只要 order by 必会出现 Using filesort 。
     
    现在增加索引:create index a_id on a(a_id);
    把表 a 的记录增加到约 100w(1048576) 条, a_id 与 b_id 都是随机生成的数字:
     
    mysql> select * from a order by rand() limit 10;
    +-------+--------+
    | a_id  | b_id   |
    +-------+--------+
    | 61566 | 961297 |
    | 33951 | 680542 |
    | ..... | ...... |
    +-------+--------+
     
    mysql> explain select * from a order by a_idG
    ...
             type: ALL
    ...
             rows: 1048576
            Extra: Using filesort
     
    同样是 Using filesort ,type 为 ALL ,全表扫描。听说“取全表数据根据ID排序,走索引一定不如直接查,因为可以减少因为需要索引改变数据访问顺序造成随机IO的概率,数据库放弃索引是应该的”,参考:
     
    当 type 为 rang、 ref 或者 index 的时候才有可能利用索引排序,其它,如 ALL ,都无法通过索引排序,此时若有 order by ,如上例,便会出现 Using filesort 。
     
    现在增加 where 子句:
     
    mysql> explain select * from a where a_id=10 order by a_idG
    ...
             type: ref
    possible_keys: a_id
              key: a_id
    ...
             rows: 8
            Extra:
     
    查询走了索引 a_id ,此时 type 为 ref ,直接按索引顺序返回,没有 Using filesort 。
     
    修改 where 子句:
     
    mysql> explain select * from a where a_id>10 and a_id<100 order by a_idG
    ...
             type: range
    possible_keys: a_id
              key: a_id
    ...
             rows: 712
            Extra: Using where
     
    同样利用索引排序,没有 Using filesort 。
     
    再修改 where 子句:
     
    mysql> explain select * from a where a_id >10 order by a_idG
    ...
             type: ALL
    possible_keys: a_id
              key: NULL
    ...
             rows: 1048576
            Extra: Using where; Using filesort
     
    又出现 Using filesort 且 type 变为 ALL 。注意以上例子的 rows 列,此列表示 MySQL 估计查询需要读取的行数,分别为 1048576, 8, 712, 1048576 ,特别注意最后两个数字: 712, 1048576 。
     
    可见,当索引能为查询排除大部份行时( a_id=10 时约读取 8 行,排除了大部份, a_id>10 and a_id<100 时约读取 712 行,同样排除了大部份)便使用索引,否则,如 a_id>10 时约读取 1048576 , MySQL 直接改用全表扫描,再 Using filesort 。也就是说, MySQL 会根据表中的信息及查询来决定使用任种方式。
     
    关于 MySQL 读取数据表的方式,可参考(暂缺参考资料),就会明白为什么需读取 1048576 行时,先读索引再读表数据还不如全表扫描了。
     
    对于多字段排序(order by a, b)及带 group by 的查询,可参考 MySQL 帮助手册 7.2.12. MySQL如何优化ORDER BY 。
     
    %%
     
    参考资料:
     
    MySQL 帮助手册
    7.2.1. EXPLAIN语法(获取SELECT相关信息)
    7.2.12. MySQL如何优化ORDER BY
     
    《高性能 MySQL 》(第二版)中文版
    P463(pdf.487) 附录 B
     
    《 MySQL 性能调优与架构设计》
     
    MySQL ORDER BY 的实现分析
     
    Mysql执行计划
     
    MySQL执行计划解读
     

    Mysql Explain 详解

     
    MYSQL EXPLAIN语句的extended 选项学习体会
     
    Show Warnings
     
    %%
     
    MySQL索引类型一览 让MySQL高效运行起来
     
    Mysql执行计划中的Using filesort
    "指出Mysql可以使用哪个索引在该表中找到行"
     
    2.order by b,如果b列不在索引中,不管b值是否相同,总会出现Using filesort。
     
    %%
     
    What does Using filesort mean in MySQL?
    "任何不能通过index进行的sort都称之为filesort"
     
    mysql explain中的using filesort
    "只能是如果可能的话修改查询的排序条件"
     
    Mysql之EXPLAIN显示using filesort
    "结果昨天看到公司的一个"
    http://www.ccvita.com/169.html(可能这个是出处)
     
    多列索引在建立的时候是以B-树结构建立的,因此建立索引的时候是先建立ID的按顺序排的索引,在相同ID的情况下建立FID按 顺序排的索引,最后在FID 相同的情况下建立按INVERSE_DATE顺序排的索引,如果列数更多以此类推。
     
    要在优化一下这个sql就应该为它建立另一个索引IDX(ID,INVERSE_DATE),这样就消除了using filesort速度也会快很多。
     
    %%
     
    mysql优化Using filesort
    "并且用到了room_number这列索引"
     
    MySQL 索引 优化 Using filesort
    "页面咔的一下就出来了"
     
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------

    explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。

    先解析一条sql语句,看出现什么内容

    EXPLAINSELECTs.uid,s.username,s.name,f.email,f.mobile,f.phone,f.postalcode,f.address
    FROM uchome_space ASs,uchome_spacefieldASf
    WHERE 
    AND s.groupid=0
    AND s.uid=f.uid

    1. id

    SELECT识别符。这是SELECT查询序列号。这个不重要,查询序号即为sql语句执行的顺序,看下面这条sql

    EXPLAINSELECT*FROM(SELECT* FROMuchome_space LIMIT10)ASs

    它的执行结果为


    可以看到这时的id变化了

    2.select_type

    select类型,它有以下几种值

    2.1 simple 它表示简单的select,没有union和子查询

    2.2 primary 最外面的select,在有子查询的语句中,最外面的select查询就是primary,上图中就是这样

    2.3 union union语句的第二个或者说是后面那一个.现执行一条语句,explain 
    select * from uchome_space limit 10 union select * from uchome_space limit 10,10

    会有如下结果

    第二条语句使用了union

    2.4 dependent union    UNION中的第二个或后面的SELECT语句,取决于外面的查询

    2.5 union result        UNION的结果,如上面所示

    还有几个参数,这里就不说了,不重要

    3 table

    输出的行所用的表,这个参数显而易见,容易理解

    4 type

    连接类型。有多个参数,先从最佳类型到最差类型介绍 重要且困难

    4.1 system

    表仅有一行,这是const类型的特列,平时不会出现,这个也可以忽略不计

    4.2 const

    表最多有一个匹配行,const用于比较primary key 或者unique索引。因为只匹配一行数据,所以很快

    记住一定是用到primary key 或者unique,并且只检索出两条数据的 情况下才会是const,看下面这条语句

    explain SELECT * FROM `asj_admin_log` limit 1,结果是

    虽然只搜索一条数据,但是因为没有用到指定的索引,所以不会使用const.继续看下面这个

    explain SELECT * FROM `asj_admin_log` where log_id = 111

    log_id是主键,所以使用了const。所以说可以理解为const是最优化的

    4.3 eq_ref

    对于eq_ref的解释,mysql手册是这样说的:"对于每个来自于前面的表的行组合,从该表中读取一行。这可能是最好的联接类型,除了const类型。它用在一个索引的所有部分被联接使用并且索引是UNIQUE或PRIMARY KEY"。eq_ref可以用于使用=比较带索引的列。看下面的语句

    explain select * from uchome_spacefield,uchome_space where uchome_spacefield.uid = uchome_space.uid

    得到的结果是下图所示。很明显,mysql使用eq_ref联接来处理uchome_space表。

    目前的疑问:

           4.3.1 为什么是只有uchome_space一个表用到了eq_ref,并且sql语句如果变成

           explain select * from uchome_space,uchome_spacefield where uchome_space.uid = uchome_spacefield.uid

           结果还是一样,需要说明的是uid在这两个表中都是primary

    4.4 ref 对于每个来自于前面的表的行组合,所有有匹配索引值的行将从这张表中读取。如果联接只使用键的最左边的前缀,或如果键不是UNIQUE或PRIMARY KEY(换句话说,如果联接不能基于关键字选择单个行的话),则使用ref。如果使用的键仅仅匹配少量行,该联接类型是不错的。

    看下面这条语句 explain select * from uchome_space where uchome_space.friendnum = 0,得到结果如下,这条语句能搜出1w条数据

    4.5 ref_or_null 该联接类型如同ref,但是添加了MySQL可以专门搜索包含NULL值的行。在解决子查询中经常使用该联接类型的优化。

    上面这五种情况都是很理想的索引使用情况

    4.6 index_merge 该联接类型表示使用了索引合并优化方法。在这种情况下,key列包含了使用的索引的清单,key_len包含了使用的索引的最长的关键元素。

    4.7 unique_subquery 

    4.8 index_subquery

    4.9 range 给定范围内的检索,使用一个索引来检查行。看下面两条语句

    explain select * from uchome_space where uid in (1,2)

    explain select * from uchome_space where groupid in (1,2)

    uid有索引,groupid没有索引,结果是第一条语句的联接类型是range,第二个是ALL.以为是一定范围所以说像 between也可以这种联接,很明显

    explain select * from uchome_space where friendnum = 17

    这样的语句是不会使用range的,它会使用更好的联接类型就是上面介绍的ref

    4.10 index     该联接类型与ALL相同,除了只有索引树被扫描。这通常比ALL快,因为索引文件通常比数据文件小。(也就是说虽然all和Index都是读全表,但index是从索引中读取的,而all是从硬盘中读的)

    当查询只使用作为单索引一部分的列时,MySQL可以使用该联接类型。

    4.11  ALL  对于每个来自于先前的表的行组合,进行完整的表扫描。如果表是第一个没标记const的表,这通常不好,并且通常在它情况下差。通常可以增加更多的索引而不要使用ALL,使得行能基于前面的表中的常数值或列值被检索出。

    5 possible_keys 提示使用哪个索引会在该表中找到行,不太重要

    6 keys MYSQL使用的索引,简单且重要

    7 key_len MYSQL使用的索引长度

    8 ref   ref列显示使用哪个列或常数与key一起从表中选择行。

    9 rows 显示MYSQL执行查询的行数,简单且重要,数值越大越不好,说明没有用好索引

    10 Extra  该列包含MySQL解决查询的详细信息。

    10.1 Distinct     MySQL发现第1个匹配行后,停止为当前的行组合搜索更多的行。一直没见过这个值

    10.2 Not exists  

    10.3 range checked for each record

    没有找到合适的索引

    10.4 using filesort    

    MYSQL手册是这么解释的“MySQL需要额外的一次传递,以找出如何按排序顺序检索行。通过根据联接类型浏览所有行并为所有匹配WHERE子句的行保存排序关键字和行的指针来完成排序。然后关键字被排序,并按排序顺序检索行。”目前不太明白

    10.5 using index 只使用索引树中的信息而不需要进一步搜索读取实际的行来检索表中的信息。这个比较容易理解,就是说明是否使用了索引

    explain select * from ucspace_uchome where uid = 1的extra为using index(uid建有索引)

    explain select count(*) from uchome_space where groupid=1 的extra为using where(groupid未建立索引)

    10.6 using temporary

    为了解决查询,MySQL需要创建一个临时表来容纳结果。典型情况如查询包含可以按不同情况列出列的GROUP BY和ORDER BY子句时。

    出现using temporary就说明语句需要优化了,举个例子来说

    EXPLAIN SELECT ads.id FROM ads, city WHERE   city.city_id = 8005   AND ads.status = 'online'   AND city.ads_id=ads.id ORDER BY ads.id desc

    id  select_type  table   type    possible_keys   key      key_len  ref                     rows  filtered  Extra                          
    ------  -----------  ------  ------  --------------  -------  -------  --------------------  ------  --------  -------------------------------
         1  SIMPLE       city    ref     ads_id,city_id  city_id  4        const                   2838    100.00  Using temporary; Using filesort
         1  SIMPLE       ads     eq_ref  PRIMARY         PRIMARY  4        city.ads_id       1    100.00  Using where   

    这条语句会使用using temporary,而下面这条语句则不会

    EXPLAIN SELECT ads.id FROM ads, city WHERE   city.city_id = 8005   AND ads.status = 'online'   AND city.ads_id=ads.id ORDER BY city.ads_id desc

    id  select_type  table   type    possible_keys   key      key_len  ref                     rows  filtered  Extra                      
    ------  -----------  ------  ------  --------------  -------  -------  --------------------  ------  --------  ---------------------------
         1  SIMPLE       city    ref     ads_id,city_id  city_id  4        const                   2838    100.00  Using where; Using filesort
         1  SIMPLE       ads    eq_ref  PRIMARY         PRIMARY  4        city.ads_id       1    100.00  Using where    

    这是为什么呢?他俩之间只是一个order by不同,MySQL 表关联的算法是 Nest Loop Join,是通过驱动表的结果集作为循环基础数据,然后一条一条地通过该结果集中的数据作为过滤条件到下一个表中查询数据,然后合并结果。EXPLAIN 结果中,第一行出现的表就是驱动表(Important!)以上两个查询语句,驱动表都是 city,如上面的执行计划所示!

    对驱动表可以直接排序,对非驱动表(的字段排序)需要对循环查询的合并结果(临时表)进行排序(Important!)
    因此,order by ads.id desc 时,就要先 using temporary 了!
    驱动表的定义
    wwh999 在 2006年总结说,当进行多表连接查询时, [驱动表] 的定义为:
    1)指定了联接条件时,满足查询条件的记录行数少的表为[驱动表];
    2)未指定联接条件时,行数少的表为[驱动表](Important!)。

    永远用小结果集驱动大结果集

    今天学到了一个很重要的一点:当不确定是用哪种类型的join时,让mysql优化器自动去判断,我们只需写select * from t1,t2 where t1.field = t2.field

    10.7 using where

    WHERE子句用于限制哪一个行匹配下一个表或发送到客户。除非你专门从表中索取或检查所有行,如果Extra值不为Using where并且表联接类型为ALL或index,查询可能会有一些错误。(这个说明不是很理解,因为很多很多语句都会有where条件,而type为all或index只能说明检索的数据多,并不能说明错误,useing where不是很重要,但是很常见)

    如果想要使查询尽可能快,应找出Using filesort 和Using temporary的Extra值。

    10.8 Using sort_union(...), Using union(...),Using intersect(...)

    这些函数说明如何为index_merge联接类型合并索引扫描

    10.9 Using index for group-by

    类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查询GROUP BY或DISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。并且,按最有效的方式使用索引,以便对于每个组,只读取少量索引条目。

    实例讲解

    通过相乘EXPLAIN输出的rows列的所有值,你能得到一个关于一个联接如何的提示。这应该粗略地告诉你MySQL必须检查多少行以执行查询。当你使用max_join_size变量限制查询时,也用这个乘积来确定执行哪个多表SELECT语句。

  • 相关阅读:
    推送注意事项
    如何实现推送
    SVPRogressHUD问题小总结
    Flurry统计库包含出错处理
    python报"TypeError: object of type 'Greenlet' has no len()"
    python运行不报错又无任何结果输出
    python报"IndentationError: unexpected indent"
    windows环境将系统U盘还原
    windows2007环境Python3.5下paramiko模块的安装
    Python基础九--hashlib、hmac模块
  • 原文地址:https://www.cnblogs.com/hechunhua/p/4082754.html
Copyright © 2011-2022 走看看