数据分片

语法说明

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 需使用 RDL 管理的数据源资源
  • shardingAlgorithmType 指定自动分片算法类型,请参考 自动分片算法
  • keyGenerateStrategyType 指定分布式主键生成策略,请参考 分布式主键
  • 重复的 tableName 将无法被创建
  • shardingAlgorithm 能够被不同的 Sharding Table Rule 复用,因此在执行 DROP SHARDING TABLE RULE 时,对应的 shardingAlgorithm 不会被移除
  • 如需移除 shardingAlgorithm,请执行 DROP SHARDING ALGORITHM
  • strategyType 指定分片策略,请参考分片策略
  • Sharding Table Rule 同时支持 Auto TableTable 两种类型,两者在语法上有所差异,对应配置文件请参考 数据分片
  • 使用 autoCreativeAlgorithm 方式指定 shardingStrategy 时,将会自动创建新的分片算法,算法命名规则为 tableName_strategyType_shardingAlgorithmType,如 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 会使用新的配置直接覆盖数据库内的绑定表配置

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

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;
  4. DROP SHARDING BROADCAST TABLE RULES t_b;