ALTER SHARDING TABLE RULE
语法用于修改当前所选逻辑库的分片规则
AlterShardingTableRule ::=
'ALTER' 'SHARDING' 'TABLE' 'RULE' ( tableDefinition | autoTableDefinition ) ( ',' ( tableDefinition | autoTableDefinition ) )*
tableDefinition ::=
tableName '(' 'DATANODES' '(' dataNode ( ',' dataNode )* ')' ( ',' 'DATABASE_STRATEGY' '(' strategyDefinition ')' )? ( ',' 'TABLE_STRATEGY' '(' strategyDefinition ')' )? ( ',' 'KEY_GENERATE_STRATEGY' '(' keyGenerateStrategyDefinition ')' )? ')'
autoTableDefinition ::=
tableName '(' 'RESOURCES' '(' resourceName ( ',' resourceName )* ')' ',' 'SHARDING_COLUMN' '=' columnName ',' algorithmDefinition ( ',' 'KEY_GENERATE_STRATEGY' '(' keyGenerateStrategyDefinition ')' )?')'
strategyDefinition ::=
'TYPE' '=' strategyType ',' ( 'SHARDING_COLUMN' | 'SHARDING_COLUMNS' ) '=' columnName ',' algorithmDefinition
keyGenerateStrategyDefinition ::=
'KEY_GENERATE_STRATEGY' '(' 'COLUMN' '=' columnName ',' ( 'KEY_GENERATOR' '=' algorihtmName | algorithmDefinition ) ')'
algorithmDefinition ::=
('SHARDING_ALGORITHM' '=' algorithmName | 'TYPE' '(' 'NAME' '=' algorithmType ( ',' 'PROPERTIES' '(' propertyDefinition ')' )?')' )
propertyDefinition ::=
( key '=' value ) ( ',' key '=' value )*
tableName ::=
identifier
resourceName ::=
identifier
columnName ::=
identifier
algorithmName ::=
identifier
tableDefinition
为标准分片规则定义;autoTableDefinition
为自动分片规则定义。标准分片规则和自动分片规则可参考数据分片;DATANODES
只能使用已经添加到当前逻辑库的资源,且只能使用 INLINE 表达式指定需要的资源;DATABASE_STRATEGY
、TABLE_STRATEGY
表示分库和分表策略,均为可选项,未配置时使用默认策略;strategyDefinition
中属性 TYPE
用于指定分片算法的类型,目前仅支持 STANDARD
、COMPLEX
。使用 COMPLEX
时需要用 SHARDING_COLUMNS
指定多个分片键。RESOURCES
只能使用已经添加到当前逻辑库的资源,可通过枚举或 INLINE 表达式指定需要的资源;algorithmType
为分片算法类型,分片算法类型请参考分片算法;tableName
_ strategyType
_ algorithmType
;tableName
_ strategyType
;KEY_GENERATE_STRATEGY
用于指定主键生成策略,为可选项,关于主键生成策略可参考分布式主键。-- 修改分片算法
ALTER SHARDING ALGORITHM database_inline (
TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 4}"))
), table_inline (
TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${order_id % 4}"))
);
-- 修改分片规则为指定分片算法
ALTER SHARDING TABLE RULE t_order (
DATANODES("resource_${0..3}.t_order_item${0..3}"),
DATABASE_STRATEGY(TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM=database_inline),
TABLE_STRATEGY(TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline)
);
-- 修改分片算法
ALTER SHARDING ALGORITHM database_inline (
TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 4}"))
), table_inline (
TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${order_id % 4}"))
);
-- 修改默认分库策略
ALTER DEFAULT SHARDING DATABASE STRATEGY (
TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=database_inline
);
-- 修改分片规则为指定分片算法
ALTER SHARDING TABLE RULE t_order (
DATANODES("resource_${0..3}.t_order_item${0..3}"),
TABLE_STRATEGY(TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline)
);
-- 修改分片算法
ALTER SHARDING ALGORITHM database_inline (
TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 4}"))
), table_inline (
TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${order_id % 4}"))
);
-- 修改默认分库策略
ALTER DEFAULT SHARDING DATABASE STRATEGY (
TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=database_inline
);
-- 修改默认分表策略
ALTER DEFAULT SHARDING TABLE STRATEGY (
TYPE="standard", SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline
);
-- 修改分片规则
ALTER SHARDING TABLE RULE t_order (
DATANODES("resource_${0..3}.t_order_item${0..3}")
);
ALTER SHARDING TABLE RULE t_order (
DATANODES("resource_${0..3}.t_order_item${0..3}"),
DATABASE_STRATEGY(TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM(TYPE(NAME="inline", PROPERTIES("algorithm-expression"="ds_${user_id % 2}")))),
TABLE_STRATEGY(TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM(TYPE(NAME="inline", PROPERTIES("algorithm-expression"="ds_${order_id % 2}"))))
);
ALTER SHARDING TABLE RULE t_order (
RESOURCES(ds_0, ds_1),
SHARDING_COLUMN=order_id, TYPE(NAME="MOD", PROPERTIES("sharding-count"="4"))
);
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