我试图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
Run Code Online (Sandbox Code Playgroud)
这是第一种查询类型:
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 ;
Run Code Online (Sandbox Code Playgroud)
这是第二个:
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 ;
Run Code Online (Sandbox Code Playgroud)
这两个查询是同一个问题,唯一的区别是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
Run Code Online (Sandbox Code Playgroud)
第二个结果:
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
Run Code Online (Sandbox Code Playgroud)
正如您所看到的差异是前两行的顺序,我的问题特别是在第二行(更快的查询)第二行应该是内部表,但如果是这样,为什么列ref告诉我:max.dmess,这应该是ref-table(子查询)的列.
最后一行是关于如何构建第一行的吗?最后,你认为有一个更有效的查询?
我认为答案是表扫描与主键。如果您看到,通过第一个查询,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
Run Code Online (Sandbox Code Playgroud)
但是您在“衍生2”表上使用“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
Run Code Online (Sandbox Code Playgroud)
该键不存在,因此 MySQL 应该创建它。
如果您采用第二个示例,则全表扫描发生在“衍生2”表上,并且“text_mess”中的主键正在使用中:
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
Run Code Online (Sandbox Code Playgroud)
答案是,在这种情况下MySQL决定创建并使用索引,但通常它会进行全表扫描,而且速度更快。
想象一下auto_key0键再次包含该子查询中唯一的一列。这个额外的过程是没有必要的。这就是为什么你的第二个查询更快。
| 归档时间: |
|
| 查看次数: |
1461 次 |
| 最近记录: |