ALTER SHARDING TABLE RULE

Description

The ALTER SHARDING TABLE RULE syntax is used to alter sharding table rule for the currently selected database

Syntax

  1. AlterShardingTableRule ::=
  2. 'ALTER' 'SHARDING' 'TABLE' 'RULE' ( tableDefinition | autoTableDefinition ) ( ',' ( tableDefinition | autoTableDefinition ) )*
  3. tableDefinition ::=
  4. tableName '(' 'DATANODES' '(' dataNode ( ',' dataNode )* ')' ( ',' 'DATABASE_STRATEGY' '(' strategyDefinition ')' )? ( ',' 'TABLE_STRATEGY' '(' strategyDefinition ')' )? ( ',' 'KEY_GENERATE_STRATEGY' '(' keyGenerateStrategyDefinition ')' )? ( ',' 'AUDIT_STRATEGY' '(' auditStrategyDefinition ')' )? ')'
  5. autoTableDefinition ::=
  6. tableName '(' 'RESOURCES' '(' resourceName ( ',' resourceName )* ')' ',' 'SHARDING_COLUMN' '=' columnName ',' algorithmDefinition ( ',' 'KEY_GENERATE_STRATEGY' '(' keyGenerateStrategyDefinition ')' )? ( ',' 'AUDIT_STRATEGY' '(' auditStrategyDefinition ')' )? ')'
  7. strategyDefinition ::=
  8. 'TYPE' '=' strategyType ',' ( 'SHARDING_COLUMN' | 'SHARDING_COLUMNS' ) '=' columnName ',' algorithmDefinition
  9. keyGenerateStrategyDefinition ::=
  10. 'KEY_GENERATE_STRATEGY' '(' 'COLUMN' '=' columnName ',' ( 'KEY_GENERATOR' '=' algorihtmName | algorithmDefinition ) ')'
  11. auditStrategyDefinition ::=
  12. 'AUDIT_STRATEGY' '(' 'AUDITORS' '=' '[' auditorName ',' auditorName ']' ',' 'ALLOW_HINT_DISABLE' '=' 'TRUE | FALSE' ')'
  13. |
  14. 'AUDIT_STRATEGY' '(' '[' 'NAME' '=' auditorName ',' algorithmDefinition ']' ',' '[' 'NAME' '=' auditorName ',' algorithmDefinition ']' ')'
  15. algorithmDefinition ::=
  16. ('SHARDING_ALGORITHM' '=' algorithmName | 'TYPE' '(' 'NAME' '=' algorithmType ( ',' 'PROPERTIES' '(' propertyDefinition ')' )?')' )
  17. propertyDefinition ::=
  18. ( key '=' value ) ( ',' key '=' value )*
  19. tableName ::=
  20. identifier
  21. resourceName ::=
  22. identifier
  23. columnName ::=
  24. identifier
  25. auditorName ::=
  26. identifier
  27. algorithmName ::=
  28. identifier
  29. strategyType ::=
  30. string

Supplement

  • tableDefinition is defined for standard sharding table rule; autoTableDefinition is defined for auto sharding table rule. For standard sharding rules and auto sharding rule, refer to Data Sharding;
  • use standard sharding table rule:
    • DATANODES can only use resources that have been added to the current database, and can only use INLINE expressions to specify required resources;
    • DATABASE_STRATEGY, TABLE_STRATEGY are the database sharding strategy and the table sharding strategy, which are optional, and the default strategy is used when not configured;
    • The attribute TYPE in strategyDefinition is used to specify the type of Sharding Algorithm, currently only supports STANDARD, COMPLEX. Using COMPLEX requires specifying multiple sharding columns with SHARDING_COLUMNS.
  • use auto sharding table rule:
    • RESOURCES can only use resources that have been added to the current database, and the required resources can be specified by enumeration or INLINE expression;
    • Only auto sharding algorithm can be used, please refer to Auto Sharding Algorithm.
  • algorithmType is the sharding algorithm type, please refer to Sharding Algorithm;
  • The auto-generated algorithm naming rule is tableName _ strategyType _ shardingAlgorithmType;
  • The auto-generated primary key strategy naming rule is tableName _ strategyType;
  • KEY_GENERATE_STRATEGY is used to specify the primary key generation strategy, which is optional. For the primary key generation strategy, please refer to Distributed Primary Key.
  • AUDIT_STRATEGY is used to specify the sharding audit strategy, which is optional. For the sharding audit generation strategy, please refer to Sharding Audit.

Example

1.Standard sharding table rule

  • Alter standard sharding table rule to the specified sharding algorithms being altered
  1. -- alter sharding algorithms
  2. ALTER SHARDING ALGORITHM database_inline (
  3. TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 4}"))
  4. ), table_inline (
  5. TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${order_id % 4}"))
  6. );
  7. -- alter a sharding rule to the specified sharding algorithms being altered
  8. ALTER SHARDING TABLE RULE t_order (
  9. DATANODES("resource_${0..3}.t_order_item${0..3}"),
  10. DATABASE_STRATEGY(TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM=database_inline),
  11. TABLE_STRATEGY(TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline)
  12. );
  • Use the altered default sharding database strategy, alter standard sharding table rule to the specified sharding algorithm being altered
  1. -- alter sharding algorithms
  2. ALTER SHARDING ALGORITHM database_inline (
  3. TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 4}"))
  4. ), table_inline (
  5. TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${order_id % 4}"))
  6. );
  7. -- alter a default sharding database strategy
  8. ALTER DEFAULT SHARDING DATABASE STRATEGY (
  9. TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=database_inline
  10. );
  11. -- alter a sharding table rule to the specified sharding algorithm being altered
  12. ALTER SHARDING TABLE RULE t_order (
  13. DATANODES("resource_${0..3}.t_order_item${0..3}"),
  14. TABLE_STRATEGY(TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline)
  15. );
  • Use both the altered default sharding and the altered default sharding strategy, alter standard sharding table rule
  1. -- alter sharding algorithms
  2. ALTER SHARDING ALGORITHM database_inline (
  3. TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 4}"))
  4. ), table_inline (
  5. TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${order_id % 4}"))
  6. );
  7. -- alter a default sharding database strategy
  8. ALTER DEFAULT SHARDING DATABASE STRATEGY (
  9. TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=database_inline
  10. );
  11. -- alter a default sharding table strategy
  12. ALTER DEFAULT SHARDING TABLE STRATEGY (
  13. TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline
  14. );
  15. -- alter a sharding table rule
  16. ALTER SHARDING TABLE RULE t_order (
  17. DATANODES("resource_${0..3}.t_order_item${0..3}")
  18. );
  • Alter standard sharding table rule and create sharding algorithms at the same time
  1. ALTER SHARDING TABLE RULE t_order (
  2. DATANODES("ds_${0..1}.t_order_${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=user_id, SHARDING_ALGORITHM(TYPE(NAME="inline", PROPERTIES("algorithm-expression"="ds_${order_id % 2}"))))
  5. );

2.Auto sharding table rule

  • alter auto sharding table rule
  1. ALTER SHARDING TABLE RULE t_order (
  2. RESOURCES(ds_0, ds_1),
  3. SHARDING_COLUMN=order_id, TYPE(NAME="MOD", PROPERTIES("sharding-count"="4"))
  4. );

Reserved word

ALTER, SHARDING, TABLE, RULE, DATANODES, DATABASE_STRATEGY, TABLE_STRATEGY, KEY_GENERATE_STRATEGY, RESOURCES, SHARDING_COLUMN, TYPE, SHARDING_COLUMN, KEY_GENERATOR, SHARDING_ALGORITHM, COLUMN, NAME, PROPERTIES, AUDIT_STRATEGY, AUDITORS, ALLOW_HINT_DISABLE