Sharding Algorithm #
Auto Sharding Algorithm #
Modulo Sharding Algorithm #
Type: MOD
Configurable Properties:
Property name | Data type | Description |
---|---|---|
sharding-count | int | Number of shards |
Hash Modulo Sharding Algorithm #
Type: HASH_MOD
Configurable Properties:
Property name | Data type | Description |
---|---|---|
sharding-count | int | Number of shards |
Volume Based Range Sharding Algorithm #
Type: VOLUME_RANGE
Configurable Properties:
Property name | Data type | Description |
---|---|---|
range-lower | long | Range lower bound, throw exception if lower than bound |
range-upper | long | Range upper bound, throw exception if upper than bound |
sharding-volume | long | Sharding volume |
Boundary Based Range Sharding Algorithm #
Type: BOUNDARY_RANGE
Configurable Properties:
Property name | Data type | Description |
---|---|---|
sharding-ranges | String | Range of sharding border, multiple boundaries separated by commas |
Auto Interval Sharding Algorithm #
Type: AUTO_INTERVAL
Configurable Properties:
Property name | Data type | Description |
---|---|---|
datetime-lower | String | Shard datetime begin boundary, pattern: yyyy-MM-dd HH:mm:ss |
datetime-upper | String | Shard datetime end boundary, pattern: yyyy-MM-dd HH:mm:ss |
sharding-seconds | long | Max seconds for the data in one shard, allows sharding key timestamp format seconds with time precision, but time precision after seconds is automatically erased |
Consistent Hash Sharding Algorithm #
Type: SphereEx:CONSISTENT_HASH
Configurable Properties:
Property name | Data type | Description |
---|---|---|
sharding-count | int | Required, used to specify the number of shards. |
sharding-node-weight | String | Optional, the weight of sharding data nodes, separated by commas, for example: “2,2,1,1”. The greater the weight, the more virtual nodes are used for consistent Hash computing, and the default weight is 1. |
virtual-node-count | int | Required item, the number of virtual nodes corresponding to the sharding data nodes. The more the number of virtual nodes, the more balanced the sharding, but too many virtual nodes will also affect the performance. |
consistent-hash-seed | int | Optional, the seed parameter for computing consistent Hash, the default value is 0. |
HASH RANGE Algorithm #
Type: SphereEx:HASH_RANGE
Configurable Properties:
Property Name | Data Type | Description |
---|---|---|
partition-count | int | Number of partitions |
partition-length | int | Length of partitions |
Precise Routing Explanation:
partitionCount: C1, C2...Cn
partitionLength: L1, L2...Ln
By configuring these two items, calculate the cardinality M = C1*L1 + C2*L2 + ... + Cn*Ln
Routing logic: shardingValue % M, then see which partition it falls into
For example:
partitionCount is configured as 2, 3
partitionLength is configured as 100, 50
A total of 5 Ranges, the first two ranges are each 100 in size, and the last three ranges are each 50 in size
So M = 2 * 100 + 3 * 50 = 350
For example, if the routing sharingValue = 805, then 805 % 350 = 105, falls into the first range (indexed from 0), corresponding to t_order_0.
String Hash Range Algorithm #
Type: SphereEx:STRING_HASH_RANGE
Configurable Properties:
Property Name | Data Type | Description |
---|---|---|
partition-count | int | Number of partitions |
partition-length | int | Length of partitions |
Precise Routing Explanation:
Sharding route logic:
Precise routing:
1. Substring according to shardingValue string
2. Calculate the hash value of the substring
3. Modulo (based on partitionLength and partitionCount jointly calculated cardinality M.)
4. Calculate which range it falls into
5. Route to the real table
Range routing: Full routing
Standard Sharding Algorithm #
The builtin standard sharding algorithm implementation classes of DB Plus Engine include:
Inline Sharding Algorithm #
With Groovy expressions, InlineShardingStrategy provides single-key support for the sharding operation of =
and IN
in SQL.
Simple sharding algorithms can be used through a simple configuration to avoid laborious Java code developments. For example, t_user_$->{u_id % 8}
means table t_user
is divided into 8 tables according to u_id
, with table names from t_user_0
to t_user_7
.
Please refer to Inline Expression for more details.
Type: INLINE
Configurable Properties:
Property name | Data type | Description | Default value |
---|---|---|---|
algorithm-expression | String | Inline expression sharding algorithm | |
allow-range-query-with-inline-sharding (?) | boolean | Whether range query is allowed. Note: range query will ignore sharding strategy and conduct full routing | false |
Interval Sharding Algorithm #
Type: INTERVAL
Configurable Properties:
Name | DataType | Description | Default Value |
---|---|---|---|
datetime-pattern | String | Timestamp pattern of sharding value, must can be transformed to Java LocalDateTime. For example: yyyy-MM-dd HH:mm:ss, yyyy-MM-dd or HH:mm:ss etc. But Gy-MM etc. related to java.time.chrono.JapaneseDate are not supported | - |
datetime-lower | String | Datetime sharding lower boundary, pattern is defined datetime-pattern | - |
datetime-upper (?) | String | Datetime sharding upper boundary, pattern is defined datetime-pattern | Now |
sharding-suffix-pattern | String | Suffix pattern of sharding data sources or tables, must can be transformed to Java LocalDateTime, must be consistent with datetime-interval-unit . For example: yyyyMM | - |
datetime-interval-amount (?) | int | Interval of sharding value, after which the next shard will be entered | 1 |
datetime-interval-unit (?) | String | Unit of sharding value interval, must can be transformed to Java ChronoUnit’s Enum value. For example: MONTHS | DAYS |
Complex Sharding Algorithm #
Complex Inline Sharding Algorithm #
Please refer to Inline Expression for more details.
Type: COMPLEX_INLINE
Name | DataType | Description | Default Value |
---|---|---|---|
sharding-columns (?) | String | sharing column names, Multiple columns need to be separated by commas. If it is not configured, it cannot be verified | |
algorithm-expression | String | Inline expression sharding algorithm | |
allow-range-query-with-inline-sharding (?) | boolean | Whether range query is allowed. Note: range query will ignore sharding strategy and conduct full routing | false |
Hint Sharding Algorithm #
Hint Inline Sharding Algorithm #
Please refer to Inline Expression for more details.
Type: HINT_INLINE
Name | DataType | Description | Default Value |
---|---|---|---|
algorithm-expression (?) | String | Inline expression sharding algorithm | ${value} |
Class Based Sharding Algorithm #
Custom extensions can be realised by configuring the sharding strategy type and algorithm class name.
Type: CLASS_BASED
Configurable Properties:
Name | DataType | Description |
---|---|---|
strategy | String | Sharding strategy type, support STANDARD, COMPLEX or HINT (case insensitive) |
algorithmClassName | String | Fully qualified name of sharding algorithm |