Terminology
- Colocation Group (CG): A CG will contain one or more Tables. The Tables within a CG have the same bucketing and replica placement, and are described using the Colocation Group Schema.
- Colocation Group Schema (CGS): A CGS contains the bucketing key, number of buckets, and number of replicas of a CG.
Principle
Colocate Join is to form a CG with a set of Tables having the same CGS, and ensure that the corresponding bucket copies of these Tables will fall on the same set of BE nodes. When the tables in the CG perform Join operations on the bucketed columns, the local data can be joined directly, saving time from transferring data between nodes. Bucket Seq is obtained byhash(key) mod buckets. Suppose a Table has 8 buckets, then there are [0, 1, 2, 3, 4, 5, 6, 7] 8 buckets, and each Bucket has one or more sub-tables, the number of sub-tables depends on the number of partitions. If it is a multi-partitioned table, there will be multiple tablets.
In order to have the same data distribution, tables within the same CG must comply with the following.
- Tables within the same CG must have the identical bucketing key (type, number, order) and the same number of buckets so that the data slices of multiple tables can be distributed and controlled one by one. The bucketing key is the columns specified in the table creation statement
DISTRIBUTED BY HASH(col1, col2, ...). The bucketing key determines which columns of data are Hashed into different Bucket Seqs. The name of the bucketing key can vary for tables within the same CG.The bucketing columns can be different in the creation statement, but the order of the corresponding data types inDISTRIBUTED BY HASH(col1, col2, ...)should be exactly the same . - Tables within the same CG may have different numbers of partitions and different partition keys.
"colocate_with" = "group_name" in the table PROPERTIES. If the CG does not exist, it means the table is the first table of the CG and called Parent Table. The data distribution of the Parent Table (type, number and order of split bucket keys, number of copies and number of split buckets) determines the CGS. If the CG exists, check whether the data distribution of the table is consistent with the CGS.
The copy placement of tables within the same CG satisfies:
- The mapping between the Bucket Seq and BE nodes of all the Tables is the same as that of the Parent Table.
- The mapping between the Bucket Seq and BE nodes of all the Partitions in the Parent Table is the same as that of the first Partition.
- The mapping between the Bucket Seq and BE nodes of the first Partition of the Parent Table is determined using the native Round Robin algorithm.
Usage
Table creation
When creating a table, you can specify the attribute"colocate_with" = "group_name" in PROPERTIES to indicate that the table is a Colocate Join table and belongs to a specified Colocation Group.
NOTE
From version 2.5.4, Colocate Join can be performed on tables from different databases. You only need to specify the same colocate_with property when you create tables.
For example:
dbId_groupName, but you only perceive groupName.
NOTE
If you specify the same Colocation Group to associate tables from different databases for Colocate Join, the Colocation Group exists in each of these databases. You can run show proc "/colocation_group" to check the Colocation Groups in different databases.
Delete
A complete deletion is a deletion from the Recycle Bin. Normally, after a table is deleted with theDROP TABLE command, by default it will stay in the recycle bin for a day before being deleted. When the last table in a Group is completely deleted, the Group will also be deleted automatically.
View group information
The following command allows you to view the Group information that already exists in the cluster.Text
- GroupId: The cluster-wide unique identifier of a Group, with the first half being the db id and the second half being the group id.
- GroupName: The full name of the Group.
- TabletIds: The list of ids of the Tables in the Group.
- BucketsNum: The number of buckets.
- ReplicationNum: The number of replicas.
- DistCols: Distribution columns, i.e. bucketing column type.
- IsStable: Whether the Group is stable (for the definition of stability, see the section of Colocation Replica Balancing and Repair).
Text
- BucketIndex: Subscript of the sequence of buckets.
- BackendIds: The ids of BE nodes where the bucketing data slices t are located.
Note: The above command requires the NODE privilege or the cluster_admin role. Regular users cannot access it.
Modifying Table Group Properties
You can modify the Colocation Group properties of a table. For example:Other related operations
When adding a partition usingADD PARTITION or modifying the number of copies to a table with the Colocation attribute, the database checks if the operation will violate the Colocation Group Schema and rejects it if it does.
Colocation replica balancing and repair
The replicas distribution of a Colocation table needs to follow the distribution rules specified in the Group schema, so it differs from normal sharding in terms of replica repair and balancing. The Group itself has astable property. When stable is true, it means that no changes are being made to table slices in the Group and the Colocation feature is working properly. When stable is false, it means that some table slices in the current Group are being repaired or migrated, and the Colocate Join of the affected tables will degrade to a normal Join.
Replica repair
Replicas can only be stored on the specified BE node. The database will look for the least loaded BE to replace an unavailable BE (e.g.,down, decommission),. After the replacement, all the bucketing data slices on the old BE are repaired. During migration, the Group is marked as Unstable.Replica balancing
The database tries to distribute the Colocation table slices evenly across all BE nodes. Balancing for normal tables is at the replica level, that is, each replica individually finds a lower-load BE node. Balancing for Colocation tables is at the Bucket level, that is, all replicas within a Bucket are migrated together. We use a simple balancing algorithm that distributesBucketsSequnce evenly across all BE nodes without considering the actual size of the replicas but only the number of replicas. The exact algorithm can be found in the code comments in ColocateTableBalancer.java.
Note 1: The current Colocation replica balancing and repair algorithm may not work well for clusters with heterogeneous deployment. The so-called heterogeneous deployment means that the disk capacity, number of disk, and disk type (SSD and HDD) of BE nodes are not consistent. In the case of heterogeneous deployment, it may happen that a small-capacity BE node stores the same number of replicas as a large-capacity BE node.
Note 2: When a Group is in the Unstable state, the Join of its tables will degrade to a normal Join, which may significantly degrade the query performance of the cluster. If you do not want the system to be automatically balanced, set the FE configuration disable_colocate_balance to disable automatic balancing and enable it back at the appropriate time. (See the section Advanced Operations (#Advanced Operations) for details)
Query
The Colocation table is queried in the same way as a normal table. If the Group where the Colocation table is located is in Unstable state, it will automatically degrade to a normal Join, as illustrated by the following example. Table 1:Text
colocate: true.
If it doesn’t take effect, the query plan is as follows:
Text
colocate: false, reason: group is not stable. An EXCHANGE node will be generated at the same time.
Advanced operations
FE configuration items
- disable_colocate_relocate
- disable_colocate_balance
- disable_colocate_join You can disable Colocate join at session granularity by changing this variable.
- disable_colocate_join The Colocate join function can be disabled by changing this variable.
HTTP Restful API
The database provides several HTTP Restful APIs related to Colocate Join for viewing and modifying Colocation Groups. This API is implemented on the FE and can be accessed usingfe_host:fe_http_port with db_admin and user_admin permissions.
-
View all Colocation information of a cluster
-
Mark the Group as Stable or Unstable
If the returned result is
200, the Group is successfully marked as Stable or Unstable. -
Set the data distribution of a Group
This interface allows you to force the number distribution of a Group.
POST /api/colocate/bucketseq?db_id=10005&group_id= 10008Body:[[10004,10002],[10003,10002],[10002,10004],[10003,10002],[10002,10004],[10003,10002],[10003,10004],[10003,10004],[10003,10004],[10002,10004]]returns: 200WhereBodyisBucketsSequencerepresented as a nested array and the ids of the BEs in which bucketing slices are located.Note that to use this command, you may need to set the FE configuration
disable_colocate_relocateanddisable_colocate_balanceto true, that is, to disable the system to perform automatic Colocation replica repair and balancing. Otherwise, it may be automatically reset by the system after modification.