Distribute Hint
更新时间:2025-05-29
概述
Distribute hint 用来控制 join 的 shuffle 方式。
语法
- 支持指定右表的 Distribute Type,分为
[shuffle]
和[broadcast]
两种,需写在 Join 右表前面。 - 支持任意个 Distribute Hint。
- 当遇到无法正确生成计划的 Distribute Hint 时,系统不会显示错误,会按最大努力原则生效,最终以 EXPLAIN 显示的 Distribute 方式为准。
案例
与 Ordered Hint 混用
把 Join 顺序固定为文本序,然后再指定相应的 Join 预期使用的 Distribute 方式。例如:
使用前:
SQL
1mysql> explain shape plan select count(*) from t1 join t2 on t1.c1 = t2.c2;
2 +----------------------------------------------------------------------------------+
3 | Explain String(Nereids Planner) |
4 +----------------------------------------------------------------------------------+
5 | PhysicalResultSink |
6 | --hashAgg[GLOBAL] |
7 | ----PhysicalDistribute[DistributionSpecGather] |
8 | ------hashAgg[LOCAL] |
9 | --------PhysicalProject |
10 | ----------hashJoin[INNER_JOIN] hashCondition=((t1.c1 = t2.c2)) otherCondition=() |
11 | ------------PhysicalProject |
12 | --------------PhysicalOlapScan[t1] |
13 | ------------PhysicalDistribute[DistributionSpecHash] |
14 | --------------PhysicalProject |
15 | ----------------PhysicalOlapScan[t2] |
16 +----------------------------------------------------------------------------------+
使用后:
SQL
1mysql> explain shape plan select /*+ ordered */ count(*) from t2 join[broadcast] t1 on t1.c1 = t2.c2;
2+----------------------------------------------------------------------------------+
3| Explain String(Nereids Planner) |
4+----------------------------------------------------------------------------------+
5| PhysicalResultSink |
6| --hashAgg[GLOBAL] |
7| ----PhysicalDistribute[DistributionSpecGather] |
8| ------hashAgg[LOCAL] |
9| --------PhysicalProject |
10| ----------hashJoin[INNER_JOIN] hashCondition=((t1.c1 = t2.c2)) otherCondition=() |
11| ------------PhysicalProject |
12| --------------PhysicalOlapScan[t2] |
13| ------------PhysicalDistribute[DistributionSpecReplicated] |
14| --------------PhysicalProject |
15| ----------------PhysicalOlapScan[t1] |
16| |
17| Hint log: |
18| Used: ORDERED |
19| UnUsed: |
20| SyntaxError: |
21+----------------------------------------------------------------------------------+
Explain Shape Plan 里面会显示 Distribute 算子相关的信息。其中:
DistributionSpecReplicated
表示该算子将对应的数据复制到所有 BE 节点;DistributionSpecGather
表示将数据 Gather 到 FE 节点;DistributionSpecHash
表示将数据按照特定的 hashKey 以及算法打散到不同的 BE 节点。
与 Leading Hint 混用
在编写 SQL 查询时,可以在使用 LEADING
提示的同时,为每个 JOIN
操作指定相应的 DISTRIBUTE
方式。以下是一个具体的例子,展示了如何在 SQL 查询中混合使用 Distribute Hint
和 Leading Hint
。
SQL
1explain shape plan
2 select
3 nation,
4 o_year,
5 sum(amount) as sum_profit
6 from
7 (
8 select
9 /*+ leading(orders shuffle {lineitem shuffle part} shuffle {supplier broadcast nation} shuffle partsupp) */
10 n_name as nation,
11 extract(year from o_orderdate) as o_year,
12 l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
13 from
14 part,
15 supplier,
16 lineitem,
17 partsupp,
18 orders,
19 nation
20 where
21 s_suppkey = l_suppkey
22 and ps_suppkey = l_suppkey
23 and ps_partkey = l_partkey
24 and p_partkey = l_partkey
25 and o_orderkey = l_orderkey
26 and s_nationkey = n_nationkey
27 and p_name like '%green%'
28 ) as profit
29 group by
30 nation,
31 o_year
32 order by
33 nation,
34 o_year desc;
总结
Distribute hint 是常用的控制 join shuffle 方式的 hint,用于手工指定 shuffle 或者 broadcast 分发方式。使用好 Distribute hint 能够满足现场针对 join shuffle 方式的调优需求,增加系统控制的灵活性。