Use Colocate Group to optimize joins
Distributed joins normally require shuffling data across nodes, and the shuffle overhead grows as table sizes increase. Colocate Join eliminates this by co-locating frequently joined tables on the same nodes at table creation time, so that joins execute locally with no network transfer.
How Colocate Join works
A regular distributed join rehashes both tables by the join key and redistributes data across nodes. This shuffle is often the main performance bottleneck for large-table joins.
Colocate Join solves this at table creation time: if two tables use the same bucket key and bucket count, rows with the same key value are guaranteed to reside on the same Backend (BE) node. The join runs locally on each node with zero network transfer.
ApsaraDB for SelectDB manages co-location through Colocate Groups. Tables in the same group are guaranteed to:
-
Map identical hash values to the same BE node.
-
Rebalance bucket assignments in unison when nodes are added or removed.
Requirements
All tables in a Colocate Group must meet the following constraints. Violations cause the CREATE TABLE or ALTER TABLE statement to fail.
|
Constraint |
Requirement |
Details |
|
Bucket key |
Same data type and column count |
Column names can differ, but data types must match exactly (for example, INT and BIGINT are not compatible) |
|
Bucket count |
Identical |
All tables must specify the same BUCKETS value. |
A type mismatch produces an error such as Colocate tables distribution columns must have the same data type.
Create colocate tables
Set the colocate_with property when creating a table. Tables that specify the same value belong to the same group:
-- Orders table, bucketed by user_id, assigned to group_user
CREATE TABLE orders (
order_id BIGINT,
user_id BIGINT,
product_id BIGINT,
order_date DATE,
amount DECIMAL(10,2)
) DISTRIBUTED BY HASH(user_id) BUCKETS 4
PROPERTIES (
"colocate_with" = "group_user"
);
-- Users table, same bucket key and count, same group
CREATE TABLE users (
user_id BIGINT,
username VARCHAR(100),
city_id BIGINT,
vip_level INT
) DISTRIBUTED BY HASH(user_id) BUCKETS 4
PROPERTIES (
"colocate_with" = "group_user"
);
The group is created automatically when the first table specifies a new colocate_with value. Subsequent tables joining the group are validated against the existing constraints.
Check Colocate Group status
Run SHOW PROC '/colocation_group' to list all groups:
SHOW PROC '/colocation_group';
Sample output:
+-------------------------------+----------------------------+-----------+------------+-------------------+---------+----------+----------+
| GroupId | GroupName | TableIds | BucketsNum | ReplicaAllocation | DistCols| IsStable | ErrorMsg |
+-------------------------------+----------------------------+-----------+------------+-------------------+---------+----------+----------+
| 1778568404439.1778568404759 | test_query_opt.group_user | 10749,10776| 4 | tag.location.default:1| bigint | true | |
+-------------------------------+----------------------------+-----------+------------+-------------------+---------+----------+----------+
Key fields:
-
GroupName: Displayed as{database_name}.{group_name}. -
TableIds: IDs of all tables in the group. -
BucketsNum: The shared bucket count. -
IsStable:truemeans data placement is balanced and Colocate Join is available.falsemeans rebalancing is in progress (typically after a node change), and Colocate Join falls back to Shuffle Join.
Verify Colocate Join
When both tables belong to the same group and the join key is the bucket key, the optimizer automatically selects Colocate Join. Use EXPLAIN to confirm:
EXPLAIN SELECT o.order_id, u.username, o.amount
FROM orders o JOIN users u ON o.user_id = u.user_id
WHERE o.order_date >= '2024-01-01';
-- Look for the join op field in the output:
-- join op: INNER JOIN(COLOCATE[])[] means Colocate Join is active
-- If you see INNER JOIN(PARTITIONED) or INNER JOIN(BROADCAST), Colocate is not being used
Colocate Join falls back to Shuffle or Broadcast in these cases:
-
The join key is not the bucket key. For example, if orders and users are in the same group but the query joins on product_id, Colocate does not apply.
-
The group's
IsStableisfalse(rebalancing in progress). -
Colocate planning is disabled via the session variable:
SET disable_colocate_plan = true.
Manage Colocate Groups
Use ALTER TABLE to change a table's group assignment at any time:
-- Add a table to a group (creates the group if it does not exist)
ALTER TABLE orders SET ("colocate_with" = "group_user");
-- Move a table to a different group
ALTER TABLE orders SET ("colocate_with" = "group_new");
-- Remove a table from its group
ALTER TABLE orders SET ("colocate_with" = "");
The system validates the target group's constraints (bucket key types, bucket count) when you modify colocate_with. The operation fails if constraints are violated.
Dropped tables enter the recycle bin first. A group is not removed until the last table in it is permanently deleted from the recycle bin. Until then, the group's schema constraints (bucket key types, etc.) are preserved, and new tables must still satisfy them.
Typical scenarios
Star schema: fact table joins dimension tables
Place the fact table and high-frequency dimension tables in the same Colocate Group, using the join key as the bucket key:
-- Both orders and users are bucketed by user_id, so the join runs locally
SELECT u.username, SUM(o.amount) AS total_sales
FROM orders o
JOIN users u ON o.user_id = u.user_id
WHERE o.order_date BETWEEN '2024-01-01' AND '2024-06-30'
GROUP BY u.username;
Colocate Join only triggers when the join key matches the bucket key. If you also need to join on product_id with a products table, that join will not use Colocate. For multiple high-frequency join paths, create separate Colocate Groups for each join key.
Real-time analytics with multiple detail tables
When real-time ingested detail tables are frequently joined by user ID for analysis, bucket them all by user ID in the same group to avoid network shuffles during joins.
Best practices
-
Choose the most frequently joined column as the bucket key, not the partition column.
-
Size buckets appropriately. Too few causes data skew; too many increases metadata overhead. Aim for 100 MB to 1 GB of data per bucket.
-
Keep no more than 10 tables per Colocate Group.
-
After node scaling, monitor
IsStableand wait for it to return totruebefore running performance-sensitive queries.