Sharding

Syntax

Sharding Table Rule

  1. CREATE SHARDING TABLE RULE shardingTableRuleDefinition [, shardingTableRuleDefinition] ...
  2. ALTER SHARDING TABLE RULE shardingTableRuleDefinition [, shardingTableRuleDefinition] ...
  3. DROP SHARDING TABLE RULE tableName [, tableName] ...
  4. CREATE DEFAULT SHARDING shardingScope STRATEGY (shardingStrategy)
  5. ALTER DEFAULT SHARDING shardingScope STRATEGY (shardingStrategy)
  6. DROP DEFAULT SHARDING shardingScope STRATEGY;
  7. DROP SHARDING ALGORITHM algorithmName [, algorithmName] ...
  8. DROP SHARDING KEY GENERATOR [IF EXISTS] keyGeneratorName [, keyGeneratorName] ...
  9. DROP SHARDING AUDITOR [IF EXISTS] auditorName [, auditorName] ...
  10. shardingTableRuleDefinition:
  11. shardingAutoTableRule | shardingTableRule
  12. shardingAutoTableRule:
  13. tableName(storageUnits, shardingColumn, algorithmDefinition [, keyGenerateDefinition] [, auditDefinition])
  14. shardingTableRule:
  15. tableName(dataNodes [, databaseStrategy] [, tableStrategy] [, keyGenerateDefinition] [, auditDefinition])
  16. storageUnits:
  17. STORAGE_UNITS(storageUnit [, storageUnit] ...)
  18. dataNodes:
  19. DATANODES(dataNode [, dataNode] ...)
  20. storageUnit:
  21. storageUnitName | inlineExpression
  22. dataNode:
  23. dataNodeName | inlineExpression
  24. shardingColumn:
  25. SHARDING_COLUMN=columnName
  26. algorithmDefinition:
  27. TYPE(NAME=shardingAlgorithmType [, PROPERTIES([algorithmProperties])])
  28. keyGenerateDefinition:
  29. KEY_GENERATE_STRATEGY(COLUMN=columnName, strategyDefinition)
  30. auditDefinition:
  31. AUDIT_STRATEGY([singleAuditDefinition, singleAuditDefinition], ALLOW_HINT_DISABLE=true)
  32. singleAuditDefinition:
  33. algorithmDefinition
  34. shardingScope:
  35. DATABASE | TABLE
  36. databaseStrategy:
  37. DATABASE_STRATEGY(shardingStrategy)
  38. tableStrategy:
  39. TABLE_STRATEGY(shardingStrategy)
  40. shardingStrategy:
  41. TYPE=strategyType, shardingColumn, shardingAlgorithm
  42. shardingAlgorithm:
  43. SHARDING_ALGORITHM(algorithmDefinition)
  44. strategyDefinition:
  45. TYPE(NAME=keyGenerateStrategyType [, PROPERTIES([algorithmProperties])])
  46. algorithmProperties:
  47. algorithmProperty [, algorithmProperty] ...
  48. algorithmProperty:
  49. key=value
  • STORAGE_UNITS needs to use storage units 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
  • auditorAlgorithmType specifies the sharding audit strategy, please refer to Sharding Audit Algorithm
  • Duplicate tableName will not be created
  • 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
  • executing CREATE SHARDING TABLE RULE,a new sharding algorithm will be created automatically. The algorithm naming rule is tableName_scope_shardingAlgorithmType,such as t_order_database_inline
  • executing CREATE DEFAULT SHARDING STRATEGY,a new sharding algorithm is also created automatically,The algorithm naming rule is default_scope_shardingAlgorithmType,such as default_database_inline

Sharding Table Reference Rule

  1. CREATE SHARDING TABLE REFERENCE RULE tableReferenceRuleDefinition [, tableReferenceRuleDefinition] ...
  2. ALTER SHARDING TABLE REFERENCE RULE tableReferenceRuleDefinition [, tableReferenceRuleDefinition] ...
  3. DROP SHARDING TABLE REFERENCE RULE ifExists? ruleName [, ruleName] ...
  4. tableReferenceRuleDefinition:
  5. ruleName (tableName [, tableName] ... )
  • A sharding table can only be associated with one sharding table reference rule

Broadcast Table Rule

  1. CREATE BROADCAST TABLE RULE tableName [, tableName] ...
  2. DROP BROADCAST TABLE RULE tableName [, tableName] ...

Example

Sharding Table Rule

Key Generator

  1. DROP SHARDING KEY GENERATOR snowflake_key_generator;

Auditor

  1. DROP SHARDING AUDITOR IF EXISTS sharding_key_required_auditor;

Auto Table

  1. CREATE SHARDING TABLE RULE t_order (
  2. STORAGE_UNITS(ds_0,ds_1),
  3. SHARDING_COLUMN=order_id,TYPE(NAME="hash_mod",PROPERTIES("sharding-count"="4")),
  4. KEY_GENERATE_STRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")),
  5. AUDIT_STRATEGY(TYPE(NAME="dml_sharding_conditions"),ALLOW_HINT_DISABLE=true)
  6. );
  7. ALTER SHARDING TABLE RULE t_order (
  8. STORAGE_UNITS(ds_0,ds_1,ds_2,ds_3),
  9. SHARDING_COLUMN=order_id,TYPE(NAME="hash_mod",PROPERTIES("sharding-count"="16")),
  10. KEY_GENERATE_STRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")),
  11. AUDIT_STRATEGY(TYPE(NAME="dml_sharding_conditions"),ALLOW_HINT_DISABLE=true)
  12. );
  13. DROP SHARDING TABLE RULE t_order;
  14. DROP SHARDING ALGORITHM t_order_hash_mod;

Table

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

Sharding Table Reference Rule

  1. CREATE SHARDING TABLE REFERENCE RULE ref_0 (t_order,t_order_item), ref_1 (t_1,t_2);
  2. ALTER SHARDING TABLE REFERENCE RULE ref_0 (t_order,t_order_item,t_user);
  3. DROP SHARDING TABLE REFERENCE RULE ref_0, ref_1;

Broadcast Table Rule

  1. CREATE BROADCAST TABLE RULE t_a,t_b;
  2. DROP BROADCAST TABLE RULE t_a;