Colocation Join
Colocation Join can avoid shuffling both inputs of an equality join. Put tables with compatible hash distribution in the same colocation group, then verify that the optimizer chooses a local join for predicates on their distribution columns.
In VeloDB Cloud, persistent data is separate from compute. The service routes the same bucket of tables in a colocation group to the same compute node within a cluster. Each cluster has its own placement. Colocation does not give users control of permanent storage replicas or guarantee that every query runs without network transfer.
Check applicability
Before using colocation, confirm the following:
- The native tables belong to the same colocation group.
- Their hash distribution columns have compatible types in the same order, and their bucket counts match.
- The join equality predicates cover the distribution columns.
- The query profile shows that join data transfer is a bottleneck worth optimizing.
Use EXPLAIN to verify the chosen plan after changing group membership. A common group name is a prerequisite, not proof that a specific query uses colocation.
Terminology
| Term | Meaning |
|---|---|
| Colocation group | Tables whose matching buckets can be scheduled together within a cluster. |
| Group schema | Shared hash distribution column types and bucket count, plus internal metadata maintained by the service. |
| Bucket index | A bucket's position in the hash distribution, such as 0 through 7 for eight buckets. |
Principles
Hash distribution assigns rows to buckets from their distribution column values. Within a cluster, colocation uses the group identity and bucket index to select a compute node. Tables in the same group use the same selection for matching buckets. The mapping can change when the cluster's available nodes change.
A nonpartitioned table has one tablet per bucket. A partitioned table has a corresponding tablet for each bucket in each partition. Group members do not need identical partition counts, partition boundaries, or partition column types.
Distribution constraints
Use DISTRIBUTED BY HASH(...) for grouped tables. Match the number and order of distribution columns, their compatible types, and the bucket count. Matching column names alone is insufficient. VARCHAR and STRING distribution columns are compatible with one another in the checked Core implementation.
Storage replication is managed in VeloDB Cloud. Do not copy replication_num or replication_allocation settings from a self-managed example to control colocation placement.
Compute placement illustration
For a group with eight buckets and a cluster with nodes A, B, C, and D, a possible mapping is:
Bucket index: 0 1 2 3 4 5 6 7
Compute node: A B C D A B C D
The same bucket index from every member table uses the same mapping within this cluster. This is an illustration, not a promise of equal bucket sizes or an exact placement algorithm. Another cluster can use a different mapping.
Manage colocation groups
The creation snippets below are alternatives. Use distinct table names, or drop a previous test table before reusing its name. You need table creation and alteration privileges for the operations you run.
Assign a group when creating a table
Purpose: Add a newly created table to a specified Colocation Group.
Command: Specify "colocate_with" = "group_name" in PROPERTIES.
Example:
CREATE TABLE tbl (k1 int, v1 int sum)
DISTRIBUTED BY HASH(k1)
BUCKETS 8
PROPERTIES(
"colocate_with" = "group1"
);
Description:
- If the specified Group does not exist, VeloDB Cloud automatically creates a Group containing only the current table.
- If the group exists, the query engine checks that the table has compatible hash distribution. The table is added only when that validation succeeds. The service routes matching buckets to the same compute node within each cluster.
- A Group belongs to a Database, and the Group name is unique within a Database. In internal storage, the full name of a Group is
dbId_groupName, but users only seegroupName.
Create a group across databases
Purpose: Implement cross-Database Colocate Join.
Command: Use the keyword __global__ as the prefix of the Group name when creating the table.
Example:
CREATE TABLE tbl (k1 int, v1 int sum)
DISTRIBUTED BY HASH(k1)
BUCKETS 8
PROPERTIES(
"colocate_with" = "__global__group1"
);
Description: A Group with the __global__ prefix no longer belongs to a Database, and its name is globally unique. By creating a Global Group, you can implement cross-Database Colocate Join.
Drop a table
When the last table in a Group is completely deleted, the Group is also automatically deleted.
Note:
A dropped table can remain in the recycle bin before final deletion. Do not use group disappearance as an immediate indication that
DROP TABLEcompleted.
Inspect groups
Purpose: View information about existing Colocation Groups in the warehouse and their compute placement.
1. Inspect groups in the warehouse
SHOW PROC '/colocation_group';
The result includes the following fields. Column names and diagnostic output can vary by Core version.
Field descriptions:
| Field | Description |
|---|---|
GroupId | Group identifier used in subsequent SHOW PROC commands. |
GroupName | Full group name. |
TableIds | Member table identifiers. |
BucketsNum | Bucket count shared by the group. |
ReplicaAllocation | Internal replication metadata. This is null in VeloDB Cloud and is not a user-selected storage replica count. |
DistCols | Hash distribution column types. |
IsStable | Group metadata stability. This alone does not prove that a particular query uses a colocate plan. |
ErrorMsg | Group diagnostic message, when present. |
2. Inspect a group's compute placement
Replace 10005.10008 with a GroupId returned by the previous command:
SHOW PROC '/colocation_group/10005.10008';
BucketIndex identifies a bucket. In VeloDB Cloud, the result can have a separate placement column for each cluster, identified internally by its compute group name. The node identifiers describe compute placement in that cluster. They do not describe permanent storage replicas or a placement shared across all clusters.
Note:
These commands require administrator or equivalent diagnostic privileges. If access is denied, ask your warehouse administrator or VeloDB Cloud support for help.
Change a table's group
Purpose: Add an existing table to, migrate it within, or remove it from a Colocation Group.
1. Assign or change the group
ALTER TABLE tbl SET ("colocate_with" = "group2");
Behavior:
- If the table did not previously belong to any Group, this command checks the Schema and adds the table to the Group (the Group is created if it does not exist).
- If the table previously belonged to another Group, this command first removes the table from the original Group and then adds it to the new Group (the Group is created if it does not exist).
2. Remove group membership
ALTER TABLE tbl SET ("colocate_with" = "");
Add partitions
When adding partitions, keep their hash distribution compatible with the group, including the number of buckets. Cloud storage replication is managed by the service. Do not change replica allocation to repair or tune a group.
Verify the query plan
Query colocated tables with ordinary SQL. Group membership makes a local join possible, but the optimizer still checks join predicates and distribution compatibility. If colocation cannot be used, the plan can use another distribution method.
The following example shows how to inspect a join plan. You need privileges to create and query tables in the current database. The plan excerpts illustrate the engine's colocation markers and are not measurements from a Cloud warehouse. Operator names and formatting can vary by version.
Create the example tables
Table 1:
CREATE TABLE `tbl1` (
`k1` date NOT NULL COMMENT "",
`k2` int(11) NOT NULL COMMENT "",
`v1` int(11) SUM NOT NULL COMMENT ""
) ENGINE=OLAP
AGGREGATE KEY(`k1`, `k2`)
PARTITION BY RANGE(`k1`)
(
PARTITION p1 VALUES LESS THAN ('2019-05-31'),
PARTITION p2 VALUES LESS THAN ('2019-06-30')
)
DISTRIBUTED BY HASH(`k2`) BUCKETS 8
PROPERTIES (
"colocate_with" = "group1"
);
Table 2:
CREATE TABLE `tbl2` (
`k1` datetime NOT NULL COMMENT "",
`k2` int(11) NOT NULL COMMENT "",
`v1` double SUM NOT NULL COMMENT ""
) ENGINE=OLAP
AGGREGATE KEY(`k1`, `k2`)
DISTRIBUTED BY HASH(`k2`) BUCKETS 8
PROPERTIES (
"colocate_with" = "group1"
);
Inspect the join plan
DESC SELECT * FROM tbl1 INNER JOIN tbl2 ON (tbl1.k2 = tbl2.k2);
+----------------------------------------------------+
| Explain String |
+----------------------------------------------------+
| PLAN FRAGMENT 0 |
| OUTPUT EXPRS:`tbl1`.`k1` | |
| PARTITION: RANDOM |
| |
| RESULT SINK |
| |
| 2:HASH JOIN |
| | join op: INNER JOIN |
| | hash predicates: |
| | colocate: true |
| | `tbl1`.`k2` = `tbl2`.`k2` |
| | tuple ids: 0 1 |
| | |
| |----1:OlapScanNode |
| | TABLE: tbl2 |
| | PREAGGREGATION: OFF. Reason: null |
| | partitions=0/1 |
| | rollup: null |
| | buckets=0/0 |
| | cardinality=-1 |
| | avgRowSize=0.0 |
| | numNodes=0 |
| | tuple ids: 1 |
| | |
| 0:OlapScanNode |
| TABLE: tbl1 |
| PREAGGREGATION: OFF. Reason: No AggregateInfo |
| partitions=0/2 |
| rollup: null |
| buckets=0/0 |
| cardinality=-1 |
| avgRowSize=0.0 |
| numNodes=0 |
| tuple ids: 0 |
+----------------------------------------------------+
In this plan format, colocate: true on the hash join indicates that the plan uses colocation. Other plan formats can identify the same distribution with a COLOCATE marker.
If it does not take effect, the query plan is as follows:
+----------------------------------------------------+
| Explain String |
+----------------------------------------------------+
| PLAN FRAGMENT 0 |
| OUTPUT EXPRS:`tbl1`.`k1` | |
| PARTITION: RANDOM |
| |
| RESULT SINK |
| |
| 2:HASH JOIN |
| | join op: INNER JOIN (BROADCAST) |
| | hash predicates: |
| | colocate: false, reason: group is not stable |
| | `tbl1`.`k2` = `tbl2`.`k2` |
| | tuple ids: 0 1 |
| | |
| |----3:EXCHANGE |
| | tuple ids: 1 |
| | |
| 0:OlapScanNode |
| TABLE: tbl1 |
| PREAGGREGATION: OFF. Reason: No AggregateInfo |
| partitions=0/2 |
| rollup: null |
| buckets=0/0 |
| cardinality=-1 |
| avgRowSize=0.0 |
| numNodes=0 |
| |
| PLAN FRAGMENT 1 |
| OUTPUT EXPRS: |
| PARTITION: RANDOM |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 03 |
| UNPARTITIONED |
| |
| 1:OlapScanNode |
| TABLE: tbl2 |
| PREAGGREGATION: OFF. Reason: null |
| partitions=0/1 |
| rollup: null |
| buckets=0/0 |
| cardinality=-1 |
| avgRowSize=0.0 |
| numNodes=0 |
| tuple ids: 1 |
+----------------------------------------------------+
The HASH JOIN node displays the corresponding reason: colocate: false, reason: group is not stable, and an EXCHANGE node is generated.
Compare join distribution methods
To help determine whether a query uses Colocation Join, the following table compares the common Join types in VeloDB Cloud:
| Join Type | Shuffle Data? | Trigger Condition |
|---|---|---|
| Colocate Join | No shuffle for this join | Compatible colocated inputs and equality predicates on the distribution columns allow the optimizer to choose a local join. |
| Bucket Shuffle Join | Partial (one side) | The Join Key is consistent with the bucket column of the left table. |
| Shuffle Join | Usually both sides, unless an input already satisfies the required distribution | Inputs are distributed by join key. |
| Broadcast Join | Replicates the build input | The optimizer or a valid hint selects broadcast when supported by the join type and resource requirements. |
Managed placement and troubleshooting
The service manages compute placement, node changes, and recovery. Self-managed Apache Doris instructions for replica repair, disable_colocate_balance, disable_colocate_relocate, or manually overwriting a backend bucket sequence do not apply as Cloud tuning procedures.
For a query that unexpectedly shuffles data:
- Inspect
SHOW CREATE TABLEfor each table and confirm the group name, distribution column types and order, and bucket count. - Inspect
EXPLAINand the join predicates. Check the reported reason if colocation is rejected. - Compare behavior on the cluster that runs the workload. A placement display from another cluster does not describe that query's execution.
- If a previously working plan changes after scaling or node recovery, record the query ID, cluster, table definitions, plan, and query profile. Contact VeloDB Cloud support for service-side diagnosis.
Do not mark a group stable or force its bucket mapping through internal FE HTTP endpoints. Those operations change service metadata and do not resolve incompatible table definitions or join predicates.
FAQ
Why does the plan report colocate: false, reason: group is not stable?
The optimizer has rejected colocation because of the group's metadata state and uses another plan. Check the actual query plan and group diagnostics. In VeloDB Cloud, do not interpret this message as an instruction to run self-managed replica repair or disable balancing. Contact VeloDB Cloud support if it persists.
Does IsStable=true guarantee that the query uses colocation?
No. The join predicates and input distribution must also satisfy the optimizer's requirements. Verify the join marker in EXPLAIN for the actual query.
How long does an unstable state last?
There is no fixed recovery duration established by this guide. Record the affected query and cluster, and contact VeloDB Cloud support if the state persists. Changing a managed balancing parameter is not a user remedy.
Why is a table rejected when joining a group?
Compare the distribution column count, order, compatible types, and bucket count with the existing group. Check the returned error for the specific mismatch. Storage replica allocation is managed by the service.
Can tables in different databases use colocation?
Yes. Use the same group name with the __global__ prefix. The name is shared across databases in the warehouse. It does not extend colocation across warehouses.
See also
Drop the example tables when they are no longer needed. Test tables and query execution consume storage and compute.