CREATE SHARDING TABLE RULE
语法用于为当前所选逻辑库添加分片规则
CreateShardingTableRule ::=
'CREATE' '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 )*
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
用于指定主键生成策略,为可选项,关于主键生成策略可参考分布式主键-- 创建分片算法
CREATE SHARDING ALGORITHM database_inline (
TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${user_id % 2}"))
), table_inline (
TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${order_id % 2}"))
);
-- 指定分片算法创建分片规则
CREATE SHARDING TABLE RULE t_order (
DATANODES("ds_${0..1}.t_order_${0..1}"),
DATABASE_STRATEGY(TYPE=standard, SHARDING_COLUMN=user_id, SHARDING_ALGORITHM=database_inline),
TABLE_STRATEGY(TYPE=standard, SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline)
);
-- 创建分片算法
CREATE SHARDING ALGORITHM database_inline (
TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${user_id % 2}"))
), table_inline (
TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${order_id % 2}"))
);
-- 创建默认分库策略
CREATE DEFAULT SHARDING DATABASE STRATEGY (
TYPE = standard, SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=database_inline
);
-- 指定分片算法创建分片规则
CREATE SHARDING TABLE RULE t_order (
DATANODES("ds_${0..1}.t_order_${0..1}"),
TABLE_STRATEGY(TYPE=standard, SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline)
);
-- 创建分片算法
CREATE SHARDING ALGORITHM database_inline (
TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${user_id % 2}"))
), table_inline (
TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${order_id % 2}"))
);
-- 创建默认分库策略
CREATE DEFAULT SHARDING DATABASE STRATEGY (
TYPE = standard, SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=database_inline
);
-- 创建默认分表策略
CREATE DEFAULT SHARDING TABLE STRATEGY (
TYPE = standard, SHARDING_COLUMN=order_id, SHARDING_ALGORITHM=table_inline
);
-- 创建分片规则
CREATE SHARDING TABLE RULE t_order (
DATANODES("ds_${0..1}.t_order_${0..1}")
);
CREATE SHARDING TABLE RULE t_order (
DATANODES("ds_${0..1}.t_order_${0..1}"),
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}"))))
);
CREATE SHARDING TABLE RULE t_order (
RESOURCES(ds_0, ds_1),
SHARDING_COLUMN=order_id, TYPE(NAME=MOD, PROPERTIES("sharding-count"=4))
);
CREATE、SHARDING、TABLE、RULE、DATANODES、DATABASE_STRATEGY、TABLE_STRATEGY、KEY_GENERATE_STRATEGY、RESOURCES、SHARDING_COLUMN、TYPE、SHARDING_COLUMN、KEY_GENERATOR、SHARDING_ALGORITHM、COLUMN、NAME、PROPERTIES