【问题标题】:In a PostgreSQL query plan, what is the difference between Materialize and Hash?在 PostgreSQL 查询计划中,Materialize 和 Hash 有什么区别?
【发布时间】:2016-12-15 05:33:43
【问题描述】:

这是 an example 使用 Materialize 的查询计划:

Nested Loop  (cost=4.65..49.46 rows=33 width=488)
   Join Filter: (t1.hundred < t2.hundred)
   ->  ... (outer)
   ->  Materialize  (cost=0.29..8.51 rows=10 width=244)
         ->  ... (inner)

下面是一个带有 Hash 的查询计划示例:

Hash Join  (cost=230.47..713.98 rows=101 width=488)
   Hash Cond: (t2.unique2 = t1.unique2)
   ->  ... (outer)
   ->  Hash  (cost=229.20..229.20 rows=101 width=244)
         ->  ... (inner)

在第二个示例中,内部查询的结果被加载到哈希表中,并且哈希连接将为每个外部行进行哈希表查找。

在第一个示例中,内部的结果加载到内存中(这就是 Materialize 的意思,不是吗?),并且对于每个外部行,嵌套循环连接也必须执行在物化数据中查找正确的行。它使用什么样的数据结构? (显然不是哈希表。)

【问题讨论】:

  • 查看来源:postgres-9.5/src/backend/executor/nodeMaterial.c
  • 看来物化节点使用的是普通的元组存储,而hashjoin需要分配+管理额外的内存来存储完整的hash机+状态。 (只需比较来源的文件大小......)
  • @joop 确实,查看源代码回答了我的问题。谢谢。

标签: postgresql explain sql-execution-plan


【解决方案1】:

在咨询the source 之后,我发现 Materialize 基本上只是一个连续的行缓存(一个元组存储),它会不断地为每个外部行重绕和迭代。

【讨论】:

    猜你喜欢
    • 2015-04-17
    • 2011-12-31
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2016-12-11
    • 2011-03-03
    相关资源
    最近更新 更多