Sharding

Definition

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 needs to use data source resources managed by RDL
  • shardingAlgorithmType specifies the type of automatic sharding algorithm, please refer to Auto Sharding Algorithm
  • keyGenerateStrategyType specifies the distributed primary key generation strategy, please refer to Key Generate Algorithm
  • Duplicate tableName will not be created
  • shardingAlgorithm can be reused by different Sharding Table Rule, so when executing DROP SHARDING TABLE RULE, the corresponding shardingAlgorithm will not be removed
  • To remove shardingAlgorithm, please execute DROP SHARDING ALGORITHM
  • strategyType specifies the sharding strategy,please refer toSharding Strategy

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 will overwrite the binding table configuration in the database with the new configuration

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
  • ALTER will overwrite the broadcast table configuration in the database with the new configuration

Example

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;