在mysql查询中理解EXPLAIN

时间:2015-06-04 11:54:02

标签: mysql explain

我试图在查询中解释explain的mysql(用两种不同的方式编写),这就是表:

    create table text_mess(
    datamess timestamp(3) DEFAULT 0,
    sender bigint ,
    recipient bigint ,
    roger boolean,
    msg char(255),
    foreign key(recipient) 
            references users (tel) 
                        on delete cascade
                        on update cascade,
primary key(datamess,sender)
)
engine = InnoDB

这是第一种类型的查询:

    EXPLAIN
    select /*!STRAIGHT_JOIN*/datamess, sender,recipient,roger,msg
    from text_mess join (select max(datamess)as dmess
                    from text_mess 
                    where roger = true
                    group by sender,recipient) as max
                    on text_mess.datamess=max.dmess ; 

这是第二个:

    EXPLAIN
    select /*!STRAIGHT_JOIN*/datamess, sender,recipient,roger,msg
    from  (select max(datamess)as dmess
                    from text_mess 
                    where roger = true
                    group by sender,recipient) as max
      join
    text_mess
    on max.dmess = text_mess.datamess ;

两个查询要求相同的事情,唯一的区别是ref_table(driving_table)的顺序,第一种情况是text_mess,第二种情况是子查询。 第一个结果:

    id  select_type     table       type    possible_keys   key     key_len      ref                            rows        Extra
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1   PRIMARY     text_mess       ALL     PRIMARY         null        null    null                            294225      null
    1   PRIMARY     derived2        ref     auto_key0       auto_key0   7       inextremis.text_mess.datamess   10          Using index
    2   DERIVED     text_mess       ALL     recipient       null        null    null                            294225      Using where; Using temporary; Using filesort

第二个结果:

    id  select_type     table       type    possible_keys   key          key_len    ref     rows    Extra
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1   PRIMARY      <derived2>     ALL     null            null         null      null     294225  Using where
    1   PRIMARY      text_mess      ref     PRIMARY         PRIMARY         6      max.dmess    1   null
    2   DERIVED      text_mess      ALL     recipient       null         null      null     294225  Using where; Using temporary; Using filesort

正如你所看到的差异是前两行的顺序,我的问题特别是在第二行(更快的查询) 第二行应该是内部表,但如果是这样,为什么列ref告诉我:max.dmess,它应该是ref-table(子查询)的列。

最后一行是如何构建第一行的? 最后,你认为有一个更有效的查询?

1 个答案:

答案 0 :(得分:0)

我认为答案是表扫描与主键。如果您看到,通过第一个查询,MySQL不使用任何键,而是准备好表格“text_mess”中的每一行:

    id  select_type     table       type    possible_keys   key     key_len      ref                            rows        Extra
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

     1   PRIMARY     text_mess       ALL     PRIMARY         null        null    null                            294225      null

但是你在“derived2”表上使用“ON”语句,MySQL将创建一个自动键:

    id  select_type     table       type    possible_keys   key     key_len      ref                            rows        Extra
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1   PRIMARY     derived2        ref     auto_key0       auto_key0   7       inextremis.text_mess.datamess   10          Using index

此密钥不存在,因此MySQL应该创建它。

如果您采用第二个示例,则在“derived2”表中进行全表扫描,并且正在使用“text_mess”中的PRIMARY KEY:

    id  select_type     table       type    possible_keys   key          key_len    ref     rows    Extra
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1   PRIMARY      <derived2>     ALL     null            null         null      null     294225  Using where
    1   PRIMARY      text_mess      ref     PRIMARY         PRIMARY         6      max.dmess    1   null

答案是,在这种情况下,MySQL决定创建并使用索引,但通常它会进行全表扫描,而且速度更快。

想象一下, auto_key0 键再次包含此子查询中的唯一一列。这个额外的过程不是必需的。这就是为什么你的第二个查询更快。