暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

pg_hint_plan技术内幕--03 Join method

原创 NickYoung 2024-11-06
198

前言

pg_hint_plan技术内幕–02 Scan method中我们介绍了scan method的原理,本文我们继续深入探究Join method的原理。

目前支持的join method list如下:

Join method NestLoop(table table[ table…]) Forces nested loop for the joins on the tables specified.
HashJoin(table table[ table…]) Forces hash join for the joins on the tables specified.
MergeJoin(table table[ table…]) Forces merge join for the joins on the tables specified.
NoNestLoop(table table[ table…]) Forces to not do nested loop for the joins on the tables specified.
NoHashJoin(table table[ table…]) Forces to not do hash join for the joins on the tables specified.
NoMergeJoin(table table[ table…]) Forces to not do merge join for the joins on the tables specified.

主要是对NestLoop HashJoin MergeJoin三种join方式的控制。

原理

以MergeJoin(table table[ table…])为例:
在make_rel_from_joinlist时进入hook, set_join_config_options将hint指定的enable_mergejoin 置为on,并将其他scan method的startup_cost配置为disable_cost(即set NestLoop to off,set HashJoin to off) ,生成Mergejoin和Nestloop的joinpath, 最终计算最小代价确定joinpath为MergeJoin。
image.png

如图,sql默认走NestLoop,我们使用hint /*+ MergeJoin (du pu) */后走了MergeJoin。
image.png

debug关键过程做下验证:

首先set_join_config_options中:
enforce_mask=2 即set merge_join to on,set enable_nestloop to off,set enable_hashjoin to off
即Nestloop和Hashjoin的startup_cost为disable_cost
image.png

add_paths_to_joinrel :
mergejoin_allow总为true,为joinrel设置MergeJoin和Nestloop的path
image.png

enable_hashjoin为false,不涉及Hashjoin的path
image.png

由于Nestloop的startup_cost被设置为disable_cost,因此set_cheapest(rel)为T_MergePath
image.png

小结

本文深入分析了Join method的原理,其实就是对hint指定的几个表,将hint之外的join方式的startup_cost配置为disable_cost,这样最小代价就是hint指定的join方式了。

最后修改时间:2024-12-01 00:16:26
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论