数据分片

定义

Sharding Table Rule

  1. CREATE SHARDING TABLE RULE shardingTableRuleDefinition [, shardingTableRuleDefinition] ...
  2. CREATE DEFAULT SHARDING shardingScope STRATEGY (shardingStrategy)
  3. ALTER SHARDING TABLE RULE shardingTableRuleDefinition [, shardingTableRuleDefinition] ...
  4. DROP SHARDING TABLE RULE tableName [, tableName] ...
  5. CREATE SHARDING ALGORITHM shardingAlgorithmDefinition [, shardingAlgorithmDefinition] ...
  6. DROP SHARDING ALGORITHM algorithmName [, algorithmName] ...
  7. shardingTableRuleDefinition:
  8. shardingAutoTableRule | shardingTableRule
  9. shardingAutoTableRule:
  10. tableName(resources (COMMA shardingColumn)? (COMMA algorithmDefinition)? (COMMA keyGenerateStrategy)?)
  11. shardingTableRule:
  12. tableName(dataNodes (COMMA databaseStrategy)? (COMMA tableStrategy)? (COMMA keyGenerateStrategy)?)
  13. resources:
  14. RESOURCES(resource [, resource] ...)
  15. dataNodes:
  16. DATANODES(dataNode [, dataNode] ...)
  17. resource:
  18. resourceName | inlineExpression
  19. dataNode:
  20. resourceName | inlineExpression
  21. shardingColumn:
  22. SHARDING_COLUMN=columnName
  23. algorithmDefinition:
  24. TYPE(NAME=shardingAlgorithmType [, PROPERTIES([algorithmProperties])])
  25. keyGenerateStrategy:
  26. GENERATED_KEY(COLUMN=columnName, strategyDefinition)
  27. shardingScope:
  28. DATABASE | TABLE
  29. databaseStrategy:
  30. DATABASE_STRATEGY(shardingStrategy)
  31. tableStrategy:
  32. TABLE_STRATEGY(shardingStrategy)
  33. shardingStrategy:
  34. TYPE=strategyType, shardingColumn, shardingAlgorithm
  35. shardingColumn:
  36. SHARDING_COLUMN=columnName
  37. shardingAlgorithm:
  38. SHARDING_ALGORITHM=shardingAlgorithmName
  39. strategyDefinition:
  40. TYPE(NAME=keyGenerateStrategyType [, PROPERTIES([algorithmProperties])])
  41. shardingAlgorithmDefinition:
  42. shardingAlgorithmName(algorithmDefinition)
  43. algorithmProperties:
  44. algorithmProperty [, algorithmProperty] ...
  45. algorithmProperty:
  46. key=value
  • RESOURCES 需使用 RDL 管理的数据源资源
  • shardingAlgorithmType 指定自动分片算法类型,请参考 自动分片算法
  • keyGenerateStrategyType 指定分布式主键生成策略,请参考 分布式主键
  • 重复的 tableName 将无法被创建
  • shardingAlgorithm 能够被不同的 Sharding Table Rule 复用,因此在执行 DROP SHARDING TABLE RULE 时,对应的 shardingAlgorithm 不会被移除
  • 如需移除 shardingAlgorithm,请执行 DROP SHARDING ALGORITHM
  • strategyType 指定分片策略,请参考分片策略

Sharding Binding Table Rule

  1. CREATE SHARDING BINDING TABLE RULES bindTableRulesDefinition [, bindTableRulesDefinition] ...
  2. ALTER SHARDING BINDING TABLE RULES bindTableRulesDefinition [, bindTableRulesDefinition] ...
  3. DROP SHARDING BINDING TABLE RULES bindTableRulesDefinition [, bindTableRulesDefinition] ...
  4. bindTableRulesDefinition:
  5. (tableName [, tableName] ... )
  • ALTER 会使用新的配置直接覆盖数据库内的绑定表配置

Sharding Broadcast Table Rule

  1. CREATE SHARDING BROADCAST TABLE RULES (tableName [, tableName] ...)
  2. ALTER SHARDING BROADCAST TABLE RULES (tableName [, tableName] ...)
  3. DROP SHARDING BROADCAST TABLE RULES (tableName [, tableName] ...)
  • ALTER 会使用新的配置直接覆盖数据库内的广播表配置

示例

Sharding Table Rule

  1. CREATE SHARDING TABLE RULE t_order (
  2. RESOURCES(resource_0,resource_1),
  3. SHARDING_COLUMN=order_id,
  4. TYPE(NAME=hash_mod,PROPERTIES("sharding-count"=4)),
  5. GENERATED_KEY(COLUMN=another_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
  6. ),t_order_item (
  7. DATANODES("resource_${0..1}.t_order${0..1}"),
  8. DATABASE_STRATEGY(TYPE=standard,SHARDING_COLUMN=user_id,SHARDING_ALGORITHM=database_inline),
  9. TABLE_STRATEGY(TYPE=standard,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=database_inline),
  10. GENERATED_KEY(COLUMN=another_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
  11. );
  12. ALTER SHARDING TABLE RULE t_order (
  13. RESOURCES(resource_0,resource_1),
  14. SHARDING_COLUMN=order_id,
  15. TYPE(NAME=hash_mod,PROPERTIES("sharding-count"=10)),
  16. GENERATED_KEY(COLUMN=another_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
  17. ),t_order_item (
  18. DATANODES("resource_0.t_order${0..1}"),
  19. DATABASE_STRATEGY(TYPE=standard,SHARDING_COLUMN=user_id,SHARDING_ALGORITHM=database_inline),
  20. TABLE_STRATEGY(TYPE=standard,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=database_inline),
  21. GENERATED_KEY(COLUMN=another_id,TYPE(NAME=uuid,PROPERTIES("worker-id"=123)))
  22. );
  23. DROP SHARDING TABLE RULE t_order, t_order_item;
  24. CREATE DEFAULT SHARDING DATABASE STRATEGY (
  25. TYPE = standard,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=algorithmsName
  26. );
  27. CREATE SHARDING ALGORITHM algorithmName (
  28. TYPE(NAME=hash_mod,PROPERTIES("algorithm-expression"="t_order_${order_id % 2}"))
  29. );
  30. DROP SHARDING ALGORITHM t_order_hash_mod;

Sharding Binding Table Rule

  1. CREATE SHARDING BINDING TABLE RULES (t_order,t_order_item),(t_1,t_2);
  2. ALTER SHARDING BINDING TABLE RULES (t_order,t_order_item);
  3. DROP SHARDING BINDING TABLE RULES;
  4. DROP SHARDING BINDING TABLE RULES (t_order,t_order_item);

Sharding Broadcast Table Rule

  1. CREATE SHARDING BROADCAST TABLE RULES (t_b,t_a);
  2. ALTER SHARDING BROADCAST TABLE RULES (t_b,t_a,t_3);
  3. DROP SHARDING BROADCAST TABLE RULES;
  4. DROP SHARDING BROADCAST TABLE RULES t_b;