Use hints to adjust join shuffle methods
ApsaraDB for SelectDB automatically selects a join data distribution strategy (Shuffle or Broadcast) based on table statistics. When statistics are stale or data volume changes significantly after filtering, you can override the optimizer's choice by using bracket-syntax hints.
Join data distribution methods
Distributed joins require data from different Backend (BE) nodes to be co-located. ApsaraDB for SelectDB supports the following distribution methods:
|
Method |
Mechanism |
Best for |
EXPLAIN output |
|
Shuffle |
Hash-redistributes both sides of the join by the join key |
Joining two large tables |
|
|
Broadcast |
Sends the entire right table to every node holding left table data |
Large table joined with a small table |
|
|
Colocate |
Data already resides on the same nodes; no network transfer needed |
Tables in the same Colocate Group |
|
By default, the optimizer chooses Broadcast when the right table is below a size threshold, and Shuffle otherwise. If both tables belong to the same Colocate Group, Colocate Join takes priority over both.
When to override the optimizer
The automatic selection may be suboptimal in the following situations:
-
Stale statistics: Table data volume has changed significantly but statistics have not been refreshed, causing the optimizer to misjudge table sizes.
-
Large data reduction after filtering: The right table has a large raw row count, but a highly selective WHERE clause reduces it to a few hundred rows. The optimizer picks Shuffle based on the raw count, but Broadcast would be faster.
-
Broadcast causes OOM: The right table is selected for Broadcast but is larger than expected, causing out-of-memory errors. Forcing Shuffle avoids this issue.
Hint syntax
Place a bracket hint [] after the JOIN keyword to specify the distribution method. Two hints are supported: [shuffle] and [broadcast].
Force Shuffle Join
-- Force Shuffle Join
SELECT *
FROM orders o JOIN [shuffle] users u ON o.user_id = u.user_id
WHERE o.order_date >= '2024-01-01';
Force Broadcast Join
-- Force the right table to be broadcast to all nodes
SELECT *
FROM orders o JOIN [broadcast] dim_city c ON o.city_id = c.city_id
WHERE o.order_date >= '2024-01-01';
Hints have higher priority than the optimizer's automatic selection but lower priority than Colocate. If both tables belong to the same Colocate Group and the join key is the bucket key, Colocate Join is used regardless of any [shuffle] or [broadcast] hint.
Examples
Force Broadcast for a post-filter small table
The products table has many rows, but a highly selective filter reduces the result to just a handful. The optimizer picks Shuffle based on the raw row count, but Broadcast is faster for the actual result size:
-- Post-filter products data is tiny; Broadcast is more efficient
SELECT o.*, p.product_name
FROM orders o
JOIN [broadcast] products p ON o.product_id = p.product_id
WHERE p.category = 'limited_edition' AND p.status = 'active';
Avoid Broadcast OOM
Statistics show the right table is small, so the optimizer picks Broadcast. In reality the table has grown well beyond the statistics, and broadcasting it causes OOM. Force Shuffle instead:
-- Statistics are stale and Broadcast causes OOM; force Shuffle
SELECT o.order_id, u.username
FROM orders o
JOIN [shuffle] users u ON o.user_id = u.user_id;
Specify different methods per join
In a multi-table join, you can specify a different distribution method for each join independently:
-- Different distribution methods for each join
SELECT o.order_id, u.username, p.product_name
FROM orders o
JOIN [shuffle] users u ON o.user_id = u.user_id
JOIN [broadcast] products p ON o.product_id = p.product_id;
Verify hint effectiveness
Run EXPLAIN and check the join op field to confirm the hint is in effect:
EXPLAIN SELECT o.*, u.username
FROM orders o JOIN [broadcast] users u ON o.user_id = u.user_id;
-- Look for these in the output:
-- join op: INNER JOIN(BROADCAST)[] means Broadcast is active
-- join op: INNER JOIN(PARTITIONED)[] means Shuffle is active
-- The Hint log at the bottom shows:
-- Used: [broadcast]_1 means the hint was accepted
If the Hint log shows the hint under UnUsed or SyntaxError, the hint did not take effect.
Best practices
-
Run
ANALYZE TABLEfirst to refresh statistics. Accurate statistics let the optimizer make the right call without hints. -
Hints are hard-coded and may become counterproductive as data distributions change. Review them periodically.
-
Before adding a hint, use EXPLAIN to compare the default plan with the hinted plan and confirm the hint actually improves performance.
-
Test hint effects in a non-production environment before applying them to production SQL.