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. CREATE SHARDING ALGORITHM shardingAlgorithmDefinition [, shardingAlgorithmDefinition] ...
  8. ALTER SHARDING ALGORITHM shardingAlgorithmDefinition [, shardingAlgorithmDefinition] ...
  9. DROP SHARDING ALGORITHM algorithmName [, algorithmName] ...
  10. CREATE SHARDING KEY GENERATOR keyGeneratorDefinition [, keyGeneratorDefinition] ...
  11. ALTER SHARDING KEY GENERATOR keyGeneratorDefinition [, keyGeneratorDefinition] ...
  12. DROP SHARDING KEY GENERATOR [IF EXISTS] keyGeneratorName [, keyGeneratorName] ...
  13. CREATE SHARDING AUDITOR auditorDefinition [, auditorDefinition] ...
  14. ALTER SHARDING AUDITOR auditorDefinition [, auditorDefinition] ...
  15. DROP SHARDING AUDITOR [IF EXISTS] auditorName [, auditorName] ...
  16. shardingTableRuleDefinition:
  17. shardingAutoTableRule | shardingTableRule
  18. shardingAutoTableRule:
  19. tableName(resources, shardingColumn, algorithmDefinition [, keyGenerateDeclaration] [, auditDeclaration])
  20. shardingTableRule:
  21. tableName(dataNodes [, databaseStrategy] [, tableStrategy] [, keyGenerateDeclaration] [, auditDeclaration])
  22. resources:
  23. RESOURCES(resource [, resource] ...)
  24. dataNodes:
  25. DATANODES(dataNode [, dataNode] ...)
  26. resource:
  27. resourceName | inlineExpression
  28. dataNode:
  29. dataNodeName | inlineExpression
  30. shardingColumn:
  31. SHARDING_COLUMN=columnName
  32. algorithmDefinition:
  33. TYPE(NAME=shardingAlgorithmType [, PROPERTIES([algorithmProperties])])
  34. keyGenerateDeclaration:
  35. keyGenerateDefinition | keyGenerateConstruction
  36. keyGenerateDefinition:
  37. KEY_GENERATE_STRATEGY(COLUMN=columnName, strategyDefinition)
  38. auditDeclaration:
  39. auditDefinition | auditStrategy
  40. auditDefinition:
  41. AUDIT_STRATEGY([(singleAuditDefinition),(singleAuditDefinition)], ALLOW_HINT_DISABLE=true)
  42. singleAuditDefinition:
  43. NAME=auditor1, algorithmDefinition
  44. auditStrategy:
  45. AUDIT_STRATEGY(AUDITORS=[auditor1,auditor2], ALLOW_HINT_DISABLE=true)
  46. shardingScope:
  47. DATABASE | TABLE
  48. databaseStrategy:
  49. DATABASE_STRATEGY(shardingStrategy)
  50. tableStrategy:
  51. TABLE_STRATEGY(shardingStrategy)
  52. keyGenerateConstruction
  53. KEY_GENERATE_STRATEGY(COLUMN=columnName, KEY_GENERATOR=keyGenerateAlgorithmName)
  54. shardingStrategy:
  55. TYPE=strategyType, shardingColumn, shardingAlgorithm
  56. shardingAlgorithm:
  57. existingAlgorithm | autoCreativeAlgorithm
  58. existingAlgorithm:
  59. SHARDING_ALGORITHM=shardingAlgorithmName
  60. autoCreativeAlgorithm:
  61. SHARDING_ALGORITHM(algorithmDefinition)
  62. strategyDefinition:
  63. TYPE(NAME=keyGenerateStrategyType [, PROPERTIES([algorithmProperties])])
  64. shardingAlgorithmDefinition:
  65. shardingAlgorithmName(algorithmDefinition)
  66. algorithmProperties:
  67. algorithmProperty [, algorithmProperty] ...
  68. algorithmProperty:
  69. key=value
  70. keyGeneratorDefinition:
  71. keyGeneratorName (algorithmDefinition)
  72. auditorDefinition:
  73. auditorName (auditorAlgorithmDefinition)
  74. auditorAlgorithmDefinition:
  75. TYPE(NAME=auditorAlgorithmType [, PROPERTIES([algorithmProperties])])
  • 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
  • auditorAlgorithmType specifies the sharding audit strategy, please refer to Sharding Audit 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")
  3. );
  4. ALTER SHARDING KEY GENERATOR snowflake_key_generator (
  5. TYPE(NAME="SNOWFLAKE"))
  6. );
  7. DROP SHARDING KEY GENERATOR snowflake_key_generator;

Auditor

  1. CREATE SHARDING AUDITOR sharding_key_required_auditor (
  2. TYPE(NAME="DML_SHARDING_CONDITIONS")
  3. );
  4. ALTER SHARDING AUDITOR sharding_key_required_auditor (
  5. TYPE(NAME="DML_SHARDING_CONDITIONS")
  6. );
  7. DROP SHARDING AUDITOR IF EXISTS sharding_key_required_auditor;

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. KEY_GENERATE_STRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")),
  5. AUDIT_STRATEGY(AUDITORS=[auditor1,auditor2],ALLOW_HINT_DISABLE=true)
  6. );
  7. ALTER SHARDING TABLE RULE t_order (
  8. RESOURCES(resource_0,resource_1,resource_2,resource_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(AUDITORS=[auditor1,auditor2],ALLOW_HINT_DISABLE=true)
  12. );
  13. DROP SHARDING TABLE RULE t_order;
  14. 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. KEY_GENERATE_STRATEGY(COLUMN=another_id,KEY_GENERATOR=snowflake_key_generator),
  9. AUDIT_STRATEGY(AUDITORS=[auditor1,auditor2],ALLOW_HINT_DISABLE=true)
  10. );
  11. ALTER SHARDING ALGORITHM database_inline (
  12. TYPE(NAME="inline",PROPERTIES("algorithm-expression"="resource_${user_id % 4}"))
  13. ),table_inline (
  14. TYPE(NAME="inline",PROPERTIES("algorithm-expression"="t_order_item_${order_id % 4}"))
  15. );
  16. ALTER SHARDING TABLE RULE t_order_item (
  17. DATANODES("resource_${0..3}.t_order_item${0..3}"),
  18. DATABASE_STRATEGY(TYPE="standard",SHARDING_COLUMN=user_id,SHARDING_ALGORITHM=database_inline),
  19. TABLE_STRATEGY(TYPE="standard",SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=table_inline),
  20. KEY_GENERATE_STRATEGY(COLUMN=another_id,KEY_GENERATOR=snowflake_key_generator),
  21. AUDIT_STRATEGY(AUDITORS=[auditor1,auditor2],ALLOW_HINT_DISABLE=true)
  22. );
  23. DROP SHARDING TABLE RULE t_order_item;
  24. DROP SHARDING ALGORITHM database_inline;
  25. CREATE DEFAULT SHARDING DATABASE STRATEGY (
  26. TYPE="standard",SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=database_inline
  27. );
  28. ALTER DEFAULT SHARDING DATABASE STRATEGY (
  29. TYPE="standard",SHARDING_COLUMN=another_id,SHARDING_ALGORITHM=database_inline
  30. );
  31. DROP DEFAULT SHARDING DATABASE STRATEGY;

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;