Mysql 为什么使用group by的子查询执行两次完全扫描?
我正在测试两种类型的查询。 第一种类型如下所示:Mysql 为什么使用group by的子查询执行两次完全扫描?,mysql,sql,Mysql,Sql,我正在测试两种类型的查询。 第一种类型如下所示: explain select * from ord_order; explain select * from (select * from ord_order) as tbl; explain select * from ord_order group by bundle_or_order_number; explain select * from (select * from ord_order
explain select * from ord_order;
explain select * from (select * from ord_order) as tbl;
explain select * from ord_order
group by bundle_or_order_number;
explain select * from
(select * from ord_order
group by bundle_or_order_number) as tbl;
这两个执行计划显示完全扫描一次的相同行为
但是,第二种类型如下所示:
explain select * from ord_order;
explain select * from (select * from ord_order) as tbl;
explain select * from ord_order
group by bundle_or_order_number;
explain select * from
(select * from ord_order
group by bundle_or_order_number) as tbl;
第二个查询执行两次完整扫描
有人能解释一下吗?谢谢。首先,这无关紧要,因为您的查询格式不正确。不要将select*与group by一起使用。这毫无意义,而且MySQL的当前版本不支持它。问题是:这些列来自哪些行?您应该使用聚合函数 为什么这两个问题不同?在MySQL行话中,区别在于from子句中作为子查询的派生表是否被物化。子查询是否被物化取决于子查询的性质以及MySQL在您使用的版本中决定做什么
您可以在.中阅读有关优化派生表的内容,而MySQL的当前版本不支持它-不幸的是,它们仍然支持。更改的只是默认设置:@Shadow。不知何故,我认为使用这种结构的人还不足以理解服务器设置。。。我最喜欢的是一个家伙,他将group_concat缓冲区设置为超出物理内存的容量,当他的MySQL服务器崩溃时,他感到惊讶: