Sharding

Syntax

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. ALTER SHARDING ALGORITHM shardingAlgorithmDefinition [, shardingAlgorithmDefinition] ...
  7. DROP SHARDING ALGORITHM algorithmName [, algorithmName] ...
  8. shardingTableRuleDefinition:
  9. shardingAutoTableRule | shardingTableRule
  10. shardingAutoTableRule:
  11. tableName(resources COMMA shardingColumn COMMA algorithmDefinition (COMMA keyGenerateDeclaration)?)
  12. shardingTableRule:
  13. tableName(dataNodes (COMMA databaseStrategy)? (COMMA tableStrategy)? (COMMA keyGenerateDeclaration)?)
  14. resources:
  15. RESOURCES(resource [, resource] ...)
  16. dataNodes:
  17. DATANODES(dataNode [, dataNode] ...)
  18. resource:
  19. resourceName | inlineExpression
  20. dataNode:
  21. resourceName | inlineExpression
  22. shardingColumn:
  23. SHARDING_COLUMN=columnName
  24. algorithmDefinition:
  25. TYPE(NAME=shardingAlgorithmType [, PROPERTIES([algorithmProperties])])
  26. keyGenerateDeclaration:
  27. keyGenerateDefinition | keyGenerateConstruction
  28. keyGenerateDefinition:
  29. GENERATED_KEY(COLUMN=columnName, strategyDefinition)
  30. shardingScope:
  31. DATABASE | TABLE
  32. databaseStrategy:
  33. DATABASE_STRATEGY(shardingStrategy)
  34. tableStrategy:
  35. TABLE_STRATEGY(shardingStrategy)
  36. keyGenerateConstruction
  37. GENERATED_KEY(COLUMN=columnName, GENERATED_KEY_ALGORITHM=keyGenerateAlgorithmName)
  38. shardingStrategy:
  39. TYPE=strategyType, shardingColumn, shardingAlgorithm
  40. shardingColumn:
  41. SHARDING_COLUMN=columnName
  42. shardingAlgorithm:
  43. existingAlgorithm | autoCreativeAlgorithm
  44. existingAlgorithm:
  45. SHARDING_ALGORITHM=shardingAlgorithmName
  46. autoCreativeAlgorithm:
  47. SHARDING_ALGORITHM(algorithmDefinition)
  48. strategyDefinition:
  49. TYPE(NAME=keyGenerateStrategyType [, PROPERTIES([algorithmProperties])])
  50. shardingAlgorithmDefinition:
  51. shardingAlgorithmName(algorithmDefinition)
  52. algorithmProperties:
  53. algorithmProperty [, algorithmProperty] ...
  54. algorithmProperty:
  55. 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 Table Rule supports both Auto Table and Table at the same time. The two types are different in syntax. For the corresponding configuration file, please refer to Sharding
  • When using the autoCreativeAlgorithm way to specify shardingStrategy, a new sharding algorithm will be created automatically. The algorithm naming rule is tableName_strategyType_shardingAlgorithmType, such as t_order_database_inline

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

Key Generator

  1. CREATE SHARDING KEY GENERATOR snowflake_key_generator (
  2. TYPE(NAME=SNOWFLAKE, PROPERTIES("worker-id"=123))
  3. );
  4. ALTER SHARDING KEY GENERATOR snowflake_key_generator (
  5. TYPE(NAME=SNOWFLAKE, PROPERTIES("worker-id"=456))
  6. );
  7. DROP SHARDING KEY GENERATOR snowflake_key_generator;

Auto Table

  1. CREATE SHARDING TABLE RULE t_order (
  2. RESOURCES(resource_0,resource_1),
  3. SHARDING_COLUMN=order_id,TYPE(NAME=hash_mod,PROPERTIES("sharding-count"=4)),
  4. GENERATED_KEY(COLUMN=another_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
  5. );
  6. ALTER SHARDING TABLE RULE t_order (
  7. RESOURCES(resource_0,resource_1,resource_2,resource_3),
  8. SHARDING_COLUMN=order_id,TYPE(NAME=hash_mod,PROPERTIES("sharding-count"=16)),
  9. GENERATED_KEY(COLUMN=another_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
  10. );
  11. DROP SHARDING TABLE RULE t_order;
  12. DROP SHARDING ALGORITHM t_order_hash_mod;

Table

  1. CREATE SHARDING ALGORITHM table_inline (
  2. TYPE(NAME=inline,PROPERTIES("algorithm-expression"="t_order_item_${order_id % 2}"))
  3. );
  4. CREATE SHARDING TABLE RULE t_order_item (
  5. DATANODES("resource_${0..1}.t_order_item_${0..1}"),
  6. DATABASE_STRATEGY(TYPE=standard,SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME=inline,PROPERTIES("algorithm-expression"="resource_${user_id % 2}")))),
  7. TABLE_STRATEGY(TYPE=standard,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=table_inline),
  8. GENERATED_KEY(COLUMN=another_id,GENERATED_KEY_ALGORITHM=snowflake_key_generator)
  9. );
  10. ALTER SHARDING ALGORITHM database_inline (
  11. TYPE(NAME=inline,PROPERTIES("algorithm-expression"="resource_${user_id % 4}"))
  12. ),table_inline (
  13. TYPE(NAME=inline,PROPERTIES("algorithm-expression"="t_order_item_${order_id % 4}"))
  14. );
  15. ALTER SHARDING TABLE RULE t_order_item (
  16. DATANODES("resource_${0..3}.t_order_item${0..3}"),
  17. DATABASE_STRATEGY(TYPE=standard,SHARDING_COLUMN=user_id,SHARDING_ALGORITHM=database_inline),
  18. TABLE_STRATEGY(TYPE=standard,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=table_inline),
  19. GENERATED_KEY(COLUMN=another_id,GENERATED_KEY_ALGORITHM=snowflake_key_generator)
  20. );
  21. DROP SHARDING TABLE RULE t_order_item;
  22. DROP SHARDING ALGORITHM database_inline;
  23. CREATE DEFAULT SHARDING DATABASE STRATEGY (
  24. TYPE = standard,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=algorithmsName
  25. );

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;