5 水平分表 & 垂直分表
在前篇的文档中我们通过实例已经实现过水平分表的演示,这里不再重复介绍。垂直分表依据概念其实是依据表字段的重要性,拆分为多个表,类似与父子表的结构,数据库设计中经常使用,这里也不再进行演示。
6 水平分库
依照前边文档中的案例我们来实现水平分库,水平分库其实是把同一个表的数据按照规则拆分到不同的数据库中。
1、将原有order_db数据库拆分为order_db_1和order_db_2两个数据库,
2、分片规则修改,由于数据库拆分成两个,所以数据源应该配置为两个,分库的策略和分表的策略类似。
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: order1,order2
order1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_1
username: root
password: 123456
order2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_2
username: root
password: 123456
sharding:
tables:
t_order:
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: order$->{user_id % 2 + 1}
3、测试订单插入和订单查询,代码沿用快速入门阶段代码即可。
4、分库分表整合分片策略。
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: order1,order2
order1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_1
username: root
password: 123456
order2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_2
username: root
password: 123456
sharding:
tables:
t_order:
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: order$->{user_id % 2 + 1}
actualDataNodes: order$->{1..2}.t_order_$->{1..2} #配置节点
keyGenerator: #主键策略
column: order_id
type: SNOWFLAKE
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_$->{order_id % 2 + 1}
7 垂直分库
前面已经介绍过垂直分库的基本概念,指按照业务将表进行分类,分布到不同的数据库上面,每个库可以放到不同的服务器上,它的核心理念是专库专用。
1、创建数据库user_db
CREATE DATABASE 'user_db' CHARACTER 'utf8';
2、创建用户表
CREATE TABLE 't_user' (
'user_id' bigint(20) NOT NULL COMMENT '用户id',
'name' varchar(20) NOT NULL COMMENT '用户姓名',
'user_type' char(1) NOT NULL COMMENT '用户类型',
PRIMARY KEY ('user_id') USING BTREE
);
3、分片规则配置
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: order1,order2,userDb
order1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_1
username: root
password: 123456
order2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_2
username: root
password: 123456
#新增user_db数据源配置
userDb:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/user_db
username: root
password: 123456
sharding:
tables:
t_order:
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: order$->{user_id % 2 + 1}
actualDataNodes: order$->{1..2}.t_order_$->{1..2} #配置节点
keyGenerator: #主键策略
column: order_id
type: SNOWFLAKE
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_$->{order_id % 2 + 1}
#垂直分库策略;由于没有分库分表,所以节点和表分片策略可以直接写死
t_user:
actualDataNodes: userDb.t_user #配置节点
#配置表分片规则
tableStrategy:
inline:
shardingColumn: user_id
algorithmExpression: t_user
8 公共表
公共表属于系统中数据量较小,变动少,而且属于高频联合查询的依赖表,例如参数表、字典表等,可以将这些表每个数据库都保存一份,所有更新操作都同时发送到所有分库节点执行。
1、创建所有数据库中公共表t_dict
。
CREATE TABLE 't_dict' (
'dict_id' bigint(20) NOT NULL COMMENT '字典id',
'type' varchar(20) NOT NULL COMMENT '字典类型',
'code' varchar(20) NOT NULL COMMENT '字典编码',
'value' varchar(20) NOT NULL COMMENT '字典值',
PRIMARY KEY ('dict_id') USING BTREE
);
2、配置公共表规则
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: order1,order2,userDb
order1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_1
username: root
password: 123456
order2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/order_db_2
username: root
password: 123456
#新增user_db数据源配置
userDb:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:33068/user_db
username: root
password: 123456
sharding:
#配置公共表
broadcast-tables: t_dict
tables:
t_order:
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: order$->{user_id % 2 + 1}
actualDataNodes: order$->{1..2}.t_order_$->{1..2} #配置节点
keyGenerator: #主键策略
column: order_id
type: SNOWFLAKE
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_$->{order_id % 2 + 1}
t_user:
actualDataNodes: userDb.t_user #配置节点
tableStrategy:
inline:
shardingColumn: user_id
algorithmExpression: t_user