了解EXPLAIN mysql

时间:2015-06-04 10:21:28

标签: mysql 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

    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,第二种情况是子查询: ![第一次和第二次查询] [1]

你可以看到差异是前两行的顺序,我的问题特别是在第二行(更快的查询) 第二行应该是内部表,但如果是这样,为什么列ref告诉我:max.dmess,它应该是ref-table(子查询)的列。 并且,最后一行是关于如何构建第一行的? 最后你认为有一个更有效的查询?

0 个答案:

没有答案