6.8.3.9 使用 Hint 调整 Join Shuffle 方式¶
1 工作原理¶
Doris 支持使用 Hint 来调整 Join 操作中数据 Shuffle 的类型,从而优化查询性能。本节将详细介绍如何在 Doris 中利用 Hint 来指定 Join Shuffle 的类型。
目前, Doris 仅限于指定 Join 右表的 Distribute Type ,并且仅提供两种类型供选择: [shuffle] 和 [broadcast] 。 Distribute Type 需置于 Join 右表之前,可采用中括号 [] 的方式。
示例如下:
| SQL | |
|---|---|
1 2 3 4 | |
在使用时,需注意以下事项:
-
若遇到无法正确生成执行计划的
DistributeHint时,Doris不会显示该Hint,而是会按“最大努力”原则使其生效。最终,以EXPLAIN显示的Distribute方式为准。 -
在当前版本中,
DistributeHint暂不支持与LEADING混用,且仅当Distribute指定的表位于Join右边时,Hint才会生效。 -
建议将
DistributeHint与ORDERED混用。首先利用ORDERED固定Join顺序,然后再指定相应Join中预期使用的Distribute方式。
2 调优案例¶
接下来,我们将通过同一个例子来展示 Hint 的使用方法:
-
使用前:
SQL 1EXPLAIN SHAPE PLAN SELECT COUNT(*) FROM t1 JOIN t2 ON t1.c1 = t2.c2;结果:
SQL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+----------------------------------------------------------------------------------+ | Explain String (Nereids Planner) | +----------------------------------------------------------------------------------+ | PhysicalResultSink | | --hashAgg [GLOBAL] | | ----PhysicalDistribute [DistributionSpecGather] | | ------hashAgg [LOCAL] | | --------PhysicalProject | | ----------hashJoin [INNER_JOIN] hashCondition=((t1.c1 = t2.c2)) otherCondition=()| | ------------PhysicalProject | | --------------PhysicalOlapScan [t1] | | ------------PhysicalDistribute [DistributionSpecHash] | | --------------PhysicalProject | | ----------------PhysicalOlapScan [t2] | +----------------------------------------------------------------------------------+ -
使用后:
SQL 1EXPLAIN SHAPE PLAN SELECT /*+ ordered */ COUNT(*) FROM t2 JOIN [broadcast] t1 ON t1.c1 = t2.c2;结果:
SQL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
+----------------------------------------------------------------------------------+ | Explain String (Nereids Planner) | +----------------------------------------------------------------------------------+ | PhysicalResultSink | | --hashAgg [GLOBAL] | | ----PhysicalDistribute [DistributionSpecGather] | | ------hashAgg [LOCAL] | | --------PhysicalProject | | ----------hashJoin [INNER_JOIN] hashCondition=((t1.c1 = t2.c2)) otherCondition=()| | ------------PhysicalProject | | --------------PhysicalOlapScan [t2] | | ------------PhysicalDistribute [DistributionSpecReplicated] | | --------------PhysicalProject | | ----------------PhysicalOlapScan [t1] | | | | Hint log: | | Used: ORDERED | | UnUsed: | | SyntaxError: | +----------------------------------------------------------------------------------+在
EXPLAIN结果中,可以看到Distribute算子的相关信息:-
DistributionSpecReplicated表示将对应的数据复制到所有BE节点。 -
DistributionSpecGather表示将数据Gather到FE节点。 -
DistributionSpecHash表示将数据按照特定的HashKey和算法打散到不同的BE节点。
-
3 总结¶
通过合理使用 DistributeHint ,可以优化 Join 操作的 Shuffle 方式,提升查询性能。在实践中,建议先通过 EXPLAIN 分析查询执行计划,再根据实际情况选择合适的 Shuffle 类型。在使用时,需注意以下事项:
-
若遇到无法正确生成执行计划的
DistributeHint时,Doris不会显示该Hint,而是会按“最大努力”原则使其生效。最终,以EXPLAIN显示的Distribute方式为准。 -
在当前版本中,
DistributeHint暂不支持与LEADING混用,且仅当Distribute指定的表位于Join右边时,Hint才会生效。 -
建议将
DistributeHint与ORDERED混用。首先利用ORDERED固定Join顺序,然后再指定相应Join中预期使用的Distribute方式。