MySQL 中的嵌套循环

发布于 2024-10-12 15:52:30 字数 342 浏览 7 评论 0原文

此 O'Reilly 演示文稿中,有一段介绍了理解 MySQL 的 EXPLAIN 的一些关键概念:

什么是 JOIN?

  • 一切都是 JOIN,因为 MySQL 总是使用嵌套循环
  • 即使是单表 SELECT、UNION 或子查询

谁能解释这对于单表 SELECT 是如何工作的?

In this O'Reilly presentation, there is a paragraph introducing some key concepts for understanding MySQL's EXPLAIN:

What is a JOIN?

  • Everything is a JOIN, because MySQL always uses nested-loops
  • Even a single-table SELECT or a UNION or a subquery

Can anyone explain how this works for a single table SELECT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2024-10-19 15:52:30

嵌套循环是处理连接的一种方式:

for each row of table A
  if this row matches where clauses
    for each row of joined table B
      if this row matches where clauses and join clauses
        accept row
      end
    end
  end
end

可以通过索引进行相当多的优化,通过“针对某个索引中键 K 处找到的每一行”而不是“表 A 的每一行”,对于表 B 也是如此。

演示文稿说这是 MySQL 处理连接的唯一方式。还有其他方法可以使用,但 MySQL 没有实现它们。此 OraFAQ 条目提供了 Oracle 实现的几个: http://www.orafaq.com/tuningguide/ join%20methods.html 类似: http://oracle-online-help.blogspot.com/2007/03/nested-loops-hash-join-and-sort-merge.html

“一切都是联接”是我相信这只是一个实施细节。其实没那么重要。

Nested loops is one way of processing joins:

for each row of table A
  if this row matches where clauses
    for each row of joined table B
      if this row matches where clauses and join clauses
        accept row
      end
    end
  end
end

That can be optimized with indexes quite a bit, by doing "for each row found at key K in some index" instead of "each row of table A", and the same with table B.

The presentation is saying this is the only way MySQL processes joins. There are other methods than can be used, but MySQL doesn't implement them. This OraFAQ entry gives several that Oracle implements: http://www.orafaq.com/tuningguide/join%20methods.html Similarly: http://oracle-online-help.blogspot.com/2007/03/nested-loops-hash-join-and-sort-merge.html

"Everything is a join" is just an implementation detail, I believe. Not really that important.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文