Skip to main content

Adjust Join Distribution with Hints

A distribute hint requests a distribution method for the right input of a join. Use it to compare an alternative plan when the optimizer's choice causes excessive data transfer or memory use.

Pre-tuning checklist:

  • You have used EXPLAIN SHAPE PLAN to inspect the current Join distribution mode.
  • You have confirmed that the default plan has a performance bottleneck (for example, a small table being shuffled or a large table being broadcast).
  • You understand the data sizes of both tables and can judge whether Broadcast or Shuffle is suitable.
  • You can compare the hinted and unhinted query under representative data and concurrency.

Warning:

Start with the optimizer's plan. A hint can improve one workload and regress another as data changes. Retain it only after comparing execution profiles, and review it after changes to data or the engine.

Distribute hint syntax​

VeloDB Cloud supports two independent Distribute Hints. They must be placed before the Join right table and wrapped in square brackets [].

Hint type comparison​

Hint TypeDistribution Mode (DistributionSpec)Typical Applicable ScenarioData Transfer Cost
[shuffle]DistributionSpecHashBoth inputs are large and can be distributed by the join keyRedistributes inputs that do not already satisfy the required hash distribution
[broadcast]DistributionSpecReplicatedThe right table is small and is replicated to every BE nodeThe right table is fully replicated to every BE node

Tip:

Combine a leading hint with a distribute hint to request both join order and distribution. See Control Join Order with Leading Hint.

Example 1: Request broadcast or shuffle​

Request broadcast or shuffle
-- Force the right table to use Broadcast distribution
SELECT COUNT(*) FROM t2 JOIN [broadcast] t1 ON t1.c1 = t2.c2;

-- Force the right table to use Shuffle distribution
SELECT COUNT(*) FROM t2 JOIN [shuffle] t1 ON t1.c1 = t2.c2;

Example 2: Verify the hint with EXPLAIN​

Verify the hint with `EXPLAIN`

The following example compares the same query with and without a hint. Create the sample t1 and t2 tables from the leading hint examples first. Plan output is illustrative and depends on data, statistics, and engine version.

Step 1: View the default execution plan​

Goal: Confirm the distribution mode chosen by the optimizer by default.

Command:

EXPLAIN SHAPE PLAN SELECT COUNT(*) FROM t1 JOIN t2 ON t1.c1 = t2.c2;

Description: In the default plan, t2 uses DistributionSpecHash, that is, Shuffle distribution by hash.

+----------------------------------------------------------------------------------+
| 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] |
+----------------------------------------------------------------------------------+

Step 2: Add the [broadcast] hint​

Goal: Change the distribution mode of the right table t2 to Broadcast.

Command:

EXPLAIN SHAPE PLAN SELECT COUNT(*) FROM t1 JOIN [broadcast] t2 ON t1.c1 = t2.c2;

Description: In the execution plan, the distribution mode of t2 changes from DistributionSpecHash to DistributionSpecReplicated, indicating that the hint has taken effect.

+----------------------------------------------------------------------------------+
| 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 [DistributionSpecReplicated] |
| --------------PhysicalProject |
| ----------------PhysicalOlapScan [t2] |
+----------------------------------------------------------------------------------+

FAQ​

Q1: Why does the hint not take effect after I add it?​

  • Make sure the hint is placed before the Join right table, for example JOIN [broadcast] t1, not before the left table.
  • Use EXPLAIN SHAPE PLAN to check whether the DistributionSpec of the PhysicalDistribute node matches your expectation.
  • Check the SQL syntax and hint position. A valid hint can be unused if no legal plan satisfies it. Malformed SQL, including missing brackets, can still fail to parse.

Q2: When should I use [broadcast] versus [shuffle]?​

ScenarioRecommended HintReason
The right table is small (such as a dimension table)[broadcast]Avoid the network overhead of shuffling the large table
Both tables are large with balanced data[shuffle]Broadcast amplifies the transfer cost of the right table
The join key has severe skew and the build input is small enough for every receiving nodeConsider [broadcast]Avoids hashing the skewed key on the large input, but duplicates build memory and transfer

Q3: Do I need to manually specify a hint?​

Start without a hint. The optimizer chooses a distribution mode from the available plans and statistics. Use a hint only when performance does not meet expectations and you have clearly determined that the optimizer's choice is suboptimal.

Summary​

By using Distribute Hint appropriately, you can optimize the shuffle mode of Join operations and improve query performance. In practice, it is recommended to first analyze the execution plan with EXPLAIN SHAPE PLAN, and then choose [shuffle] or [broadcast] based on the data size and distribution characteristics.

Ask AI