Skip to main content

Control Join Distribution with Distribute Hint

Overview​

Distribute Hint controls how the right-table data is distributed (shuffled) in a Join operation, and is a common way to manually tune the Join execution plan. By explicitly specifying the distribution method, you can flexibly intervene in the query execution plan when the optimizer's automatic choice is not ideal.

Core capabilities:

  • Force the distribution method of the Join right table to be shuffle or broadcast.
  • Combine with Ordered Hint and Leading Hint for more fine-grained Join tuning.
  • A valid hint can be unused when no legal plan satisfies it. Verify the actual distribution in EXPLAIN.

Quick navigation​

Syntax rules​

RuleDescription
PositionDistribute Hint is written before the Join right table
Optional types[shuffle] or [broadcast]
Placement per joinSpecify the requested distribution at each applicable join.
Failure handlingA valid hint can be unused when no legal plan satisfies it. Check the distribution in EXPLAIN. Malformed SQL can still raise a parsing error.

Distribution method description​

EXPLAIN SHAPE PLAN displays the distribution type of a distribute operator. The meanings are as follows:

Distribution TypeMeaning
DistributionSpecReplicatedReplicates the corresponding data to all BE nodes (Broadcast distribution)
DistributionSpecGatherGathers the data into one execution instance
DistributionSpecHashDistributes the data to different BE nodes by a specific hashKey and algorithm (Shuffle distribution)

The first example uses t1 and t2 from Prepare the example tables. The second uses the standard TPC-H schema (part, supplier, lineitem, partsupp, orders, and nation). Run it only in a database containing those tables. Plans are illustrative and can vary with data, statistics, and engine version.

Use cases​

Combine with an ordered hint​

Scenario: First use Ordered Hint to fix the Join order to the textual order, and then use Distribute Hint to specify the desired distribution method for each Join.

Before (default plan):

mysql> explain shape plan select count(*) from t1 join t2 on t1.c1 = t2.c2;
+----------------------------------------------------------------------------------+
| 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] |
+----------------------------------------------------------------------------------+

After (specifying Broadcast distribution):

mysql> explain shape plan select /*+ ordered */ count(*) from t2 join[broadcast] t1 on t1.c1 = t2.c2;
+----------------------------------------------------------------------------------+
| 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: |
+----------------------------------------------------------------------------------+

In the hinted plan, t1 is the right input and has DistributionSpecReplicated, so it is broadcast. The unhinted example uses a different input order and hashes t2. Compare these operator labels rather than treating the two plans as a change to the same input.

Combine with a leading hint​

Scenario: Use a LEADING hint with shuffle and broadcast tokens to request join order and distribution. A hint does not control every optimizer decision.

explain shape plan
select
nation,
o_year,
sum(amount) as sum_profit
from
(
select
/*+ leading(orders shuffle {lineitem shuffle part} shuffle {supplier broadcast nation} shuffle partsupp) */
n_name as nation,
extract(year from o_orderdate) as o_year,
l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
from
part,
supplier,
lineitem,
partsupp,
orders,
nation
where
s_suppkey = l_suppkey
and ps_suppkey = l_suppkey
and ps_partkey = l_partkey
and p_partkey = l_partkey
and o_orderkey = l_orderkey
and s_nationkey = n_nationkey
and p_name like '%green%'
) as profit
group by
nation,
o_year
order by
nation,
o_year desc;

FAQ​

Q1: What happens if a Distribute Hint is written incorrectly or the corresponding plan cannot be generated?

A valid hint might be unused if the requested distribution cannot produce a legal plan. Check the distribution shown by EXPLAIN. Invalid SQL syntax can still raise a parsing error, so do not rely on malformed hints being ignored.

Q2: When should you use shuffle, and when should you use broadcast?

  • broadcast: Use this when the right-table data volume is small and the cost of replicating it to all BE nodes is lower than the cost of Shuffle.
  • shuffle: Use this when both tables have a large data volume and redistributing by hashKey is more efficient.

Q3: Can you specify the distribution method for multiple Joins at once?

Yes. Put a distribution hint at each applicable join. Combine it with an ordered or leading hint when you also need to request join order.

Summary​

Distribute Hint is a common Hint for controlling the Join Shuffle method, and is used to manually specify the shuffle or broadcast distribution method. Reasonable use of Distribute Hint can meet on-site tuning requirements for the Join Shuffle method and improve the flexibility of system control.

Ask AI