分表配置demo
databaseName: mydb # 逻辑数据库名称 dataSources: ds_0: url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false username: root password: root connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1 rules: - !SHARDING tables: t_order: # 需要分表的表名 actualDataNodes: ds_0.t_order_${0..1} # 分片节点配置,ds_0和ds_1分别对应两个数据源,t_order_0和t_order_1是分片后的表名 tableStrategy: standard: shardingColumn: id # 分片键 shardingAlgorithmName: t_order_inline # 分片算法名称 t_user: # 逻辑表名 actualDataNodes: ds_0.t_user_${202401..202403} # 实际数据节点,这里按月分片,假设分片到2023年的每个月 tableStrategy: standard: shardingColumn: workdate # 分片键 shardingAlgorithmName: sharding_by_time # 分片算法名称 #keyGenerateStrategy: # column: id # keyGeneratorName: snowflake # 主键生成策略名称 shardingAlgorithms: t_order_inline: type: INLINE # 分片算法类型 props: algorithm-expression: t_order_${id % 2} # 分片算法表达式,这里使用取模算法 sharding_by_time: type: INTERVAL props: datetime-pattern: yyyy-MM-dd # 时间格式 datetime-interval-amount: 1 # 分片间隔数量 datetime-interval-unit: MONTHS # 分片间隔单位 datetime-lower: '2024-01-01' sharding-suffix-pattern: yyyyMM keyGenerators: snowflake: # 定义一个主键生成策略 type: SNOWFLAKE
建表语句
drop table if exists t_order_1; CREATE TABLE `t_order_1` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id', aa varchar(255) not null default '' COMMENT '', `createtime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间 [text no-add]', `updatetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间 [text no-add]', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `t_user_202401` ( `id` bigint(16) NOT NULL AUTO_INCREMENT COMMENT '主键', `workdate` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ); CREATE TABLE `t_user_202402` ( `id` bigint(16) NOT NULL AUTO_INCREMENT COMMENT '主键', `workdate` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ); CREATE TABLE `t_user_202403` ( `id` bigint(16) NOT NULL AUTO_INCREMENT COMMENT '主键', `workdate` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`) );
标签:COMMENT,datetime,demo,shardingsphere5,order,分片,分表,NULL,id From: https://www.cnblogs.com/wujf/p/18348871