ShardingSphere 分片附录详解
本附录提供分片功能的核心参考指南,包含算法表达式、配置项说明及最佳实践参数。以下为系统化总结:
一、分片算法表达式语法手册
1. 行表达式语法(Inline Sharding)
${begin..end}
${[enum1, enum2]}
${expression}
ds_${user_id % 4}.t_order_${order_date.getYear()}_${order_id % 16}
2. 时间函数扩展
tbl_${new java.text.SimpleDateFormat("yyyyMM").format(create_time)}
tbl_${create_time.getTime() + 30 * 24 * 3600 * 1000}
3. 复合表达式
${order_id > 10000 ? 'hist' : 'curr'}_${user_id % 8}
${(user_id ?: 0) % 16}
二、分片配置项全集
1. 分片规则配置项
sharding:
tables:
t_order:
actualDataNodes: ds_${0..3}.t_order_${0..15}
databaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: db_mod
complex:
shardingColumns: region,user_type
shardingAlgorithmName: composite_algo
hint:
shardingAlgorithmName: hint_algo
tableStrategy: ...
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
2. 分片算法参数表
算法类型 | 配置项 | 必填 | 说明 |
---|
MOD | sharding-count | ✅ | 分片数量 |
HASH_MOD | sharding-count | ✅ | 分片数量 |
VOLUME_RANGE | range-lower | ✅ | 范围下界 |
| range-upper | ✅ | 范围上界 |
| sharding-volume | ✅ | 分片容量 |
INTERVAL | datetime-pattern | ✅ | 时间格式(yyyy-MM) |
| datetime-lower | ✅ | 最小时间 |
| datetime-upper | ⚠️ | 最大时间(可选) |
| sharding-suffix-pattern | ✅ | 分片后缀格式(yyyyMM) |
| datetime-interval-unit | ✅ | 时间单位(DAY/MONTH) |
3. 分布式序列参数
类型 | 配置项 | 默认值 | 说明 |
---|
SNOWFLAKE | worker-id | 0 | 工作机器ID (0~1023) |
| max-vibration-offset | 1 | 最大时间回退容忍毫秒数 |
| max-tolerate-time-difference | 0 | 最大时钟偏差容忍毫秒数 |
UUID | - | - | 无参数 |
三、Hint强制路由指令集
1. SQL Hint语法
SELECT * FROM t_order;
2. 编程API(Java)
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.setDatabaseShardingValue(123L);
hintManager.addTableShardingValue("t_order", 5);
jdbcTemplate.query("SELECT * FROM t_order", ...);
}
HintManager.clear();
四、弹性伸缩操作命令
1. DistSQL 扩容语法
ADD RESOURCE ds_4 (
URL="jdbc:mysql://127.0.0.1:3306/ds_4",
USER=root,
PASSWORD=root,
PROPERTIES("maxPoolSize"=50)
);
ALTER SHARDING TABLE RULE t_order (
RESOURCES(ds_0,ds_1,ds_2,ds_3,ds_4),
SHARDING_COLUMN=user_id,
TYPE(NAME=mod, PROPERTIES("sharding-count"=5))
);
START MIGRATION JOB BY TYPE (name=sharding_scaling, props=...);
2. 迁移状态监控
SHOW SCALING LIST;
SHOW SCALING STATUS {jobId};
STOP SCALING {jobId};
五、性能调优参数表
props:
max-connections-size-per-query: 1
kernel-executor-size: 16
proxy-frontend-executor-size: 8
proxy-backend-query-fetch-size: 1000
xa-transaction-manager-type: Atomikos
xa-recovery-interval-seconds: 60
sql-show: true
sql-simple: false
六、分片元数据查询SQL
1. 规则查询
SHOW SHARDING TABLE RULES [FROM schemaName];
SHOW SHARDING ALGORITHMS;
SELECT * FROM information_schema.SHARDING_BINDING_TABLES;
2. 运行时状态
SHOW INSTANCE LIST;
SELECT * FROM information_schema.SHARDING_TABLE_STATISTICS;
七、错误代码速查表
错误码 | 类别 | 原因 | 解决方案 |
---|
10000 | 配置错误 | 分片算法未定义 | 检查shardingAlgorithms配置 |
11000 | SQL解析错误 | 不支持WITH子句 | 重写SQL语句 |
12000 | 执行异常 | 跨分片DML未启用分布式事务 | 添加@GlobalTransactional注解 |
13010 | 分布式主键冲突 | worker-id重复 | 检查集群worker-id分配 |
14020 | 弹性伸缩失败 | 源数据源连接超时 | 验证网络连通性 |
八、版本兼容性矩阵
功能 | 5.0 | 5.1 | 5.2 | 5.3 |
---|
自定义分片算法 | ✅ | ✅ | ✅ | ✅ |
DistSQL动态配置 | ❌ | ✅ | ✅ | ✅ |
异构存储支持 | ❌ | ⚠️ | ✅ | ✅ |
Kubernetes Operator | ❌ | ❌ | ✅ | ✅ |
升级注意:
- 4.x → 5.x 需重写分片配置(YAML结构调整)
- 分布式序列SNOWFLAKE实现不兼容(需数据迁移)
本附录为分片功能的权威参考,建议结合官方配置手册进行实践验证。